Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Image doesnt refrsh properly

Ive programmed an application that moves an Image when the right arrow or left arrow is pressed,But when this is done the image moves and leavs a ghost of it behind it.So when i am playing the app and press the right arrow it moves and leaves a bit of the image behind it,can someone help me fix this problem.

import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import java.net.*;

public class main extends JFrame implements KeyListener {
String path1 = "move.png";
String path2 = "x.gif";

ImageIcon idle = new ImageIcon(getClass().getResource(path2)) ;
ImageIcon move = new ImageIcon(path1) ;
ImageIcon bullet = new ImageIcon("b.png") ;
//Icon bug = new ImageIcon( getClass().getResource( "bug1.gif" ) );

//postion of ship
int x= 50;
int y = 50;

//default size of ship
int bull = 0;//0 if bullet is not being fierd 1 if it is

int im= 1; //0 means ship is moving 1 means ship is idle
public main(){
super("Astroids");


//Window Properites
setVisible(true);
setSize(500,500);
addKeyListener(this);
}

public void keyReleased(KeyEvent e) {

if(e.getKeyCode() == 39){

im = 1;

}

if(e.getKeyCode() == 37){
System.out.println("left");


im = 1;

}
if(e.getKeyCode() == 32){
System.out.println("bang");
bull = 1;//fire

}
}

public void keyPressed(KeyEvent e) {
System.out.println(e.getKeyCode());

if(e.getKeyCode() == 39){

right();
}

if(e.getKeyCode() == 37){
left();
}
}

public void right(){
System.out.println("right");

x = x + 10;
im = 0;
update();
}

public void left(){
System.out.println("left");

x = x - 10;
im = 0;
update();
}



public void keyTyped(KeyEvent e) {

}


public void update() {
try {
Thread.sleep(20); // sleep a bit
}
catch(InterruptedException ex){}

repaint();

}


public void paint(Graphics g){
super.paint(g);





g.drawImage(idle.getImage(),x,y,this);

}


public static void main(String[] args){

main app = new main();
}
}
[2624 byte] By [dojo] at [2007-11-11 9:59:48]