Results 1 to 7 of 7
- 11-04-2011, 03:52 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 15
- Rep Power
- 0
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.
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?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) { } } } } }
Thanks for the time and help!
- 11-04-2011, 04:16 PM #2
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?
- 11-04-2011, 04:49 PM #3
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?
- 11-05-2011, 08:43 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 15
- Rep Power
- 0
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.
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!
- 11-05-2011, 08:47 PM #5
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)
- 11-05-2011, 09:20 PM #6
Re: Changing images in a image array with keypresses
Better still, learn about and use key bindings instead.
How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
-
Re: Changing images in a image array with keypresses
Similar Threads
-
changing image of a label
By Coold0wn in forum New To JavaReplies: 4Last Post: 09-06-2011, 06:46 AM -
My Run Method Does Not Work Witn Changing JLabel Images From The Other Class?
By Alerhau in forum New To JavaReplies: 12Last Post: 08-16-2011, 10:25 AM -
Changing Image color
By Frecow in forum Java 2DReplies: 0Last Post: 04-04-2011, 10:16 AM -
changing JButton image upon clicking
By Logical in forum AWT / SwingReplies: 2Last Post: 12-21-2010, 02:53 AM -
Changing images by clicking arrow buttons. help?
By ashton in forum New To JavaReplies: 3Last Post: 02-08-2009, 11:29 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks