java - KeyListener 方法内的代码不起作用

标签 java rotation keylistener

我试图通过按下按键时更改变量来旋转绘制的对象。当我按下按钮时什么也没有发生。我认为可能出了问题。

我还有一个主类,我在其中创建 JPanel 的实例。

public class Move extends JPanel implements KeyListener{
    public Move() {
        addKeyListener(this);//adds the interface to the class to be used
        setFocusable(true);//sets focus, allowing the use of keylistener methods
    }
    private int c, d;
    private int a = 60;//left corner of whatever
    private int b = 60;
    private Point point1 = new Point(a+50,b+10);
    private Point point2 = new Point(a+50+15,b+10+20);
    private Point point3 = new Point(a+50-15,b+10+20);
    private Point point4 = new Point(a+50+15,b+10-20);
    private Point point5 = new Point(a+50-15,b+10-20);
    private Point point6 = new Point(a+25,b+10);
    private Point point7 = new Point(a-25,b);
    private Point point8 = new Point(a-25,b+20);
    private int x1[] = {(int) point1.getX(),(int) point2.getX(), (int) point3.getX()};
    private int y1[] = {(int) point1.getY(), (int) point2.getY(), (int) point3.getY()};
    private int n = 3;
    private Polygon p1 = new Polygon(x1,y1,n);
    private int x2[] = {(int) point1.getX(),(int) point4.getX(), (int) point5.getX()};
    private int y2[] = {(int) point1.getY(),(int) point4.getY(), (int) point5.getY()};
    private Polygon p2 = new Polygon(x2,y2,n);
    private int x3[] = {(int) point6.getX(),(int) point7.getX(), (int) point8.getX()};
    private int y3[] = {(int) point6.getY(),(int) point7.getY(), (int) point8.getY()};
    private Polygon p3 = new Polygon(x3,y3,n);

    public void paintComponent(Graphics g){
        super.paintComponent(g);

        g.drawOval(a, b, 100, 20);
        g.drawPolygon(p1);
        g.drawPolygon(p2);
        g.drawPolygon(p3);
        g.fillPolygon(p1);
        g.fillPolygon(p2);
        g.fillPolygon(p3);
        g.fillOval(a, b, 100, 20);
    }
    @Override
    public void keyPressed(KeyEvent e) {
        int c = e.getKeyCode();
            if(c==KeyEvent.VK_RIGHT){
                double x1 = point1.getX() - 160;
                double y1 = point1.getY() - 80;

                //APPLY ROTATION
                double temp_x1 = x1 * Math.sin(1.57);
                double temp_y1 = y1 * Math.cos(1.57);

                //TRANSLATE BACK
                point1.setLocation(temp_x1 + 160, temp_y1 + 80);
            }
            else if(c==KeyEvent.VK_LEFT){
                point1.setLocation(point1.getX()+100, point1.getY());
                screen.repaint();
            }
            else if(c==KeyEvent.VK_UP){

            }
            else if(c==KeyEvent.VK_DOWN){

            }
    }
    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }
    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }
}

}

我尝试将图形组件中的代码移动到按键监听器中,以检查问题是否出在按键监听器上,但是当我这样做时,按键监听器在我按下某个键时绘制。所以此时我不确定问题出在哪里。

最佳答案

作为多边形点的坐标的 Point 实例与多边形类中的实际坐标无关,因为您将点的坐标放入数组中并用它们初始化多边形,但当您更改点坐标时,数组中的数字值不会更改。

要平移多边形,您必须更改polygon.xpointspolygon.ypoints 数组中的值。

其他一切都应该工作得很好。

关于java - KeyListener 方法内的代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34145117/

相关文章:

CSS : Rotate an element without rotating its inner content - Single Element

Java Applet 从另一个类重绘

java - 如何计算在 JSP 中对主机执行 ping 操作的次数?

java - request.getParameter() 不存在时表示其为 null

c - OpenGL/GLUT : glTranslatef and glRotatef before drawing cube, 或之后?

java - 我的计时器的逻辑错误在哪里,该计时器将镜头动画移动到帧的顶部?

Java KeyListener 没有在 JSpinner 上触发

c# - 如何从 C# 转换为 Java JDBC

java - 无法更改进度条的可见性

html - 将IE7旋转90度