简体   繁体   中英

Java class variables are updating inconsistently between classes

In general, my goal is to draw lines defined by the user's 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. 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. (Nodes and Line Segments are my own defined classes). The class in which the lines are drawn is called 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).

As an example, within the main class, I have two mouse listeners, one for mouse clicks and one for mouse movement. They have similar functions, but the mouse click listener updates both the current Node and the lineList ArrayList of LineSegments.

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). 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.

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. This means that GraphicsPanel.current will keep pointing to the original instance, but MainClass.current will point to a new distance.

If you did something like instance.setY(p.getY()) , it would modify the single original instance that both classes are pointing to.

Your MouseListener adds objects to the Main class list.

Then it assigns a different new object to the panel reference current . But that doesn't change the Main class refence!

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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