How do I make a rectangle move across the screen with key bindings?

后端 未结 1 502
面向向阳花
面向向阳花 2020-12-02 01:17

The game I\'m trying to create is snake and so far I\'ve figured out how to use paint(Graphics g) a bit of JPanel, mouse listener and now I\'m tryi

相关标签:
1条回答
  • 2020-12-02 01:52
    1. You should override paintComponent in your JPanel and call super.paintComponent(g) in it.
    2. See How to Use Key Bindings tutorial. Key bindings are preffered in this case rather than a KeyListener
    3. pack() then setVisible()
    4. You should set global variables for x anf y location, so they can be accessed from within your Action. Then in your actions, increment your x or y and repaint

    Try running this example

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class KeyBidings extends JFrame {
        int x = 0;
        int y = 0;
    
        DrawPanel drawPanel = new DrawPanel();
    
        public KeyBidings(){
            Action rightAction = new AbstractAction(){
                public void actionPerformed(ActionEvent e) {
                    x +=10;
                    drawPanel.repaint();
                }
            };
    
                InputMap inputMap = drawPanel.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
                ActionMap actionMap = drawPanel.getActionMap();
    
            inputMap.put(KeyStroke.getKeyStroke("RIGHT"), "rightAction");
            actionMap.put("rightAction", rightAction);
    
            add(drawPanel);
    
            pack();
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            setVisible(true);
        }
    
        private class DrawPanel extends JPanel {
    
    
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.GRAY);
                        g.fillRect(0, 0, getWidth(), getHeight());
                g.setColor(Color.GREEN);
                g.fillRect(x, y, 50, 50);
            }
    
            public Dimension getPreferredSize() {
                return new Dimension(400, 200);
            }
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable(){
                public void run(){
                    new KeyBidings();
                }
            });
        }
    }
    

    enter image description here

    Here's the code you're more concerned with

        Action rightAction = new AbstractAction(){
            public void actionPerformed(ActionEvent e) {
                x +=10;
                drawPanel.repaint();
            }
        };
    
        InputMap inputMap = drawPanel.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
        ActionMap actionMap = drawPanel.getActionMap();
    
        inputMap.put(KeyStroke.getKeyStroke("RIGHT"), "rightAction");
        actionMap.put("rightAction", rightAction);
    

    Create a custom action and add that action to the action map, linked to the input map keystroke. In the action, just increment or, decrement the x and/or y, depending on the direction, then repaint the panel.


    See Key binding tutorial | Graphics tutorial

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