Java:从其他类更新变量

时间:2014-10-23 11:50:46

标签: java class variables graphics

我正在努力解决一个我似乎无法解决的奇怪问题。我使用了各种方法(比如直接通过shape.rightident = 1设置变量等)。

类别:

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;

public abstract class MyShape {

protected int rightidentifier = -1; // to make sure you get an error when it is not being changed.
protected int x1, y1, x2, y2;
protected int width, height, startx, starty;

public MyShape() {
    this(0, 0, 0, 0);
}

public MyShape(int x1, int y1, int x2, int y2) {
    this.x1 = x1;
    this.x2 = x2;
    this.y1 = y1;
    this.y2 = y2;
}


public void setRightIdent(int identt) {
    this.rightidentifier = identt;
}

public int getRightIdent() {
    return this.rightidentifier;
}
}

现在,当我尝试设置变量时:

public void addToShapeList(MyShape shape) {
    int currcount;
    switch(shape.type) {
        case "Rectangle":
            rectanglecount++;
            currcount = rectanglecount;
            break;
        case "Line":
            linecount++;
            currcount = linecount;
            break;
        case "Ellipse":
            ellipsecount++;
            currcount = ellipsecount;
            break;
        default:
            currcount = 0;
            break;
    }
    listModel.addElement(shape.type + " " + currcount);
    shape.rightidentifier = listModel.size() - 1;
    shape.setRightIdent(listModel.size()-1);
    System.out.println("setted rightident to " + (listModel.size() -1));
    System.out.println("Rightident now:" + shape.getRightIdent());
}

public void deleteFromList(MyShape shape) {
    System.out.println("tried to remove rightident: " + shape.getRightIdent());
    listModel.remove(shape.getRightIdent());

}

然后系统输出:

setted rightident to 0
Rightident now:0
tried to remove rightident: -1

似乎对象没有保存更新的变量,我该怎么做才能保存变量?

修改:完整代码:https://github.com/Rubenkl/peent-daanruben/tree/peent2-ruben/Colorclicker/src

可以在此处找到调用addtoShape和deletefromShape的类: https://github.com/Rubenkl/peent-daanruben/blob/peent2-ruben/Colorclicker/src/DrawingPanel.java

1 个答案:

答案 0 :(得分:0)

我发现了问题。您永远不会在 DrawingPanel.java 中新创建的形状上设置正确的标识。这是我对你的解决方法:

    switch(command) {
                case "Rectangle":
                    shape = new MyRectangle(start.x, start.y, end.x, end.y);
                    shape.setRightIdent(index); // here
                    if (newww) {
                        rightPanel.addToShapeList(shape);
                        shapesList.add(shape);
                    } else {
                        shape.getRightIdent());
                        shapesList.set(index, shape);
                    }
                    break;
                case "Line":
                    shape = new MyLine(start.x, start.y, end.x, end.y);
                    shape.setRightIdent(index); // here
                    if (newww) {
                        rightPanel.addToShapeList(shape);
                        shapesList.add(shape);
                    } else {
                        shapesList.set(index, shape);
                    }

                    break;
                case "Ellipse":
                    shape = new MyEllipse(start.x, start.y, end.x, end.y);
                    shape.setRightIdent(index); // here
                    if (newww) {
                        rightPanel.addToShapeList(shape);
                        shapesList.add(shape);
                    } else {
                        shapesList.set(index, shape);
                    }
                    break;
相关问题