the code compiles, the GUI frame appears, and the image is drawn on the panel in the upper left corner. but when i press the arrow keys, he doesn't move. i've never used events before so my problem probably lies there... thanks for your help (:
import java.awt.*; import java.awt.image.BufferedImage; import java.awt.event.*; import java.awt.Graphics; import javax.swing.*; import javax.imageio.ImageIO; import java.io.File; public class KeyboardNess { public static void main(String[] args) throws java.io.IOException { BufferedImage megaman = ImageIO.read(new File("C:\\Documents and Settings\\Nemo\\My Documents\\My Pictures\\MegaMan.jpg")); JFrame frame = new JFrame("can has megaman?"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300,300); Container pane = frame.getContentPane(); DrawingPanel panel = new DrawingPanel(megaman); pane.add(panel); frame.setVisible(true); } } class DrawingPanel extends JPanel implements KeyListener { static BufferedImage image; static int x = 0; static int y = 0; public DrawingPanel(BufferedImage img) { image = img; this.addKeyListener(this); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.drawImage(image,null,x,y); } public void keyPressed(KeyEvent ke) { switch (ke.getKeyCode()) { case KeyEvent.VK_RIGHT: x++;System.out.println("Hi"); //debugging output statement case KeyEvent.VK_LEFT: x--; case KeyEvent.VK_DOWN: y--; case KeyEvent.VK_UP: y++; } this.repaint(); } public void keyReleased(KeyEvent ke){} // keylistener public void keyTyped(KeyEvent ke){} // is abstract }