Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Why the blue and red rectangle exceed the JPanel border?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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: 4109
156
  • Mark post as helpful
  • send pies
  • 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:

 
I'm not sure if I approve of this interruption. But this tiny ad checks out:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic