2

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.

Reilly T
  • 23
  • 3

1 Answers1

2

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Just wondering, do you mean *decorate* as well as *override*? ie `setLocation()` should call `super.setLocation()`? Or possibly not be implemented at all? – Bohemian Aug 06 '12 at 02:40
  • @Bohemian: It's a pure override since his class must extend JComponent or one of its children, and if he does not initiate the super's method, I fear that objects of this class will misbehave with how the set their own location. – Hovercraft Full Of Eels Aug 06 '12 at 02:46
  • I call repaint() in setLocation(...) in order to paint the JComponent in the proper position after I toggle the full screen; the actual paint method works perfectly. I use setLocation to change the variable for later use, but that change doesn't occur. When I call the setLocation(...) method, I'm trying to change the original value of "location" to the parameter "loc". It seems to do that at the end of the method, but when repaint() is called, it's almost like the value of location was never changed. – Reilly T Aug 06 '12 at 02:59
  • @Reilly: You do understand that your `setLocation(...)` method overrides a method of the super class, right? – Hovercraft Full Of Eels Aug 06 '12 at 03:09
  • @HovercraftFullOfEels: It doesn't make any difference whether I'm overriding that particular method or not, since I'm not painting the component with the super's location value. I actually changed the name of the method to "setQ" for kicks, and it has the exact same effect as if I did setLocation; so I don't think overriding methods is the issue. – Reilly T Aug 06 '12 at 03:26
  • @Reilly: you haven't yet addressed my other issue regarding layout managers. Again, what layout is your container using? This will control where all components are eventually placed. – Hovercraft Full Of Eels Aug 06 '12 at 04:27
  • @HovercraftFullOfEels I don't know, I'm not as experienced with the swing package as I should be. I know why the JComponent isn't centering, it's because the "location" variable is not updating. I'm just going to end this question, since it hasn't gotten me anywhere. – Reilly T Aug 07 '12 at 15:57
  • @Reilly: If you show us more of your code and more of the problem, we likely can help you better, so don't give up on this stackoverflow thread just yet. Consider creating and posting a small test program that demonstrates the problem, a program that we are able to compile and run, an [sscce](http://sscce.org). – Hovercraft Full Of Eels Aug 07 '12 at 16:40