Win a copy of Functional Design and Architecture this week in the Functional programming forum!
  • 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 ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Why the blue and red rectangle exceed the JPanel border?

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








 
Bartender
Posts: 4619
182
  • 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:

 
reply
    Bookmark Topic Watch Topic
  • New Topic