结束球的路径 [英] ending the ball's path
问题描述
我有一个小面板,我可以通过改变它的 x 坐标
来使球移动.我希望球在遇到帧尾时向后移动.我的帧宽度为 300(by fr.setSize(300,300))
.现在我对动画进行了编程:
I have a small panel where i am making a ball to move by just varying it's x co-ordinate
.
I want the ball move back when it encounters the end of frame.The width of my frame is 300
(by fr.setSize(300,300))
.
Now I programmed the animation like :
// when x == 300
// stop the timer
但是 x=300 似乎大于它的宽度 300 !这怎么可能.**球移出300 x 300
帧并变得不可见.为什么会这样?
But x=300 seems to be greater than it's width which is 300 ! How is this possible.
**The ball moves out of the 300 x 300
frame and becomes invisible.
Why is this happening ?
这些是最终发生的事情的屏幕截图.
These are the screen shots of what happens eventually.
第一张是移动的球,第二张是球不见了,第三张是放大后球还在.
为什么会这样.?如何将帧的终点设置为球的终点?
推荐答案
您需要考虑组件的可视大小.它不一定与您要求的尺寸相同.
You need to consider the viewable size of your component. It won't necessarily be the same as the size you requested.
您可以使用 getSize
方法来确定您的组件的实际大小,但您还需要调用 getInsets
来查看是否有任何空间已预留使用按边界.这将为您提供真实的可绘制区域:
You can use the getSize
method to determine the actual size of your component, but you also need to call getInsets
to find out if any space has been reserved for use by borders. This will give you the real, drawable area:
public void paint(Graphics g) {
Dimension size = getSize();
Insets insets = getInsets();
int available = size.width - insets.left - insets.right;
// Draw stuff. Remember to offset by insets.left and insets.top!
...
}
还请记住,像 fillOval
这样的 Graphics
例程在您指定的坐标的右侧向下绘制,因此您需要考虑球坐标的含义.它是球的中心,还是左侧或右侧?在计算是否到达可绘制区域的一侧时,您可能需要减去球的宽度.
Also remember that Graphics
routines like fillOval
draw down and to the right of the coodinate you specify, so you need to think about what the ball coordinate means. Is it the center of the ball, or the left or right side? You may need to subtract the width of the ball when calculating whether it has reached the side of the drawable area or not.
这篇关于结束球的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!