Bonjour tlm,
j'ai un petit soucis, je dois représenter une balle qui avance suivant l'équation d'une trajectoire. Je voudrais donc dessiner ma courbe au fur et a mesure. Mon problème, c'est que je ne sait pas trop manipuler les threads et j'aimerais donc inserer ceci
try { Thread.sleep(100); }
catch (InterruptedException e) {} Voici le code de ma fonction et la position ou je compte l'inserer. Mais apperement ma boucle tourne après dans le vide rien ne s'affiche.
Merci beaucoup ...
import java.awt.*;
import javax.swing.*;
public class DessinCosOptimal extends JPanel {
// la fonction à tracer (ici, cos(x^2))
public static double f(double x) {
return Math.cos(x*x);
}
//chgmt de repère
public static final double X_MIN=0;
public static final double X_MAX=Math.PI;
public static final double Y_MIN=-1.0;
public static final double Y_MAX=1.0;
public double fromScreenX(int X) {
return X_MIN+(X_MAX-X_MIN)*X/(getWidth()-1);
}
public int toScreenY(double y) {
return (int )Math.round((getHeight()-1)*(y-Y_MAX)/(Y_MIN-Y_MAX));
}
public void paintComponent(Graphics g) {
super .paintComponent(g);
double f=f(X_MIN);
for (int i=0;i<getWidth();i++) {
double fi=f(fromScreenX(i+1));
g.drawLine(i,toScreenY(f),i+1,toScreenY(fi));
f=fi;
try { Thread.sleep(100); }
catch (InterruptedException e) {} }
}
public static void main(String[] args) {
JFrame fenêtre=new JFrame();
fenêtre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel dessin=new DessinCosOptimal();
dessin.setPreferredSize(new Dimension(250,200));
fenêtre.getContentPane().add(dessin,BorderLayout.CENTER);
fenêtre.pack();
fenêtre.setVisible(true );
}
}