1

我正在尝试将 Java KeyListener 合并到我的移动对象中,左/右箭头影响 x 轴 (xSpeed) 坐标,上/下箭头影响 y 轴 (ySpeed)。由于某种原因,我只是无法连接对象和 KeyListener。请帮帮我?谢谢!

    import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;

public class Action
{
    private static final int GRAVITY = 1;
    private int ballDegradation = 8;
    private Ellipse2D.Double circle;
    private Color color;
    private int diameter;
    private int xPosition;
    private int yPosition;
    private final int groundPosition; 
    private final int topPosition;
    private final int leftSidePosition;
    private final int rightSidePosition;
    private Canvas canvas;
    private int ySpeed = -1;  
    private int xSpeed = 8;
    public Action(int xPos, int yPos, int ballDiameter, Color ballColor,
    int groundPos, int topPos, int leftSidePos, int rightSidePos, Canvas drawingCanvas)
    {
        xPosition = xPos;
        yPosition = yPos;
        color = ballColor;
        diameter = ballDiameter;
        groundPosition = groundPos;
        topPosition = topPos;
        leftSidePosition = leftSidePos;
        rightSidePosition = rightSidePos;
        canvas = drawingCanvas;
    }
    public void draw()
    {
        canvas.setForegroundColor(color);
        canvas.fillCircle(xPosition, yPosition, diameter);
    }
    public void erase()
    {
        canvas.eraseCircle(xPosition, yPosition, diameter);
    }    
    public void move()
    {
        erase();
        ySpeed += GRAVITY;
        yPosition += ySpeed;
        xPosition += xSpeed;
        if(yPosition >= (groundPosition - diameter) && ySpeed > 0) 
        {
            yPosition = (int)(groundPosition - diameter);
            ySpeed = -ySpeed + ballDegradation; 
        }
        if(yPosition <= topPosition && ySpeed < 0)
        {
            yPosition = (int)topPosition;
            ySpeed = -ySpeed + ballDegradation;
        }
        if(xPosition <= leftSidePosition && xSpeed <0)
        {
            xPosition = (int)leftSidePosition;
            xSpeed = -xSpeed + ballDegradation;
        }
        if(xPosition >= (rightSidePosition - diameter) && xSpeed > 0) 
        {
            xPosition = (int)(rightSidePosition - diameter);
            xSpeed = -xSpeed + ballDegradation;
        }
        draw();
    }
        public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode();
            switch( keyCode ) { 
        case KeyEvent.VK_UP:
            ySpeed = -ySpeed --;
            break;
        case KeyEvent.VK_DOWN:
            ySpeed = -ySpeed ++;
            break;
        case KeyEvent.VK_LEFT:
            xSpeed = xSpeed --;
            break;
        case KeyEvent.VK_RIGHT :
            xSpeed = xSpeed ++;
            break;
     }
    } 
    }
4

1 回答 1

2
  • 不要为 API、方法或 ei 使用保留的 Java 名称,Action可能是MyAction

  • 不要使用 AWT Canvas(如果你有非常重要的原因,OpenXxx、CAD、CAM...),请改用 JPanel 或 JComponent

  • (没有人知道您的其余代码)不要将 AWT 组件与 Swing JComponent 混合

  • 如果您将使用 JPanel 或 JComponent,则使用KeyBindings 而不是 KeyListener

  • 否则你必须为 Canvas 设置焦点,并且在对焦点进行任何更改之后,你必须将焦点设置为画布,这是 KeyListener 的问题

于 2012-05-28T11:09:51.800 回答