Results 1 to 7 of 7
  1. #1
    Nonoon is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Changing images in a image array with keypresses

    Hello

    I have been trying to change the images within a image array with the up/down arrow keypresses. Despite pressing the keys the images do not change.

    Java Code:
    import java.applet.Applet;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.image.BufferedImage;
    import java.net.URL;
    
    
    public class Main extends Applet implements Runnable, KeyListener{
    
    
    	 Image[] MenuImages = new Image[4];
    	 int frame = 0;
    	 boolean [] keys = new boolean[500];
    	 boolean running = false;
    
    
    	public void init () {
    		{
    			System.out.println("Applet::init() called"); 
    			try {
    				MenuImages[0] = loadImage("res/MainMenu.png");
    				MenuImages[1] = loadImage("res/ImageOne.png");
    				MenuImages[2] = loadImage("res/ImageTwo.png");
    				MenuImages[3] = loadImage("res/ImageThree.png");
    			} catch(Exception ex) {
    			}
    			
    		}
    	}
    	
    	public void start() 
    	{
    		System.out.println("Applet::start() called");
    		Thread th = new Thread(this); 
    		addKeyListener(this);
    	    running= true;
    		th.start();
    	}
    
    	private Image loadImage( String filename )
    	{
    		Image img = null;
    		URL url = this.getClass().getResource(filename);
    		System.out.println(url.toString());
    		img = Toolkit.getDefaultToolkit().getImage(url);
    		return img;
    	}
    	
        public void paint(Graphics g) {
            g.drawImage(MenuImages[frame], 0, 0, this);
        }
    	
    	private void gameUpdate()
    	{
    	if(keys[KeyEvent.VK_UP]){
    	frame++;
    	System.out.println("Applet::keypress() called");
    		
    		//keys[KeyEvent.VK_UP] = false;
    	}
    	if(keys[KeyEvent.VK_DOWN]){
    		frame--;
            System.out.println("Applet::keypress() called");
    		
    			
    	//	keys[KeyEvent.VK_DOWN] = false;
    	}
    	
    	}
    
    	@Override
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void keyPressed(KeyEvent e) {
    		// TODO Auto-generated method stub
    		keys[e.getKeyCode()] = true;	
    	}
    
    	@Override
    	public void keyReleased(KeyEvent e) {
    		// TODO Auto-generated method stub
    		keys[e.getKeyCode()] = false;			
    	}
    
    	@Override
    	public void run() {
    		// TODO Auto-generated method stub
    		{
    
    			System.out.println("Applet::run() called");
    			while(running)
    			{
    				gameUpdate();
    				repaint();  
    
    				try
    				{
    					Thread.sleep(40);
    				}
    				catch(InterruptedException ex)
    				{
    
    				}
    			}
    		}
    		
    	}
    
    }
    The few System.out.println lines added to the keypresses do not come up at all even when the keys are pressed. Does anyone have any idea where I made a mistake in the code?

    Thanks for the time and help!

  2. #2
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default Re: Changing images in a image array with keypresses

    It seems to work fine for me, printlns show up and the images change. How are you running the applet?

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,558
    Rep Power
    23

    Default Re: Changing images in a image array with keypresses

    Sounds like your component with the key listener does not have the focus.
    Have you tried clicking on the applet before pressing the keys?

  4. #4
    Nonoon is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Re: Changing images in a image array with keypresses

    Sorry for the wait for this reply! After reading Shoss's reply I made a new project and just copy and pasted everything into it. Somehow started working then. I was running it the same before, as a java applet.

    Quote Originally Posted by Norm View Post
    Sounds like your component with the key listener does not have the focus.
    Have you tried clicking on the applet before pressing the keys?
    This does happen now. Is it to be expected? Looking through tutorials they seem to just have it the same way as I do.

    Thanks for the replies and help!

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,558
    Rep Power
    23

    Default Re: Changing images in a image array with keypresses

    There are methods in the Component class you can call to request the focus.
    Also you might read in the tutorial:
    How to Use the Focus Subsystem (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Changing images in a image array with keypresses

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Changing images in a image array with keypresses

    Quote Originally Posted by DarrylBurke View Post
    I'm a big fan of Key Bindings as you know, but I thought that it was Swing specific. Will it work for an AWT program? Thanks in advance.

    /Pete

Similar Threads

  1. changing image of a label
    By Coold0wn in forum New To Java
    Replies: 4
    Last Post: 09-06-2011, 06:46 AM
  2. Replies: 12
    Last Post: 08-16-2011, 10:25 AM
  3. Changing Image color
    By Frecow in forum Java 2D
    Replies: 0
    Last Post: 04-04-2011, 10:16 AM
  4. changing JButton image upon clicking
    By Logical in forum AWT / Swing
    Replies: 2
    Last Post: 12-21-2010, 02:53 AM
  5. Changing images by clicking arrow buttons. help?
    By ashton in forum New To Java
    Replies: 3
    Last Post: 02-08-2009, 11:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •