Problème de rafraichissement d'un dessin
Bonjour à tous,
je développe actuellement un petit logiciel ou j'ai des objets runnable qui ont une position définie dans leur classe.
Ces objets sont dessinés dans un JPanel en récupérant le graphics.
Dans ma méthode run de l'objet, j'efface le dernier point de l'objet avant de dessiner le nouveau point.
Seul hic, des fois le précédent point est mal effacé et reste malgré le rafraichissement.
Honnetement, je vois pas trop d'où cela peut venir: une idée?
Pour illustrer cela, voici le bout de code de ma méthode run() de ma classe:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| public void run() {
while(true){
//Beginning of the process
//First part of Display Management
Graphics g=this.getJp().getGraphics();
Color c = g.getColor();
g.drawOval(this.getX(),this.getY(),1,1);
//Move management
int dx,dy;
dx=(int) (Math.round((this.getSpeed()*Math.random()-this.getSpeed()*Math.random())));
dy=(int) (Math.round((this.getSpeed()*Math.random()-this.getSpeed()*Math.random())));
this.setX(this.getX()+dx);
this.setY(this.getY()+dy);
System.out.println("Id: "+this.getId()+" Ship: "+this.getName()+" X: "+this.getX()+" Y: "+this.getY());
//Second part of Display Management
g.setColor(Color.red);
g.drawOval(this.getX(),this.getY(),1,1);
g.setColor(c);
//End of the process
try {
//Wait 1 second
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} |
Merci pour votre aide.