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
| /*
* 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.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferStrategy;
import java.util.Random;
/**
*
* @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()
{
lance();
}
public void generateBalls(int numBalls)
{
Random rand = new Random();
for (int i = 0; i < numBalls; i++)
{
Ball tempBall = new Ball(rand.nextInt(10) + getWidth()/2, rand.nextInt(10) + getHeight()/2, 10);
balls[ballCount] = tempBall;
ballCount++;
}
}
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);
}
Ball tempBall = currentBall;
if (tempBall != null) tempBall.draw(this.g2);
if (!strategy.contentsLost()) strategy.show();
}
private class MouseHandler extends MouseAdapter implements MouseMotionListener
{
public void mousePressed(MouseEvent e)
{
Ball tempBall = new Ball(e.getX(), e.getY(), 15);
}
public void mouseReleased(MouseEvent e)
{
balls[ballCount] = tempBall;
ballCount++;
}
}
public class Ball implements Comparable<Ball>{
public Vector2d position;
private float radius;
public Ball(float x, float y, float radius)
{
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;
}
}
}
} |
Partager