Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Moving an image around the screen using the arrow keys.

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    14
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default 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 (:

    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
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

    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);

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    nemo (October 17th, 2010)

  4. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    14
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default Re: Moving an image around the screen using the arrow keys.

    thanks (: i think you meant to say frame.addKeyListener(panel); though.

  5. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    491
    Thanks
    8
    Thanked 47 Times in 45 Posts

    Default Re: Moving an image around the screen using the arrow keys.

    Quote Originally Posted by nemo View Post
    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
    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,
    setFocusable(true);
    then depending on what other components are involved, you may need a
    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

  6. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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...

Similar Threads

  1. List of shortcuts key for Eclipse
    By Flash in forum Java JDK & IDE Tutorials
    Replies: 3
    Last Post: July 18th, 2013, 08:40 AM
  2. video game problem - delay in response to arrow key presses
    By gib65 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 17th, 2010, 07:39 PM
  3. Problem in rotating and moving image at the same time
    By SlimShady in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 17th, 2010, 02:33 PM
  4. Eclipse: How to use/edit shorcut keys.
    By Truffy in forum Java JDK & IDE Tutorials
    Replies: 3
    Last Post: December 3rd, 2009, 11:51 AM
  5. combining keys
    By subhvi in forum Java SE APIs
    Replies: 10
    Last Post: August 29th, 2009, 04:35 PM