1 pièce(s) jointe(s)
Deplacer rectangle et thread
Bonjour, j'essaye de déplacer mon rectangle d'un point a un autre mais le résultat n'est pas bonne (voir ci-jointPièce jointe 387615). Je sais que je dois utiliser un Thread mais je n'arrive pas.
mon Code:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| int x1 = 10;
int y1 = 60;
int x2 = 230;
int y2 = 400;
double angle = Math.atan2(y2 - y1, x2 - x1);
double px = x1, py = y1;
while(px < x2 && py < y2) {
px += SPEED * Math.cos(angle);
py += SPEED * Math.sin(angle);
imRecIcone = new ImageRectangle(iconeImage, (int)px, (int)py);
Graphics g2 = (Graphics)g;
imRecIcone.draw(g2,this);
repaint();
} |
-----
Constructeur classe ImageRectangle:
Code:
1 2 3 4 5 6
| public ImageRectangle(BufferedImage img, int x, int y) {
this.img=img;
ImageIcon icon = new ImageIcon(img);
this.rect=new Rectangle (x, y, icon.getIconWidth(), icon.getIconHeight());
} |
Merci beaucoup