freemefast | 9 Feb 22:27
Picon

Adding images to key events

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. :)


Gmane