Bonjour, tout le monde je programme en java depuis peu et je voudrais savoir a partir de ce code qui anime un rectangle
comment je dois m'y prendre pour animer d'autres composants dans la même fenetre que le premier mais pas en même temps l'un après l'autre exemple niveau de l'objet cuve monte ensuite un autre composant rectangle se remplit de la même façon
Code java : 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
88
89
90
91
92
93
94
95
96
97
98
99 /////////////1ere page//////// import javax.swing.*; public class Main extends Object { public static void pause(long duree) { try { Thread.sleep(duree); } catch(InterruptedException e) { } } public static void main(String[] args) { // Creation d'un composant cuve Animation cuve = new Animation(); // Création d'un panel pour la cuve. JPanel panel = new JPanel(); panel.add(cuve); // création d'une frame JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setContentPane(panel); f.pack(); f.setVisible(true); // Fixer le niveau de la cuve. cuve.setNiveau(0); // Animation de la cuve for(int i=cuve.NIVEAU_MIN; i<=cuve.NIVEAU_MAX; i++) { cuve.setNiveau(i); pause(100); } for(int i=cuve.NIVEAU_MAX; i>=cuve.NIVEAU_MIN;i--) { t0.setNiveau(i); pause(100); } } } //////////seconde page///////////// import javax.swing.*; import java.awt.*; public class Animation extends JComponent { public final int NIVEAU_MIN = 0; public final int NIVEAU_MAX = 50; private int temp = NIVEAU_MIN; public Animation() { super(); this.setPreferredSize(new Dimension(1262, 600)); } public void paintComponent(Graphics g) { super.paintComponent(g); //selectionne la largeur de l'écran final int w = this.getWidth(); //selectionne la hauteur de l'écran final int h = this.getHeight(); final int bulbDia = h/10; final int bulbGauche = w/2 - bulbDia/2; final int bulbHaut = h - bulbDia; final int stemLargeur = bulbDia/3; final int stemGauche = w/2 - stemLargeur/2; final int stemHauteur = h - bulbDia; final int fluidHauteur = stemHauteur * (this.temp - NIVEAU_MIN) / (MAX_TEMP - NIVEAU_MIN); final int fluidHaut = stemHauteur - fluidHauteur; g.setColor(Color.BLACK); g.fillRect(650, 0, 10, fluidHaut2); g.setColor(Color.BLUE); g.fillRect(650, fluidHaut2, 10, fluidHauteur2); } public void setNiveau(int newNiv1) { this.temp = newNiv1; this.repaint(); } public int getNiveau() { return this.temp; } }
j'epère que je m'exprime clairement
c'est surement très simple mais![]()
Partager