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
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.
Out on HF and heard nobody, but didn't call CQ? Nobody heard you either. 73 de N7GH
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.
There are three kinds of actuaries: those who can count, and those who can't.