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

后端 未结 1 1261
伪装坚强ぢ
伪装坚强ぢ 2021-01-23 07:42

Ok, so what i want is the rectangle to always be moving, but when you press the left and right arrow is changes the direction by either increasing or decreasing the angle. With

相关标签:
1条回答
  • 2021-01-23 08:06

    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

    0 讨论(0)
提交回复
热议问题