Bonjour,


Je souhaite faire des animations en utilisant la classe Graphics dans un JScrollPane.

Comment pourrais-je modifier le code suivant pour utiliser l'animation dans un JScrollPane ?

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
 
import java.awt.*; 
import java.awt.image.*;
import java.util.Random;
 
public class DoubleBufferingSoftware extends Frame{
 
   // boucle d'affichage	
   RenderingThread renderingThread = new RenderingThread();
 
   // buffer mémoire (2eme buffer)
   Graphics buffer;
 
   // image mémoire correspondante au buffer   
   Image image; 
 
   int x = 0; // coordonnée x de l'affichage du texte
 
   int x1,y1,x2,y2; // coordonnée x de l'affichage de la ligne
 
 
   public DoubleBufferingSoftware(){
      //affichage
      setSize( 400, 400 );
      setVisible( true );
 
      Random rand = new Random(System.currentTimeMillis()); // constructeur
	  x1 = rand.nextInt(10); // génération
      y1 = rand.nextInt(10); // génération
      x2 = rand.nextInt(10); // génération
      y2 = rand.nextInt(10); // génération
 
 
      // on démarre la bouche d'affichage
      renderingThread.start();
 
   }
 
   public void update(Graphics g){
      paint(g);
   }
 
   public void paint( Graphics g ){
   	  //création du buffer si il n'existe pas
   	  if(buffer==null){
   	     image = createImage(400,400);
         buffer = image.getGraphics();	
   	  }
   	  //on dessine sur le buffer mémoire
      buffer.setColor( Color.white );
      buffer.fillRect( 0, 0, 400, 400 );
      buffer.setColor( Color.black );
      buffer.drawString( "affichage d'une ligne de texte", x, 200 );
      buffer.setColor( Color.red );
      buffer.drawLine(x1,y1,x2,y2);
      x++;
 
 
      if(x>400) x = 0;
      // finalement, le buffer mémoire est dessiné dans le buffer d'affichage
      g.drawImage(image, 0, 0, this);
 
   }
 
 
 
   class RenderingThread extends Thread {
   	  /**
           *  Ce thread appelle le rafraichissement de notre fenêtre 
           *  toutes les 10 milli-secondes
           */
      public void run(){
         while(true){
            try {
               repaint(); 
               sleep( 10 );
            } catch ( Exception e ) {} 
         }
      }
   }   
 
 
 
   public static void main(String[] args) {
   		new DoubleBufferingSoftware();
   }
}
merci