Java中意外的额外动画

Java中意外的额外动画,java,animation,Java,Animation,我试图弄清楚线程和动画,所以我制作了一个非常基本的程序,你可以按按钮上下移动一个矩形。问题是,如果您一直按向上按钮,直到矩形到达屏幕顶部,突然会出现一个额外的蓝色矩形 这个问题有点难以解释,只需运行程序并不断向上按,当你到达顶部时,突然画出了两个矩形。我的油漆部件有问题吗 import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.Graphics; import

我试图弄清楚线程和动画,所以我制作了一个非常基本的程序,你可以按按钮上下移动一个矩形。问题是,如果您一直按向上按钮,直到矩形到达屏幕顶部,突然会出现一个额外的蓝色矩形

这个问题有点难以解释,只需运行程序并不断向上按,当你到达顶部时,突然画出了两个矩形。我的油漆部件有问题吗

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class RectangleMovement implements Runnable {
    JFrame frame;
    MyPanel panel;
    private int x = 100;
    private int y = 100;
    private int playerX = 400;
    private int playerY = 350;
    private int horizontalGoal = 0;
    private int verticalGoal = 0;

    public static void main(String[] args) {

            new RectangleMovement().go();

    }

    private void go() {
            frame = new JFrame("Worst Game Ever");
            panel = new MyPanel();
            MyPanel buttonPanel = new MyPanel();
            JButton left = new JButton("Left");
            JButton right = new JButton("Right");
            JButton down = new JButton("Down");
            JButton up = new JButton("Up");
            left.addActionListener(new LeftButton());
            right.addActionListener(new RightButton());
            down.addActionListener(new DownButton());
            up.addActionListener(new UpButton());
            buttonPanel.setLayout(new FlowLayout());
            buttonPanel.add(left);
            buttonPanel.add(down);
            buttonPanel.add(up);
            buttonPanel.add(right);

            frame.getContentPane().add(BorderLayout.CENTER, panel);
            frame.getContentPane().add(BorderLayout.SOUTH, buttonPanel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setResizable(false);
            frame.setSize(500, 500);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);

            Thread player = new Thread(this);
            player.start();
            animate();
    }

    private class MyPanel extends JPanel {
            /**
             *
             */
            private static final long serialVersionUID = 1L;

            public void paintComponent(Graphics g) {
                    g.setColor(Color.RED);
                    g.fillRect(x, y, 10, 10);
                    g.setColor(Color.BLUE);
                    g.fillRect(playerX, playerY, 10, 10);
            }
    }

    class LeftButton implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                    horizontalGoal = -5;
            }
    }

    class RightButton implements ActionListener {

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

    class UpButton implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                    verticalGoal = -5;
            }
    }

    class DownButton implements ActionListener {

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

    /*
     * Animates the player
     */
    public void run() {
            while (true) {
                    if (horizontalGoal < 0 && playerX > 0) {
                            playerX--;
                            horizontalGoal++;
                    } else if (horizontalGoal > 0 && playerX + 20 < frame.getWidth()) {
                            playerX++;
                            horizontalGoal--;
                    }
                    if (verticalGoal < 0 && playerY > 0) {
                            playerY--;
                            verticalGoal++;
                    } else if (verticalGoal > 0 && playerY + 10 < 400) {
                            playerY++;
                            verticalGoal--;
                    }
                    try {
                            Thread.sleep(15);
                    } catch (InterruptedException e) {
                            e.printStackTrace();
                    }
                    if (intersects()){
                            playerX = 400;
                            playerY = 350;
                    }
                    frame.repaint();
            }
    }

    private void animate() {
            boolean direction = true;
            while (true) {
                    if (y <= 100) {
                            direction = true;
                    } else if (y >= 400) {
                            direction = false;
                    }
                    if (direction) {
                            y++;
                    } else {
                            y--;
                    }
                    try {
                            Thread.sleep(15);
                    } catch (InterruptedException e) {
                            e.printStackTrace();
                    }
                    frame.repaint();
            }
    }

    private boolean intersects() {
            if (x <= playerX && playerX <= x + 10 && y <= playerY
                            && playerY <= y + 10) {
                    return true;
            }
            return false;
    }
}
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.FlowLayout;
导入java.awt.Graphics;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
公共类RectangleMovement实现了Runnable{
JFrame框架;
我的小组;
私人整数x=100;
私人int y=100;
专用整数播放器x=400;
私人智力游戏=350;
私有int水平目标=0;
私有int垂直目标=0;
公共静态void main(字符串[]args){
新矩形运动().go();
}
私人空间{
frame=新JFrame(“有史以来最差的游戏”);
panel=新的MyPanel();
MyPanel buttonPanel=新建MyPanel();
JButton left=新JButton(“左”);
JButton right=新JButton(“right”);
JButton down=新JButton(“down”);
JButton up=新JButton(“up”);
addActionListener(新的LeftButton());
addActionListener(新的RightButton());
addActionListener(新的DownButton());
addActionListener(新的UpButton());
buttonPanel.setLayout(新的FlowLayout());
按钮面板。添加(左);
按钮面板。添加(向下);
按钮面板。添加(向上);
按钮面板。添加(右);
frame.getContentPane().add(BorderLayout.CENTER,panel);
frame.getContentPane().add(BorderLayout.SOUTH,buttonPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setresizeable(false);
框架。设置尺寸(500500);
frame.setLocationRelativeTo(空);
frame.setVisible(true);
线程播放器=新线程(此);
player.start();
制作动画();
}
私有类MyPanel扩展了JPanel{
/**
*
*/
私有静态最终长serialVersionUID=1L;
公共组件(图形g){
g、 setColor(Color.RED);
g、 fillRect(x,y,10,10);
g、 setColor(Color.BLUE);
g、 fillRect(playerX,playerY,10,10);
}
}
类LeftButton实现ActionListener{
@凌驾
已执行的公共无效操作(操作事件e){
水平目标=-5;
}
}
类RightButton实现ActionListener{
@凌驾
已执行的公共无效操作(操作事件e){
水平目标=5;
}
}
类UpButton实现ActionListener{
@凌驾
已执行的公共无效操作(操作事件e){
垂直目标=-5;
}
}
类DownButton实现ActionListener{
@凌驾
已执行的公共无效操作(操作事件e){
垂直目标=5;
}
}
/*
*为播放器设置动画
*/
公开募捐{
while(true){
如果(水平目标<0&&playerX>0){
playerX--;
水平目标++;
}否则如果(horizontalGoal>0&&playerX+200){
游戏——;
垂直目标++;
}否则如果(垂直目标>0&&playerY+10<400){
playerY++;
垂直目标--;
}
试一试{
睡眠(15);
}捕捉(中断异常e){
e、 printStackTrace();
}
if(相交()){
playerX=400;
playerY=350;
}
frame.repaint();
}
}
私有void动画(){
布尔方向=真;
while(true){
如果(y=400){
方向=假;
}
如果(指示){
y++;
}否则{
y--;
}
试一试{
睡眠(15);
}捕捉(中断异常e){
e、 printStackTrace();
}
frame.repaint();
}
}
私有布尔相交(){

如果(x你的buttonPanel是问题所在。它也是一个MyPanel,使用你自己的paintComponent方法。因此,如果你的玩家应该被绘制playerY=10。它被绘制在你的游戏面板和你的buttonPanel上。 把你的钮扣从

 MyPanel buttonPanel = new MyPanel();


而你的问题消失了:-)

请不要链接到pastebin。问题向导中有代码格式化工具,因此请使用它并将所有相关代码包含在问题本身中,至少有两个原因:1.将来链接可能会消失,因此问题将不完整;2.在我的特定情况下,我在阻止pastebin的代理后面,因此我会
Panel buttonPanel = new Panel();