Java - 使用KeyPressed更改另一个类中矩形的位置

时间:2017-12-02 17:56:57

标签: java key listener

我正在创建一个破砖游戏,我有一个将显示所有元素的类,这很好。

然后我有一个执行操作的键监听器类。基本上我想访问我在类中绘制的形状,并使用键监听器类更改它的位置。

我用系统测试了它,它确实改变了X的值,但它没有实际改变它(在gui中)。我正在使用setter和getter方法。

认为问题是我在键盘课程中使用shapes[]的唯一方法是创建一个新的,所以它不是使用我的在我的paintComponent中制作,而不是我在我的关键监听器中制作的那个?关于如何访问我的对象并对其应用键盘操作,我有点迷失。

-

public class GameView extends JComponent implements ActionListener {

//additional code is here (not related to issue)


public void paintComponent(Graphics g)
    {



            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < h; j++)
                {
                    g.setColor(Color.darkGray);
                    g.fillRect(1,1,410,410);

                    shapeClass[1].draw(g);

                }
            }
        }

现在在我的键盘监听器类

public class MyKeyListener implements KeyListener, ActionListener{
    Shape[] shapes = new Shape[5];

    @Override
    public void actionPerformed(ActionEvent e) {
        timer.start();
        repaint();
    }

    @Override
    public void keyTyped(KeyEvent e) {}

    @Override
    public void keyReleased(KeyEvent e) {}


    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
            if(shapes[1].getX() >= 400) {
                shapes[1].setX(400);
            }
            else {
                moveRight();
                repaint();

            }

        }

        if(e.getKeyCode() == KeyEvent.VK_LEFT) {
            if(shapes[1].getX() < 0) {
                shapes[1].setX(0);
            }
            else {
                moveLeft();
            }
        }
    }

    public void moveRight() {
        play = true;
        shapes[1].incrementX();
    }

    public void moveLeft() {
        play = true;
        shapes[1].decrementX();
    }


}

2 个答案:

答案 0 :(得分:0)

如果将形状[]设置为public static,则可以从任何类访问它们。所以,做这样的事情。

public class GameView extends JCompnent implements ActionListener {

    public static Shape[] shapes = new Shape[5];

然后,从那里,您可以通过输入以下内容从另一个类访问它们:

GameView.shapes.someShapeMethod();
祝你好运,欢呼!

答案 1 :(得分:0)

您可以通过使用依赖注入来访问Shape[]GameView中的MyKeyListener而无需创建数组public static

Shape[] shapes = new Shape[5];
shapes[1] = new Rectangle(180,390,50,8,0);
shapes[2] = new Circle (199,365,10,0);

GameView view = new GameView(shapes);
MyKeyListener listener = new MyKeyListener(shapes);

你给每个类一个构造函数来接受形状,然后让它们保存对形状的引用:

class GameView {
    private Shape[] shapes;

    public GameView(Shape[] shapes) {
        this.shapes = shapes;
    }

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

        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                g.setColor(Color.darkGray);
                g.fillRect(1,1,410,410);

                shapes[1].draw(g);
                shapes[2].draw(g);
            }
        }
    }
}

class MyKeyListener {
    private Shape[] shapes;

    public MyKeyListener(Shape[] shapes) {
        this.shapes = shapes;
    }

    @Override
    public void keyPressed(KeyEvent e) {
       if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
           if(shapes[1].getX() >= 410) {
               shapes[1].setX(410);
           } else {
               moveRight();
           }
        }
    }
}

请注意我删除了几行代码,例如shapes[1] = new Rectangle(180,390,50,8,0);,每次按下某个键都会重置矩形。

如果您在MyKeyListener中创建了GameView对象,则可以在Shape[]内创建GameView

class GameView {
    private Shape[] shapes;
    private MyKeyListener listener;

    public GameView() {
        shapes[1] = ...;
        shapes[2] = ...;
        listener = new MyKeyListener(shapes);
    }
}

否则,您必须在实例化GameViewMyKeyListener的位置创建它。例如,如果您在main方法中实例化它们:

public static void main(String[] args) {
    Shape[] shapes = new Shape[5];;
    shapes[1] = ...;
    shapes[2] = ...;
    GameView view = new GameView(shapes);
    MyKeyListener listener = new MyKeyListener(shapes);
}