2012-05-28 30 views
1

我试图将Java KeyListener与左/右箭头影响x轴(xSpeed)坐标和影响y轴的上/下箭头(ySpeed)结合到我的移动对象中。 。我只是无法连接对象和KeyListener出于某种原因。请帮帮我?谢谢!带有移动对象的Java 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; 
    } 
    } 
    } 

回答

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

  • 不使用AWT画布(磺酰基如果你已经得到了真正重要的原因,OpenXxx ,CAD,CAM ......),使用的JPanel或者JComponent的替代

  • (没人知道你的代码的其余部分)不与Swing混合AWT组件的JComponent

  • 中,您将使用的JPanel或者JComponent的情况下,然后使用KeyBindings而不是KeyListener的

  • 否则,你setFocusable为Canvas和有关分众必须将焦点设置到画布后面的任何更改后,这是KeyListener的问题

+1

[''LinePanel'](http://stackoverflow.com/a/5797965/230513)就是一个例子。 – trashgod