简体   繁体   English

Java class 变量在类之间更新不一致

[英]Java class variables are updating inconsistently between classes

In general, my goal is to draw lines defined by the user's cursor.一般来说,我的目标是绘制由用户的 cursor 定义的线条。 To accomplish this, I am figuring out the calculations for those lines in one class, and then updating the class that paints the lines with those new values.为了实现这一点,我在一个 class 中计算出这些线条的计算,然后更新用这些新值绘制线条的 class。 Overall I want to access a list of line segments, a list of nodes, and the current abstract "node" that the cursor is located at.总的来说,我想访问 cursor 所在的线段列表、节点列表和当前抽象“节点”。 (Nodes and Line Segments are my own defined classes). (节点和线段是我自己定义的类)。 The class in which the lines are drawn is called GraphicsPanel.绘制线条的 class 称为 GraphicsPanel。

I set up access between the classes as follows:我在类之间设置访问权限,如下所示:

public class MainClass extends JFrame {
    protected static ArrayList<LineSegment> lineList = new ArrayList<LineSegment>();
    protected static ArrayList<Node> nodeList = new ArrayList<Node>();
    protected static Node current = new Node();

    // Code for calculations and user interactions
    {
        GraphicsPanel displayPanel = new GraphicsPanel();

        // Values are updated
        displayPanel.revalidate();
        displayPanel.repaint();
    }
}

public class GraphicsPanel extends JPanel {

    private ArrayList<LineSegment> lineList = package.MainClass. lineList;
    private ArrayList<Node> nodeList = package.MainClass.nodeList;
    private Node current = package.MainClass.current;

    public GraphicsPanel() {

    }
    public void paint(Graphics g) {
       // Paint lines and other shapes
    }
}

While the lineList and nodeList objects get correctly updated when a new LineSegment or Node is added to the list, the current Node is never updated and always shows the default of (0, 0).虽然 lineList 和 nodeList 对象在将新 LineSegment 或 Node 添加到列表时会正确更新,但当前 Node 永远不会更新,并且始终显示默认值 (0, 0)。

As an example, within the main class, I have two mouse listeners, one for mouse clicks and one for mouse movement.例如,在主 class 中,我有两个鼠标监听器,一个用于鼠标点击,一个用于鼠标移动。 They have similar functions, but the mouse click listener updates both the current Node and the lineList ArrayList of LineSegments.它们有类似的功能,但是鼠标点击监听器同时更新当前节点和 LineSegments 的 lineList ArrayList。

displayPanel.addMouseListener(new MouseListener() {
    @Override
    public void mousePressed(MouseEvent e) {
        Point p = e.getPoint();
        current = new Node(p.getX(), p.getY());

        lineList.add(new LineSegment(current, current);
        // Don't worry, the line segment gets updated (correctly) with a new end node as
        // the cursor moves around the window

        displayPanel.revalidate();
        displayPanel.repaint();
    }           
});

When I click on the window to create lines, the lines show up as expected but the current Node remains at (0, 0).当我单击 window 创建线条时,线条按预期显示,但当前节点仍位于 (0, 0)。 I am completely flabbergasted by this, since it seems like only one of the variables is updating, even though both are written to update in basically the same way: I modify the instance of the class variable in the main class, which should modify the instance of the variable in the GraphicsPanel class.我对此完全感到震惊,因为似乎只有一个变量正在更新,即使两者都以基本相同的方式写入更新:我修改主 class 中的 class 变量的实例,这应该修改实例GraphicsPanel class 中的变量。

I appreciate any help with this conundrum and welcome suggestions for what's wrong, as well as better ways to approach this application.感谢您对这个难题的任何帮助,并欢迎就问题所在提出建议,以及处理此应用程序的更好方法。

You don't modify the instance, you create a new instance, replacing the old.您不修改实例,而是创建一个new实例,替换旧实例。 This means that GraphicsPanel.current will keep pointing to the original instance, but MainClass.current will point to a new distance.这意味着GraphicsPanel.current将继续指向原始实例,但MainClass.current将指向一个新的距离。

If you did something like instance.setY(p.getY()) , it would modify the single original instance that both classes are pointing to.如果你做了类似instance.setY(p.getY())的事情,它会修改两个类都指向的单个原始实例。

Your MouseListener adds objects to the Main class list.您的 MouseListener 将对象添加到 Main class 列表中。

Then it assigns a different new object to the panel reference current .然后它为面板参考电流分配一个不同的新 object。 But that doesn't change the Main class refence!但这不会改变主要的 class 参考!

You could just not have another current reference in the panel code.您可能只是在面板代码中没有另一个当前引用。 Simply directly assign to the current instance that belongs to the Main class!只需直接分配给属于 Main 类的当前实例!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM