0
我是新來處理,所以這個問題可能是... 我想添加拖放設施到我生成的圖形。處理 - 如何添加一個拖放設施到圖形
我發現Processing - Mouse Functions它解釋瞭如何添加拖動到特定的對象,但我正在尋找一個通用的解決方案,我不必爲每個對象添加拖動方法,我希望能夠拖動。
謝謝
我是新來處理,所以這個問題可能是... 我想添加拖放設施到我生成的圖形。處理 - 如何添加一個拖放設施到圖形
我發現Processing - Mouse Functions它解釋瞭如何添加拖動到特定的對象,但我正在尋找一個通用的解決方案,我不必爲每個對象添加拖動方法,我希望能夠拖動。
謝謝
一種選擇是將功能封裝到您可以擴展的類中。 如果你的圖形擴展了這樣一個類,那麼它們也變成了「可拖動的」。
這裏有一個小例子,其中圖形是簡單的盒子,但延長「拖拽行爲」:
int nb = 3;
Box[] boxes = new Box[nb];//a list of draggable graphics, currently empty
void setup(){
size(400,400);
for(int i = 0 ; i < nb; i++){
boxes[i] = new Box();//populate the list with actual objects
boxes[i].x = 10+110*i;//and setup their coordinates
boxes[i].y = 100;//and dimenensions
boxes[i].w = boxes[i].h = 100;
}
}
void draw(){
background(0);//clear
for(int i = 0 ; i < nb; i++){
boxes[i].update(mouseX,mouseY);//update the internal state(if it's over or not, calculate drag offset, etc.)
boxes[i].draw();//render each graphics element on screen
}
}
void mouseDragged(){//if the mouse is dragged
for(int i = 0 ; i < nb; i++){//for each graphics element
if(boxes[i].isOver) {//if it's over
boxes[i].x = mouseX-boxes[i].offx;//than drag based on the mouse position
boxes[i].y = mouseY-boxes[i].offy;//but take te mouse offset in relation to each object into account
}
}
}
class Draggable{//a generic draggable template with no graphics to display
float x,y,w,h,offx,offy;//position, dimensions and x,y offset to drag
boolean isOver;//is the cursor over the bounding box of this object ?
void update(int mx,int my){//let's work that out based on the mouse x and y coordinates
isOver = ((mx >= x && mx <= x+w) && (my >= y && my <= y+h));//if it's within bounds on x and y axis, then we're in the over state
if(isOver){//if we're in the over state we can also update the mouse drag offsets
offx = mx-x;
offy = my-y;
}
}
}
class Box extends Draggable{
void draw(){
fill(isOver ? 127 : 255);
rect(x,y,w,h);
}
}
這將是檢驗一個面向對象的概念,一個有趣的小機會:多態性
int nb = 6;
Draggable[] boxes = new Draggable[nb];//a list of draggable graphics, currently empty
void setup(){
size(400,400);
for(int i = 0 ; i < nb; i++){
boxes[i] = (random(1.0) > .5) ? new Box() : new Blob();//populate the list with actual objects
boxes[i].x = 10+110*i;//and setup their coordinates
boxes[i].y = 100;//and dimenensions
boxes[i].w = boxes[i].h = 100;
}
}
void draw(){
background(0);//clear
for(int i = 0 ; i < nb; i++){
boxes[i].update(mouseX,mouseY);//update the internal state(if it's over or not, calculate drag offset, etc.)
boxes[i].draw();//render each graphics element on screen
}
}
void mouseDragged(){//if the mouse is dragged
for(int i = 0 ; i < nb; i++){//for each graphics element
if(boxes[i].isOver) {//if it's over
boxes[i].x = mouseX-boxes[i].offx;//than drag based on the mouse position
boxes[i].y = mouseY-boxes[i].offy;//but take te mouse offset in relation to each object into account
}
}
}
class Draggable{//a generic draggable template with no graphics to display
float x,y,w,h,offx,offy;//position, dimensions and x,y offset to drag
boolean isOver;//is the cursor over the bounding box of this object ?
void update(int mx,int my){//let's work that out based on the mouse x and y coordinates
isOver = ((mx >= x && mx <= x+w) && (my >= y && my <= y+h));//if it's within bounds on x and y axis, then we're in the over state
if(isOver){//if we're in the over state we can also update the mouse drag offsets
offx = mx-x;
offy = my-y;
}
}
void draw(){}//empty implementation to be overwritten by a subclass
}
class Box extends Draggable{
void draw(){
fill(isOver ? 127 : 255);
rect(x,y,w,h);
}
}
class Blob extends Draggable{
void draw(){
fill(isOver ? 127 : 255);
ellipse(x,y,w,h);
}
}
這是一個如何實現這個想法,但有多種方式來實現這一點。 看看Processing OOP tutorial或更深入的Java OOP one 例如,上述可以使用接口或AbstractClass來實現。 這取決於你的目標和限制什麼是最好的解決方案。
非常感謝喬治。我有點希望已經有東西可用或者通過擴展來構建,但是對於你的文章,應該很容易做到。再次感謝。 – dorjeduck