Search code examples
javavariablesupdating

Variable will not update (in Java)


I'm having trouble with a game I'm working on right now; the purpose of this code is to change the location of the JComponent on the window. This is how I want it to play out:

1: Program starts, JFrame created at 640x640 pixels; JComponent is painted in direct center.

2: When "ESC" is pressed, JFrame blows up to full screen; JComponent is painted in direct center.

3: When "ESC" is pressed while the JFrame is in full screen, it reverts to its original size.

The issue I'm having is that the JComponent is not being repainted to the center of the full screen, despite my efforts at updating the variable. I decided to try debugging it by doing three System.out.println tests: the first one, "A", prints the value of parameter "loc" in the setLocation method. The second one, "B", prints the value of Point "location" after it changes its value from the original value to the value given by the parameter.

The third test, "C", is given precisely when the paintComponent method is called by repaint in setLocation, it prints the value of location like test "B". This is where things screw up.

The first set of tests are given when the JFrame is first painted; the location of the JComponent is the same in each test. The second set of tests are given when the JFrame is resized; the location of the JComponent should be updated in the setLocation method, as shown in "A" and "B". However, when I call for the location in test "C", the location is reverted to the original location of the JFrame.

public void setLocation(Point loc)
{
    location = new Point(loc.x, loc.y);
    System.out.println(loc + "A"); System.out.println(location + "B");
    repaint();
}
public void paintComponent(Graphics g)
{
    System.out.println(location + "C");
    Graphics2D g2 = (Graphics2D)(g);

    BufferedImage x = null;
    if(isMale)x = mobs[0];  else    x = mobs[1];
    g2.drawImage(rotate(x, direction), null, (int)location.getX(), (int)location.getY());
}

private Point location = new Point(280, 280);

Output:

java.awt.Point[x=280,y=280]A

java.awt.Point[x=280,y=280]B

java.awt.Point[x=280,y=280]C

java.awt.Point[x=1880,y=1040]A

java.awt.Point[x=1880,y=1040]B

java.awt.Point[x=280,y=280]C

java.awt.Point[x=280,y=280]A

java.awt.Point[x=280,y=280]B

java.awt.Point[x=280,y=280]C

If anyone can help me figure out why Point "location" does not stay at (1880, 1040) after it is updated, and then repainted, I'd really appreciate it.


Solution

  • I will change this to an answer:

    Shouldn't your setLocation(...) method override call its super method? If not, it won't do what it needs to be doing. And I'm not sure it should be calling repaint(). Also, what layout is your container that holds your JComponent using? There are several ways to center a component in a container including using GridBagLayout and adding the component without GridBagConstraints, that is in a default way. Finally, consider creating and posting an sscce to better demonstrate your problem.