Processing 如何不覆盖现有的对象列表?
我想在单击画布时画一条线,因此如果单击一次,则保存该点;如果单击第二次,则在这两个点后面画一条线。但是,我想多次这样做,所以,如果我第三次单击,那么这个点将是新行的起点。 我是这样创作的: 这在Processing 如何不覆盖现有的对象列表?,processing,line,instance,Processing,Line,Instance,我想在单击画布时画一条线,因此如果单击一次,则保存该点;如果单击第二次,则在这两个点后面画一条线。但是,我想多次这样做,所以,如果我第三次单击,那么这个点将是新行的起点。 我是这样创作的: 这在主菜单中: ArrayList<Shape> shapes = new ArrayList<Shape>(); Shape selected_shape = null; Boolean drawmode = true; void setup() { siz
主菜单中
:
ArrayList<Shape> shapes = new ArrayList<Shape>();
Shape selected_shape = null;
Boolean drawmode = true;
void setup() {
size(1000, 600);
}
void draw() {
//update();
background(224, 224, 224);
//draw the existing
for(Shape s: shapes){
pushMatrix();
//list all
s.Draw();
s.Log();
popMatrix();
}
println("shape size: "+shapes.size());
}
//menu
int value = 0;
void keyPressed() {
if(key == '0'){
System.out.println("Draw mode OFF"); // exit from draw mode
value = 0;
}
if(key == '1'){
println("Draw a line: select the start point of the line and the end point!"); // line
value = 1;
}
//System.out.println("key: " + key);
}
Line l = new Line();
void mousePressed() {
if(value == 1){
if(l.centerIsSet){
if (mouseButton == LEFT) {
l.x2 = mouseX;
l.y2 = mouseY;
println("end point added");
l.centerIsSet = false;
}
shapes.add(l);
l.Log();
} else {
if (mouseButton == LEFT) {
l.x1 = mouseX;
l.y1 = mouseY;
l.centerIsSet = true;
println("start point added");
}
}
}
}
以及:
但最后创建的行总是覆盖旧行,如何解决此问题?我想每一条线都需要一个新的实例,但我不知道如何才能做到这一点。变量
l
指的是线
对象,它保存当前绘制的线的坐标
如果您已经完成了一行,那么对行对象l
的引用将添加到容器shapes
。现在,您必须为下一行创建一个新的line对象,并将其指定给l
:
shapes.add(l);
l = new Line();
class Line extends Shape {
int x1, x2, y1, y2;
Boolean centerIsSet = false;
Line(){}
Line(int x1, int y1){
this.x1 = x1;
this.y1 = y1;
}
Line(int x1, int y1, int x2, int y2){
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
}
void Draw(){
line(x1, y1, x2, y2);
}
void Log(){
System.out.println("x1: "+x1+" x2: "+x2+" y1: "+y1+" y2: "+y2);
}
}
shapes.add(l);
l = new Line();