2014-03-07 3 views
0

처리가 새로 생겼으므로이 질문의 내용은 ... 생성하는 그래픽에 끌어서 놓기 기능을 추가하고 싶습니다.처리 - 그래픽에 끌기 기능을 추가하는 방법

특정 객체에 드래그를 추가하는 방법을 설명하는 아직 발견되지 않은 일반적인 솔루션을 찾고 있는데 각 객체에 드래그 할 수있는 메소드를 추가해야합니다. 드래그 할 수 있기를 원합니다.

감사합니다.

답변

1

하나의 옵션은 확장 할 수있는 클래스에 기능을 캡슐화하는 것입니다. 그래픽이 그러한 클래스를 확장하면 "드래그 가능"이됩니다.

다음 그래픽은 단순히 박스입니다 최소한의 예는, 그러나 "드래그 동작을"확장 : 다형성

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); 
    } 
} 
:

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); 
    } 
} 

이는 OOP 개념을 테스트하는 재미 작은 기회가 될 것입니다

이것은 구현 방법에 대한 아이디어이지만이를 달성하는 방법은 여러 가지가 있습니다. Processing OOP tutorial 이상을 살펴보십시오. Java OOP one 예를 들어 위의 내용은 인터페이스 또는 AbstractClass를 사용하여 얻을 수 있습니다. 귀하의 목표와 제한에 따라 최선의 해결책이 결정됩니다.

+0

George에게 감사드립니다. 이미 사용할 수 있거나 확장을 통해 이미 빌드 된 것이 있으면 좋겠지 만 게시글을 사용하면 직접 할 수 있어야합니다. 다시 한번 감사드립니다. – dorjeduck

관련 문제