Processing 处理-如何向图形中添加拖动工具
我是新的处理,所以这个问题可能是。。。 我想在生成的图形中添加拖放功能 我发现这解释了如何添加拖动到一个特定的对象,但我正在寻找一个通用的解决方案,我不必添加拖动方法为每个对象,我想能够拖动周围Processing 处理-如何向图形中添加拖动工具,processing,Processing,我是新的处理,所以这个问题可能是。。。 我想在生成的图形中添加拖放功能 我发现这解释了如何添加拖动到一个特定的对象,但我正在寻找一个通用的解决方案,我不必添加拖动方法为每个对象,我想能够拖动周围 谢谢一个选项是将功能封装到一个可以扩展的类中。 如果您的图形扩展了这样一个类,那么它们也变得“可拖动” 下面是一个简单的示例,其中图形只是框,但扩展了“可拖动行为”: int nb=3; 方框[]方框=新方框[nb]//可拖动图形的列表,当前为空 无效设置(){ 尺寸(400400); 对于(int i
谢谢一个选项是将功能封装到一个可以扩展的类中。 如果您的图形扩展了这样一个类,那么它们也变得“可拖动” 下面是一个简单的示例,其中图形只是框,但扩展了“可拖动行为”:
int nb=3;
方框[]方框=新方框[nb]//可拖动图形的列表,当前为空
无效设置(){
尺寸(400400);
对于(int i=0;i isOver=((mx>=x&&mx=y&&my=x&&mx=y&&my=y&&my)非常感谢George。我希望已经有了内置的或通过扩展的东西,但是通过你的帖子,我自己应该很容易做到。再次感谢。
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);
}
}