我是Java编程的新手,我正在开发我的第一个2D游戏。我按照Oracle关键监听器的一个教程来了解如何使用WASD键移动我的播放器。该示例已经有一个鼠标监听器。我决定删除鼠标监听器并添加KeyListener。我为D添加了一个KeyListener,它将我的图形向右移动。但是,我的图形只向右移动1个坐标而不是更新正方形的新位置,并且每当按下D键时将其移动到连续循环中。我正在使用绘画组件来绘制我的图像。
这是我的班级:
import javax.swing.SwingUtilities;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseMotionAdapter;
public class PaintComponent {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
System.out.println("Created GUI on EDT? "
+ SwingUtilities.isEventDispatchThread());
JFrame f = new JFrame("Swing Paint Demo");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new MyPanel());
f.pack();
f.setVisible(true);
}
}
class MyPanel extends JPanel {
private int squareX = 50;
private int squareY = 50;
private int squareW = 20;
private int squareH = 20;
public int posX = squareX;
public int posY = squareY;
public MyPanel() {
setFocusable(true);
requestFocusInWindow();
setBorder(BorderFactory.createLineBorder(Color.black));
addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_D) {
moveSquareRight(posX);
} else {
e.consume();
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
int keyCode = e.getKeyCode();
e.consume();
}
});
/*
* addMouseListener(new MouseAdapter() { public void
* mousePressed(MouseEvent e) { moveSquare(e.getX(),e.getY()); } });
*
* addMouseMotionListener(new MouseAdapter() { public void
* mouseDragged(MouseEvent e) { moveSquare(e.getX(),e.getY()); } });
*/
}
private void moveSquareRight(int x) {
int OFFSET = 1;
if (squareX == x) {
repaint(squareX, squareY, squareW + OFFSET, squareH + OFFSET);
squareX = x + 1;
repaint(squareX, squareY, squareW + OFFSET, squareH + OFFSET);
}
}
public Dimension getPreferredSize() {
return new Dimension(250, 200);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("This is my custom Panel!", 10, 20);
g.setColor(Color.RED);
g.fillRect(squareX, squareY, squareW, squareH);
g.setColor(Color.BLACK);
g.drawRect(squareX, squareY, squareW, squareH);
}
}
答案 0 :(得分:4)
我正在考虑将posX转换为数组并为其添加for循环,
不,你不需要循环。
moveSquareRight(posX);
没有必要将posX作为参数传递,因为它是每个方法都可以访问它的类变量。
虽然posX没有改变,因此它保持在同一位置。
所以更新posX。
if (squareX == x) {
repaint(squareX, squareY, squareW + OFFSET, squareH + OFFSET);
squareX = x + 1;
repaint(squareX, squareY, squareW + OFFSET, squareH + OFFSET);
}
您正在使上述代码过于复杂。尝试类似:
posX++;
repaint();
您不希望指定要重绘的矩形(),因为在将矩形绘制到新位置之前,不会清除旧绘画。
您还应该查看Motion Using the Keyboard以了解移动组件的方法。