Java: Moving an object at an angle and changing angle with KeyPress

天涯浪子 提交于 2019-12-02 05:17:46

As started in my comments...

  • Don't start the timer in paintComponent, this method gets called repeatedly and can be called often in quick succession.
  • Use key bindings

.

public class TestAnimation01 {

    public static void main(String[] args) {
        new TestAnimation01();
    }

    public TestAnimation01() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new Fields());
                frame.setSize(400, 400);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class Fields extends JPanel implements ActionListener {

        Timer tm = new Timer(125, this);
        double x = 250, y = 250, vel = 0.2, angle = 90;
        private int velX = 4;
        private int velY = 4;

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            this.setBackground(Color.BLACK);
            g.setColor(Color.GREEN);
            g.fillRect((int) x, (int) y, 5, 5);
        }

        public void actionPerformed(ActionEvent e) {

            x += (velX * (float) Math.cos(Math.toRadians(angle - 90)));
            y += (velX * (float) Math.sin(Math.toRadians(angle - 90)));

            repaint();
        }

        public Fields() {

            setFocusable(true);

            InputMap im = getInputMap(WHEN_FOCUSED);
            ActionMap am = getActionMap();

            // left 37
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "goLeft");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "goRight");

            am.put("goLeft", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    angle--;
                    repaint();
                }
            });
            am.put("goRight", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    angle++;
                    repaint();
                }
            });

            tm.setRepeats(true);
            tm.setCoalesce(true);
            tm.start();

            requestFocusInWindow();

        }
    }
}

There's a bunch of other things you've not covered, such as edge conditions (what happens when it leaves the screen) and individual x/y speeds, but I'm sure you'll work it out

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!