I wanted to move my image using the keyboard arrow keys. when i press the arrow keys, it moves according to the direction. However, I need to click on the image before I can transfer it. Can I learn how to edit the code so that I donβt have to click the image before it can be moved? I would also like to know how to make the image appear on the left when it reaches the right and vice versa.
My codes are:
Collect.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent ke) { if(ke.getKeyCode() == KeyEvent.VK_LEFT) { Collect.setLocation(Collect.getX()-8,Collect.getY()); repaint(); } if(ke.getKeyCode() == KeyEvent.VK_RIGHT) { Collect.setLocation(Collect.getX()+8,Collect.getY()); repaint(); } } }); Collect.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent me) { if(me.getClickCount() == 1) { boolean dd = Collect.isOptimizedDrawingEnabled(); boolean ff = Collect.requestFocusInWindow(); repaint(); } } });
source share