如何不覆盖现有的对象列表?
How to not overwrite existing list of objects?
我想在点击 canvas 时画线,所以如果我点击一次然后保存点,如果我点击第二次然后在这两个点后面画一条线。但是,我想做多次,所以,如果我点击第三次,那么这个点将成为新线的起点。
我这样创建的:
这是在 main
:
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");
}
}
}
}
并且我使用 shape
class 并且此 class 扩展为 line
:
abstract class Shape {
PVector position = new PVector();
PVector fill_color = new PVector(0, 0, 0);
PVector stroke_color = new PVector(0, 0, 0);
PVector select_fill_color = new PVector(255, 0, 0);
PVector select_stroke_color = new PVector(255, 0, 0);
Boolean selected = false;
int shape_color_r;
int shape_color_g;
int shape_color_b;
int shape_rotation_angle = 0;
int detailness = 10;
abstract void Draw();
abstract void Log();
}
和:
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);
}
}
但是最后创建的行总是覆盖旧的,我该如何解决这个问题?我想我需要为每一行创建一个新实例,但我不知道该怎么做。
变量l
指的是Line
对象,它保存着当前绘制的线的坐标。
如果您完成了一行,则会将对行对象 l
的引用添加到容器 shapes
。现在您必须为下一行创建一个新的行对象并将其分配给 l
:
shapes.add(l);
l = new Line();
我想在点击 canvas 时画线,所以如果我点击一次然后保存点,如果我点击第二次然后在这两个点后面画一条线。但是,我想做多次,所以,如果我点击第三次,那么这个点将成为新线的起点。
我这样创建的:
这是在 main
:
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");
}
}
}
}
并且我使用 shape
class 并且此 class 扩展为 line
:
abstract class Shape {
PVector position = new PVector();
PVector fill_color = new PVector(0, 0, 0);
PVector stroke_color = new PVector(0, 0, 0);
PVector select_fill_color = new PVector(255, 0, 0);
PVector select_stroke_color = new PVector(255, 0, 0);
Boolean selected = false;
int shape_color_r;
int shape_color_g;
int shape_color_b;
int shape_rotation_angle = 0;
int detailness = 10;
abstract void Draw();
abstract void Log();
}
和:
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);
}
}
但是最后创建的行总是覆盖旧的,我该如何解决这个问题?我想我需要为每一行创建一个新实例,但我不知道该怎么做。
变量l
指的是Line
对象,它保存着当前绘制的线的坐标。
如果您完成了一行,则会将对行对象 l
的引用添加到容器 shapes
。现在您必须为下一行创建一个新的行对象并将其分配给 l
:
shapes.add(l);
l = new Line();