KeyListener方法中的代码无法正常工作

时间:2015-12-07 23:12:39

标签: 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

    }
}

}

我尝试将图形组件中的代码移动到keylistener中以检查问题是否与键监听器有关,但是当我这样做时,keylistener在我按下一个键时画了。所以在这一点上我不确定问题的来源。

1 个答案:

答案 0 :(得分:0)

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

要翻译polygon,您必须更改polygon.xpointspolygon.ypoints数组中的值。

其他一切都应该很好。