Moving an image around the screen using the arrow keys.
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 (:
Code Java:
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
}
Re: Moving an image around the screen using the arrow keys.
I believe the problem is because your DrawingPanel isn't listening to key events from the JFrame.
Code Java:
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);
panel.addKeyListener(frame);
frame.setVisible(true);
Re: Moving an image around the screen using the arrow keys.
thanks (: i think you meant to say frame.addKeyListener(panel); though.
Re: Moving an image around the screen using the arrow keys.
Quote:
Originally Posted by
nemo
thanks (: i think you meant to say frame.addKeyListener(panel); though.
Nope. There's no reason to add a key listener to a top level window, and the code you originally posted has the panel add itself as a key listener
Code :
this.addKeyListener(this);
If I'm reading this correctly, the problem is that a JPanel is by default not focusable. So you require, in the constructor, then depending on what other components are involved, you may need a
Code :
panel.requestFocusInWindow();
somewhere.
That said, a KeyListener is a poor substitute for key bindings, which being a higher-level API is more robust. Read about key bindings here:
How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
Key Bindings Java Tips Weblog
db
Re: Moving an image around the screen using the arrow keys.
and yet the switch case statements don't have break; at each case and no default case either...