Java:从其他类更新变量

Java:从其他类更新变量,java,class,variables,graphics,Java,Class,Variables,Graphics,我正在努力解决一个我似乎无法解决的奇怪问题。。我使用了各种方法(如直接通过shape.righident=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 g

我正在努力解决一个我似乎无法解决的奇怪问题。。我使用了各种方法(如直接通过shape.righident=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
似乎对象没有保存更新的变量,我该如何保存变量

编辑:完整代码:

调用addtoShape和deletefromShape的类可以在以下位置找到:
我发现了这个问题。您从未在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;

在我看来,变量被正确设置为
listModel.size()-1
。您确定您的数据是正确的,并且打印的值是正确的吗?我看不出密码有什么问题。例如,是否确实要将相同的MyShape对象发送到addShapeToList和deleteFromList方法?看起来listModel对象的列表中只有1个元素。如我们所知,列表从0开始,如果只有一个成员,则以1结束。所以listModel.size()-1=0。当您在addToShapeList(MyShape shape)方法中说
shape.setRightIdentit(listModel.size()-1)
时。应该是零。您希望得到什么值???@olavimutanoja它应该将相同的MyShape对象发送到deleteFromList,因为只创建了一个形状。这正是我之所以使用setRightIdentit(ListModel.Size()-1)的原因,因为它应该将其设置为第一个位置。我基本上尝试实现的是,有两个列表填充了形状:一个用于绘画,另一个用于显示字段中的形状类型(就像photoshop中的图层一样)。今天晚些时候我将试用您的代码,但底线是发送到这两种方法的形状不一样。现在试着用这个假设:)这不是你需要设置的索引。因为它将是rightPanel列表的索引号。