具有移动对象的Java KeyListener
我试图将Java KeyListener与我的移动对象合并,左/右箭头影响x轴(xSpeed)坐标,上/下箭头影响y轴(ySpeed)。由于某种原因,我无法连接对象和KeyListener。请帮帮我好吗?谢谢具有移动对象的Java KeyListener,java,swing,awt,keylistener,Java,Swing,Awt,Keylistener,我试图将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; priv
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;
}
}
}
import java.awt.*;
导入java.awt.geom.*;
导入java.awt.event.*;
公共集体诉讼
{
私有静态最终整数重力=1;
私人int=8;
私人椭圆2d。双圈;
私人色彩;
私有内径;
私密位置;
私人内部职位;
私人最终职位;
私人最终积分排名;
私人最终int左侧位置;
私人最终int右侧位置;
私人帆布;
私有int Y速度=-1;
私有int xSpeed=8;
公共行动(int XPO、int YPO、int BALLDIAME、Color ballColor、,
int groundPos、int topPos、int leftSidePos、int righsidepos、Canvas drawing Canvas)
{
xPosition=xPos;
yPosition=yPos;
颜色=球颜色;
直径=球直径;
地面位置=地面位置;
topPosition=topPos;
leftSidePosition=leftSidePos;
rightSidePosition=rightSidePos;
画布=绘制画布;
}
公众抽签()
{
canvas.setForegroundColor(颜色);
画布。填充圆(X位置、Y位置、直径);
}
公开作废删除()
{
画布。擦除圆(X位置、Y位置、直径);
}
公开作废动议()
{
擦除();
y速度+=重力;
Y位置+=Y速度;
xPosition+=xSpeed;
如果(Y位置>=(地面位置-直径)和Y速度>0)
{
yPosition=(int)(地面位置-直径);
ySpeed=-ySpeed+BallDegration;
}
如果(位置
- 不要为API、方法或e.i.使用保留的Java名称,
Action
可以是MyAction
- 不要使用AWT画布(如果你有非常重要的原因,比如OpenXxx、CAD、CAM…),请使用JPanel或JComponent
- (没有人知道代码的其余部分)不要将AWT组件与Swing JComponent混合使用
- 在使用JPanel或JComponent的情况下,请使用而不是KeyListener
- 否则,您必须为画布设置Focusable,在对焦点进行任何更改后,您必须将焦点设置回画布,这是KeyListener的问题