Bonjour,

Je souhaite faire entrer en mouvement des balles crées au clic de la souris et affichées à l'écran de mon application Java sous NetBeans.

J'ai 3 fichiers java :

Main.java :

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
package balles;
 
import javax.swing.*;
 
public class Main {
 
	public static void main(String[] args)
	{
		JFrame frame = new JFrame("Balles");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(),BoxLayout.Y_AXIS));
 
 
		Balles ballCanvas = new Balles();
 
		frame.getContentPane().add(ballCanvas);
		frame.pack();
		frame.setVisible(true);
 
		ballCanvas.start();
 
	}
 
}
Vector2d.java :

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
package balles;
 
public class Vector2d {
 
	private float x;
	private float y;
 
		public Vector2d(float x, float y)
	{
		this.setX(x);
		this.setY(y);
	}
 
		public void setX(float x) {
		this.x = x;
	}
 
                public float getX() {
		return x;
	}
 
		public void setY(float y) {
		this.y = y;
	}
 
                public float getY() {
		return y;
	}
 
}
Balles.java :

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
/*
 * 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 balles;
 
import java.awt.Dimension;
import java.awt.Canvas;
import java.awt.RenderingHints;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferStrategy;
 
/**
 *
 * @author
 */
public class Balles extends Canvas {
 
    // Rendering / Buffer objects
	private BufferStrategy strategy;
	private Graphics2D g2;
 
    // Ball objects
    private Ball[] balls = new Ball[5000];
    private Ball tempBall;
    private Ball currentBall;
    private int ballCount = 0;
 
    public Balles() {
 
        setPreferredSize(new Dimension(800, 600));
	setIgnoreRepaint(true);
 
        // Evénement(s) clic
		MouseHandler mouseHandler = new MouseHandler();
		addMouseMotionListener(mouseHandler);
		addMouseListener(mouseHandler); 
            }
 
    	public void start()
	{
		mainStart();
	}
 
        	public void mainStart()
	{
            while(true)
		{
                lance();
                }
 
        }
 
          	public void lance()
	{
 
		if (strategy == null || strategy.contentsLost())
		{
			// Creation BufferStrategy pour affichage
			createBufferStrategy(2);
			strategy = getBufferStrategy();
			Graphics g = strategy.getDrawGraphics();
			this.g2 = (Graphics2D) g;
		}
 
 
		this.g2.setRenderingHint (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 
		// Affichage arrière plan
		this.g2.setColor(Color.BLACK);
		this.g2.fillRect(0, 0, getWidth(), getHeight());
 
		// Affichage des objets
		for(int i = 0; i < ballCount; i++)
		{
			balls[i].draw(this.g2);
		}
 
 
  		if (!strategy.contentsLost()) strategy.show();
 
	}
 
    	private class MouseHandler extends MouseAdapter implements MouseMotionListener
	{
		public void mousePressed(MouseEvent e)
                {
 
                tempBall = new Ball(e.getX(), e.getY(), 15);
                tempBall.setLocation(new Point(e.getX(), e.getY()));
                }
 
                public void mouseReleased(MouseEvent e)
                {
                balls[ballCount] = tempBall;
		ballCount++;
                }
        }
 
public class Ball implements Comparable<Ball>{
 
	public Vector2d position;
	private float radius;
        private Point location;
        private Point speed;
 
	public Ball(float x, float y, float radius)
	{
                speed = new Point(10 , 10);
		this.position = new Vector2d(x, y);
		this.setRadius(radius);
	}
 
 	public void draw(Graphics2D g2)
	{
		g2.setColor(new Color(255, 255, 255));
		g2.fillOval( (int) (position.getX() - getRadius()), (int) (position.getY() - getRadius()), (int) (2 * getRadius()) , (int) (2 * getRadius()) );
 
	}
 
	public void setRadius(float radius) {
		this.radius = radius;
	}
 
	public float getRadius() {
		return radius;
	}
 
        	public int compareTo(Ball ball) {
 
                    	if (this.position.getX() - this.getRadius() > ball.position.getX() - ball.getRadius())
		{
			return 1;
		}
		else if (this.position.getX() - this.getRadius() < ball.position.getX() - ball.getRadius())
		{
			return -1;
		}
		else
		{
			return 0;
		}
 
                }
 
        public void setLocation(Point location) {
                this.location = location;
        }
 
        public Point getLocation() {
                return location;
        }
 
        public Point getSpeed() {
                return speed;
        }
 
        public void setSpeed(Point speed) {
                this.speed = speed;
        }
}
 
}
A l'aide de l'import AWT : Point, comment faire que BufferStrategy me permette de changer la position des balles ?

D'avance merci pour votre aide.