Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8338257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:32:10+00:00 2026-06-09T04:32:10+00:00

I’m having trouble with a game I’m working on right now; the purpose of

  • 0

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 640×640 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:32:12+00:00Added an answer on June 9, 2026 at 4:32 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.