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 8 of 8

Thread: Keyboard Listeners and ImagePanel

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Keyboard Listeners and ImagePanel

    Hey. I am working on a little programming in my free time. I like coding, but don't have much experience working with Java. I want to be able to move an image around a frame using keyboard buttons. I have two sets of code. In the first, I display a panel with an image for the background that adjusts size with the window. I also display a second image fixed in the window. This seems to work fine:

    import java.awt.Graphics;
    import java.awt.Image;
     
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
     
    public class FrameTest3 extends JFrame{
    	private static int xAxis = 10;
    	private static int yAxis = 30;
    	public static int getxAxis() {
    		return xAxis;
    	}
     
    	public static void setxAxis(int xAxis) {
    		FrameTest3.xAxis = xAxis;
    	}
     
    	public static int getyAxis() {
    		return yAxis;
    	}
     
    	public static void setyAxis(int yAxis) {
    		FrameTest3.yAxis = yAxis;
    	}
     
    	public FrameTest3(){
    		add(new ImagePanel());
    	}
     
    	public static void main(String[] args){
     
     
    		JFrame frame = new FrameTest3();
     
     
    		frame.setSize(400, 300);
    		frame.setLocationRelativeTo(null);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
     
    	static class ImagePanel extends JPanel{
    		ImageIcon imageIcon = new ImageIcon("/Users/zerosdamnation/Documents/Projects/Photoshop/backgroundtest.gif");
    		Image image = imageIcon.getImage();
    		ImageIcon imageIcon2 = new ImageIcon("/Users/zerosdamnation/Documents/Projects/Photoshop/beagle.jpg");
    		Image image2 = imageIcon2.getImage();
     
    		//draw image on the panel
    		public void paintComponent(Graphics g){
    			super.paintComponent(g);
     
     
    			if (image != null)
    				g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
     
    			g.drawImage(image2, xAxis, yAxis, 100, 100, this);
    		}
     
    	}
    }

    Now I have taken the same code, but added a keyboard listener that I am trying to use to adjust the location of the second image in the panel. Somewhere in this code I am messing up and the program is not displaying either image at all. I am not really sure what I am doing wrong, as I am rather inexperienced with this.

    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
     
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
     
    public class FrameTest4 extends JFrame{
    	private KeyboardPanel keyboardPanel = new KeyboardPanel();
    	private static int xAxis = 10;
    	private static int yAxis = 30;
     
    	public static int getxAxis() {
    		return xAxis;
    	}
     
    	public static void setxAxis(int xAxis) {
    		FrameTest4.xAxis = xAxis;
    	}
     
    	public static int getyAxis() {
    		return yAxis;
    	}
     
    	public static void setyAxis(int yAxis) {
    		FrameTest4.yAxis = yAxis;
    	}
     
    	public FrameTest4(){
    		add(new ImagePanel());
    		add(keyboardPanel);
    		keyboardPanel.setFocusable(true);
    	}
     
    	public static void main(String[] args){
     
    		FrameTest4 frame = new FrameTest4();
     
     
    		frame.setTitle("Move me around the screen!");
    		frame.setSize(400, 300);
    		frame.setLocationRelativeTo(null);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
     
    	//inner class, keyboard panel for receiving key input
    	static class KeyboardPanel extends JPanel{
    		private int x = 10;
    		private int y = 30;
     
    		public KeyboardPanel(){
    			addKeyListener(new KeyAdapter(){
    				public void keyPressed(KeyEvent e){
    					switch (e.getKeyCode()){
    					case KeyEvent.VK_DOWN: y = getyAxis(); y += 5; setyAxis(y); break;
    					case KeyEvent.VK_UP: y = getyAxis(); y -= 5; setyAxis(y); break;
    					case KeyEvent.VK_LEFT: x = getxAxis(); x -= 5; setxAxis(x); break;
    					case KeyEvent.VK_RIGHT: x = getxAxis(); x += 5; setxAxis(x); break;
     
    					}
    					repaint();
    				}
    			});
    		}
    	}
     
    	static class ImagePanel extends JPanel{
    		ImageIcon imageIcon = new ImageIcon("/Users/zerosdamnation/Documents/Projects/Photoshop/backgroundtest.gif");
    		Image image = imageIcon.getImage();
    		ImageIcon imageIcon2 = new ImageIcon("/Users/zerosdamnation/Documents/Projects/Photoshop/beagle.jpg");
    		Image image2 = imageIcon2.getImage();
     
    		//draw image on the panel
    		public void paintComponent(Graphics g){
    			super.paintComponent(g);
     
    			if (image != null)
    				g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
     
    			g.drawImage(image2, xAxis, yAxis, 100, 100, this);
    		}
     
    	}
    }

    Any help would be greatly appreciated!
    Last edited by DOLZero; April 7th, 2012 at 02:55 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,138
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Keyboard Listeners and ImagePanel

    Check how you are using the layout manager. When add components to a container, the layout manger controls where the components go. For some layout mangers you need tell it where to put components by a second parameter to the add method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Keyboard Listeners and ImagePanel

    Did I mess something up in this section of code?

    	public FrameTest4(){
    		add(new ImagePanel());
    		add(keyboardPanel);
    		keyboardPanel.setFocusable(true);
    	}

    Should I create a separate frame with the keyboard controls?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,138
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Keyboard Listeners and ImagePanel

    Yes that was the code I was talking about. You need to read up on how to use layout managers.
    A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Keyboard Listeners and ImagePanel

    I wrote in a border layout, then applied the images to the center and the keyboard panel to the south frame. It shows up (with a small gap on the bottom), but the program is not repainting the image unless I try to re-size the window (which invokes the draw image re-size of the background image). The repaint() command in the KeyboardPanel() inner class is not doing the trick. I am probably missing something really obvious, but I cannot figure out what.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,138
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Keyboard Listeners and ImagePanel

    Make sure the JPanel is given a size for the layout manager to use. Try using the set preferred size method.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2012
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Keyboard Listeners and ImagePanel

    Thanks for the input. I haven't had a chance to get back to the program since last weekend. I got things working as far as moving stuff around the screen by putting in a timer and an action listener, and now the program refreshes the window every x seconds properly. The layout is still a little wonky though. I would prefer to have the keyboard panel not be visible for lower 30 pixels of the screen. Can I set the preferred size to 0 pixels to hide it? Could I use another frame that is not set to visible for the keyboard controls, or will that not let me use keyboard controls since the image panel would be the active window? I was thinking about adding in another frame that just didn't show that part of the window (make the image/keyboard frame 430 x 300 pixels, and then put that in a 400 x 300 frame), but there has got to be a better way to do it.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,138
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Keyboard Listeners and ImagePanel

    Try it and see what happens.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Listeners not working!
    By Alex-Green in forum Member Introductions
    Replies: 1
    Last Post: February 19th, 2012, 06:49 PM
  2. Serializing Listeners
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: March 11th, 2011, 04:47 PM
  3. Mouse Listeners
    By newbie in forum AWT / Java Swing
    Replies: 2
    Last Post: November 27th, 2010, 10:08 PM
  4. Action Listeners and Key Listeners Help
    By xctive in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 18th, 2010, 08:27 AM
  5. Listeners, CopyOnWriteArrayList and removals
    By Richard in forum Collections and Generics
    Replies: 2
    Last Post: August 5th, 2010, 02:12 PM