Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS 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

Bound at 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 im new in java. I want to make  objects bounds at the Jpanel border(so it will not excedd JPanel border), but what i did is something like below. I need to make simple calculation by subtracting  the width of the image from The Jpanel width, to make  it not exceed Jpanel border. But still it exceed the JPanel border slightly. and i dont know but i think there is still better method than using the subtraction method.




Before this i learned that i can use below method to make the bounding more precisely(using this.getWidth()), but only if i make the object code within class with extends Jpanel class, so that why i can use ("this") right?




So when we use different class to make the object code(that does extends Jpanel) , please teach me how to put the method (using this.getWidth()), into class with extended JPanel.
Sorry for the difficult explanation. Thanks in Advance.
Below is the full code














 
Rancher
Posts: 978
23
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
don,

remember that your object you are moving around has real size on your screen.  You have a conditional where your location for x < 0, well, if your location is less than 0, then you are out of the border already, so you have to reset your x position to 0 as well as changing the direction of the x component of your velocity. Same as when you go to the right, but you have your (JPanel border)-(object witdh) as your right most.  The same is true for your y component and your top and bottom limits.

Les
 
Bartender
Posts: 4633
182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
About the problem of the rectangle exceeding the right panelborder: see my reply in this topic: similar problem.

In short: don't set the size of the frame, but give your panel the desired preferred size. Try these examples and see the difference:

The red line should go from the upperleft to the bottomright, but as you see that fails pretty much. The 'fame.setSize(..) is obviously not good enough. Here is a version the sets the preferred size of the panel:

Also, work with the widths and heights of the panel and rectangles, instead of hard coding these numbers in your code. That makes you program much more flexible.

And: give your classes some meaningful names. Names like 'f13' and 'f14' do not say much about their meaning.
 
Piet Souris
Bartender
Posts: 4633
182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I forgot to mention:

if your panel is defined in a separate class, as you have it here (and it extends JPanel), then you can set the preferred size as follows:

or you follow the way I did in my above reply.
 
New rule: no elephants at the chess tournament. Tiny ads are still okay.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic