Bonjour,

Afin de compléter mes connaissances en Java, je souhaite faire fonctionner une application Java avec NetBeans en créant avec Swing des bulles de savon qui rebondissent sur les parois et entre-elles.

Voici mon code où les fonctions Paint et g2d ne fonctionnent pas (il n'y a qu'un objet et les bulles restent statiques) :

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bulles;
 
import java.awt.BasicStroke;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.RenderingHints;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.GradientPaint;
 
/**
 *
 * @author
 */
public class Bulles {
 
    public static void main(String[] args) {
        new Bulles();
    }
 
    // Ball objects
    private Ball[] balls = new Ball[5];
    private int ballCount = 0;
 
    public Bulles() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }
 
                JFrame frame = new JFrame("Bulles");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                Balls balls = new Balls();
                frame.add(balls);
                frame.setSize(600, 600);
                frame.setVisible(true);
 
                new Thread(new BounceEngine(balls)).start();
 
            }
        });
    }
 
    public static int random(int maxRange) {
        return (int) Math.round((Math.random() * maxRange));
    }
 
    public class Balls extends JPanel {
 
        public Balls() {
 
            for (int index = 0; index < 5; index++) {
                Ball tempBall = new Ball(new Color(random(255), random(255), random(255)));
                balls[ballCount] = tempBall;
		ballCount++;
            }
        }
 
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            for (Ball ball : balls) {
                ball.paint(g2d);
            }
            g2d.dispose();
        }
 
        public Ball[] getBalls() {
            return balls;
        }
    }
 
    public class BounceEngine implements Runnable {
 
        private Balls parent;
 
        public BounceEngine(Balls parent) {
            this.parent = parent;
        }
 
        @Override
        public void run() {
 
            int width = getParent().getWidth();
            int height = getParent().getHeight();
 
            // Position de départ aléatoire...
            for (Ball ball : getParent().getBalls()) {
                int x = random(width);
                int y = random(height);
 
                Dimension size = ball.getSize();
 
                if (x + size.width > width) {
                    x = width - size.width;
                }
                if (y + size.height > height) {
                    y = height - size.height;
                }
 
                ball.setLocation(new Point(x, y));
 
            }
 
            while (getParent().isVisible()) {
 
                // Redessiner les balles...
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        getParent().repaint();
                    }
                });
 
                // dessiner à nouveau quand il y a une modification de position...
                for (Ball ball : getParent().getBalls()) {
                    move(ball);
                }
 
                // vérifier s'il y a collision
                collision();
 
                // Ralentissement de la vitesse...
                try {
                    Thread.sleep(300);
                } catch (InterruptedException ex) {
                }
 
            }
 
        }
 
        public Balls getParent() {
            return parent;
        }
 
        public void move(Ball ball) {
 
            Point p = ball.getLocation();
            Point speed = ball.getSpeed();
            Dimension size = ball.getSize();
 
            int vx = speed.x;
            int vy = speed.y;
 
            int x = p.x;
            int y = p.y;
 
            if (x + vx < 0 || x + size.width + vx > getParent().getWidth()) {
                vx *= -1;
            }
            if (y + vy < 0 || y + size.height + vy > getParent().getHeight()) {
                vy *= -1;
            }
            x += vx;
            y += vy;
 
            ball.setSpeed(new Point(vx, vy));
            ball.setLocation(new Point(x, y));
 
        }
 
        public void collision() {
 
            for(int i = 0; i < ballCount; i++)
                {
 
                Point pos1 = balls[i].getLocation();
                Point speed1 = balls[i].getSpeed();
                Dimension size1 = balls[i].getSize();
 
                int vx1 = speed1.x;
                int vy1 = speed1.y;
 
                int x1 = pos1.x;
                int y1 = pos1.y;
 
                    for(int j = i + 1; j < ballCount; j++)
                        {   
                        Point pos2 = balls[j].getLocation();
                        Point speed2 = balls[j].getSpeed();
                        Dimension size2 = balls[j].getSize();
 
                        int vx2 = speed2.x;
                        int vy2 = speed2.y;
 
                        int x2 = pos2.x;
                        int y2 = pos2.y;
 
                        double d = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
 
                        d = Math.sqrt(d);
 
                        if(d < size1.width/2+size2.width/2) {
                            //collision
 
                            vx1 *= -1;
                            vy1 *= -1;
                            vx2 *= -1;
                            vy2 *= -1;
 
                            }
 
                            x1 += vx1;
                            y1 += vy1;
 
                            x2 += vx2;
                            y2 += vy2;
 
                            balls[i].setSpeed(new Point(vx1, vy1));
                            balls[i].setLocation(new Point(x1, y1));
 
                            balls[j].setSpeed(new Point(vx2, vy2));
                            balls[j].setLocation(new Point(x2, y2));
 
                        }
                }
 
            }
 
    }
 
    public class Ball {
 
        private Color color;
        private Point location;
        private Dimension size;
        private Point speed;
 
        public Ball(Color color) {
 
            setColor(color);
 
            speed = new Point(5, 5);
            size = new Dimension(30, 30);
 
        }
 
        public Dimension getSize() {
            return size;
        }
 
        public void setColor(Color color) {
            this.color = color;
        }
 
        public void setLocation(Point location) {
            this.location = location;
        }
 
        public Color getColor() {
            return color;
        }
 
        public Point getLocation() {
            return location;
        }
 
        public Point getSpeed() {
            return speed;
        }
 
        public void setSpeed(Point speed) {
            this.speed = speed;
        }
 
 	public void paint(Graphics2D g2d) {
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setColor(color);
		g2d.setStroke(new BasicStroke(3));
                GradientPaint gradient = new GradientPaint(size.width / 2, 4,
				color, (size.width) / 2, 4 + size.width, new Color(195,205,215,240));
		g2d.setPaint(gradient);
 
 
		g2d.fillOval(4, 4, size.width, size.width);
		gradient = new GradientPaint(size.width / 25, 4, Color.white,
				(size.width * 6) / 6, (size.width * 6) / 6, new Color(color
						.getRed(), color.getGreen(), color
						.getBlue(), 0));
		g2d.setPaint(gradient); 
		g2d.fillOval(size.width / 25, 4, (size.width * 6) / 6,
				(size.width * 6) / 6);
	}
    }
}

Comment faire en sorte que la fonction Paint crée des bulles en mouvement.


D'avance merci pour votre aide.