Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ol> <li>You should override <code>paintComponent</code> in your <code>JPanel</code> and call <code>super.paintComponent(g)</code> in it.</li> <li>See <a href="http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html" rel="nofollow noreferrer">How to Use Key Bindings tutorial</a>. Key bindings are preffered in this case rather than a <code>KeyListener</code></li> <li><code>pack()</code> <em>then</em> <code>setVisible()</code></li> <li>You should set global variables for x anf y location, so they can be accessed from within your <code>Action</code>. Then in your actions, increment your x or y and repaint</li> </ol> <p>Try running this example</p> <pre><code>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(); } }); } } </code></pre> <p><img src="https://i.stack.imgur.com/6WmIj.gif" alt="enter image description here"></p> <p>Here's the code you're more concerned with</p> <pre><code> 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); </code></pre> <p>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.</p> <hr> <p><strong>See</strong> <a href="http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html" rel="nofollow noreferrer">Key binding tutorial</a> | <a href="http://docs.oracle.com/javase/tutorial/2d/" rel="nofollow noreferrer">Graphics tutorial</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload