截至球的路径 [英] ending the ball's path

查看:161
本文介绍了截至球的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小面板在那里我想提出一个球,只需改变它的 x坐标移动。 我想球移回遇到我的框架frame.The宽度年底时的 300 (由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×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 画下来,并给座标的指定权,所以你需要思考的是什么球坐标手段。难道中央的球,或左或右的一面呢?可能需要计算其是否已达到可绘制区域的侧面或不是当减去球的宽度

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆