• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Why the blue and red rectangle exceed the JPanel border?

Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, i have 2 questions from below code.
1) the blue rect  can stop at Jpanel border(x<0) but when i moved  it to the other border why is it exceed the border at (x>500/ meaning more than 500).
  note : i have to maximize the panel, to see the blue rect stop, otherwise i will not see it.

2) Related to questions 1, i try x-coordinated the red rect , so that the one side will place at coordinate 450 and the other side will place at 500.
   When i compile the program, why the red rect does not properly coordinated(only half seen)  .

Thank you.

Posts: 4670
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Don,

first question: the x and y are the topleft coordinates of a rectangle. The rectangle bounces off the right border when its right side reaches 500. So you should test for:

And the red rectangle does not move at all. See the paintComponent method, look at r2. Can you spot why it doesn't move?

And to make the bouncing off the border independent of the panel size, use something like:

Same for the y-coordinate. 'this' refers to the panel in question

Then the problem that that red rectangle is only shown partly. I'm not sure why that is so, but a general advice is:

do not set the size of a JFrame yourself. Let the panels for themselve decide how big they should be (and in many  cases even that is unnecessary: its layoutmanager will decide the size of a panel).

So, in the constructor of objectframe. I have this:

and the constructor of the objectPanel is now:

    Bookmark Topic Watch Topic
  • New Topic