9 Feb 22:27
Adding images to key events
freemefast <jellistotm1 <at> tiscali.co.uk>
2010-02-09 21:27:35 GMT
2010-02-09 21:27:35 GMT
Hi all hope you can help,
Im using a jframe form and my program is a mars lander that moves left, right up and down with the keyboard arrows.
I am using a jLabel for the lander image but want to know how I can have it so that the image icon is swapped when i
press an arrow. i.e. I want an image of my spaceship to display thrusters to the left and right when i press
the arrow keys respectively.
Here is my code for the movement, it works just fine;
private void jPanel1KeyPressed(java.awt.event.KeyEvent evt) {
System.out.println(evt.getKeyCode());
switch (evt.getKeyCode()){
case ARROW_RIGHT:
x++;
break;
case ARROW_LEFT:
x--;
break;
case ARROW_UP:
y--;
break;
case ARROW_DOWN:
y++;
break;
}
I thought maybe using the image url after the (x--) statement would work but cant find help on the syntax. Any ideas?
Thanks in advance. :)
RSS Feed