1

我正在开发一个使用 JApplet 的 Java 程序,它可以让球上下弹跳。我能够将形状绘制到 JApplet 和类似的东西上。我似乎无法让它移动。我对此进行了研究,发现我需要创建一个方法来暂停形状,然后从 JApplet 中清除它,更改坐标,然后在新区域中重新绘制形状,但由于某种原因,它对我不起作用。

预先感谢您的帮助。

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JApplet;


public class Circle extends JApplet {

int x=100;
int y=100;
int diameter=50;


public void paint(Graphics g) {

int xResize=500;
int yResize=500;

super.paint(g);
resize(xResize,yResize);
g.drawOval(x, y, diameter, diameter);   
}

public Circle (int startX, int startY,int startDiameter) {

this.x=startX;
this.y=startY;
this.diameter=startDiameter;

} 

public int getX() {
return x;
}
public void setX(int startX){
x=startX;
}
public int getY() {
return y;
}
public void setY(int startY){
y=startY;
}
public int getDiameter() {
return diameter;
}
public void setDiameter(int startDiameter){
diameter=startDiameter;


}

while (ball.getY() + ballDiameter < windowHeight) {

g.clearRect(x-1,100,20,20); 

g.fillOval(x,100,20,20); 

try 

{ 

Thread.sleep(70); 

} 

catch(Exception e) 

{ 

} 


pause.wait(0.05);

//g.clearRect(0,0,windowWidth,windowHeight);

g.clearRect(x-1,100,20,20); 

g.fillOval(x,100,20,20); 

try 

{ 

Thread.sleep(70); 

} 

catch(Exception e) 

{ 

} 

ball.setY( ball.getY()+spacer); 


}


while (ball.getY() + ballDiameter > 0) {

g.clearRect(x-1,100,20,20); 

g.fillOval(x,100,20,20); 

try 

{ 

Thread.sleep(70); 

} 

catch(Exception e) 

{ 

} 

pause.wait(0.05);
//g.clearRect(0,0, windowWidth, windowHeight);
ball.setY(ball.getY()-spacer);


}

弹跳球类

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JApplet;

public class BouncingBall extends JApplet {

public void paint(Graphics g) {

super.paint(g);

final int x = 0;
int y = 0;
final int diameter = 15;
final int spacer = 5;
int windowWidth = getWidth();
int windowHeight = getHeight();

Circle ball = new Circle(x, y, diameter);
Pause pause = new Pause();
int ballDiameter = ball.getDiameter();
int roof = getHeight();
4

1 回答 1

3

首先,动画是随时间变化的幻觉。所以首先,你需要一些方法来定期更新你的价值观。

其次,Swing 是一个单线程框架,这意味着任何阻塞该线程的东西都会阻止 Event Dispatching Thread 处理新事件,包括重绘请求。

第三,对 UI 的所有交互、更改或更新都应在 EDT 的上下文中执行。

这意味着,您需要某种方式在后台(关闭 EDT)等待,它可以在需要执行更新时通知您,并将这些更新同步回 EDT。

javax.swing.Timer是实现此目的的完美人选。可以在后台等待指定的时间;ActionListener当时间段到期并且可以重复时,它将在 EDT 的上下文中通知。

首先覆盖init,startstop的方法JApplet

@Override
public void init() {
    super.init(); 
    timer = new Timer(40, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
        }
    });
}

@Override
public void start() {
    super.start();
    timer.start();
}

@Override
public void stop() {
    timer.stop();
    super.stop(); 
}

基本上,这构造了 a Timer,适当地启动和停止它。

接下来,我们需要为动画提供一些逻辑......

正如我之前所说,动画是随着时间的推移运动的错觉。我们已经处理了时间部分(或多或少),现在我们需要运动。

基本思想是对当前值应用少量更改并提供边界检查并最终重新绘制结果。

x += delta;
if (x < 0) {
    x = 0;
    delta *= -1;
} else if (x + diameter > getWidth()) {
    x = getWidth() - diameter;
    delta *= -1;
}
repaint();

在这里,我delta在小程序中声明为实例变量并将其值设置为2. 这个逻辑应该添加到注册的actionPerformed方法中ActionListenerTimer

要让它运行,你需要删除你的构造函数,因为小程序必须有一个默认/空的构造函数。

您还应该resize从方法中删除调用paint,因为这只会导致发出更多的重绘请求,最终会消耗您的 CPU。

当你运行它时,你可能会幸运地看到这个圆圈,偶尔(或者它会闪烁)。这是因为 Swing 中的顶级容器不是双缓冲的。现在,您可以实现自己的缓冲策略,但 Swing 组件默认是双缓冲的……

为了解决这个问题,我们可以创建一个DrawPanel从 a 扩展的简单方法JPanel并覆盖它的paintComponent方法

所以你最终可能会得到更像......

在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Circle extends JApplet {

    private int delta = 2;

    private Timer timer;
    private DrawPane drawPane;

    @Override
    public void init() {
        super.init();
        setLayout(new BorderLayout());
        drawPane = new DrawPane();
        add(drawPane);
        timer = new Timer(40, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int x = drawPane.getAnimationX();
                int diameter = drawPane.getDiameter();
                x += delta;
                if (x < 0) {
                    x = 0;
                    delta *= -1;
                } else if (x + diameter > getWidth()) {
                    x = getWidth()- diameter;
                    delta *= -1;
                }
                drawPane.setAnimationX(x);
                repaint();
            }
        });
    }

    @Override
    public void start() {
        super.start();
        timer.start();
    }

    @Override
    public void stop() {
        timer.stop();
        super.stop();
    }

    public class DrawPane extends JPanel {

        int x = 100;
        int y = 100;
        int diameter = 50;

        public void setAnimationX(int x) {
            this.x = x;
        }

        public void setAnimationY(int y) {
            this.y = y;
        }

        public int getAnimationX() {
            return x;
        }

        public int getAnimationY() {
            return y;
        }

        public int getDiameter() {
            return diameter;
        }

        public void setDiameter(int startDiameter) {
            diameter = startDiameter;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawOval(x, y, diameter, diameter);
        }
    }
}

另外,在覆盖方法时要小心,getXandgetY方法有非常特殊的含义,你可以通过覆盖它们来削弱你的应用程序......

我还质疑使用的必要性JApplet,最好从使用 a 开始,JFrame这样更容易正常工作。

查看Swing 中的并发以及如何使用 Swing 计时器了解更多详细信息

于 2013-10-29T02:38:52.680 回答