我正在试图找出线程和动画,所以我做了这个非常基本的程序,你可以按下按钮上下移动一个矩形。问题是如果你一直按下UP按钮直到矩形到达屏幕顶部,突然会出现一个额外的蓝色矩形。
这个问题有点难以解释只需运行程序并继续按UP,当你到达顶部时突然出现2个矩形。这是我的油漆组件的问题吗?
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;
}
}
答案 0 :(得分:1)
你的buttonPanel就是问题所在。它也是一个使用您自己的paintComponent方法的MyPanel。因此,如果您的玩家应该被绘制,则玩家Y = 10.它会在您的游戏面板和您的buttonPanel上绘制。 从
更改ButtonPanel MyPanel buttonPanel = new MyPanel();
到
Panel buttonPanel = new Panel();
你的问题消失了: - )