/*******************************************************************************
* Copyright (c) 2010 Tudor Marcu
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE
*******************************************************************************/
import java.awt.*;
import java.awt.event.*;
import static java.
awt.
event.
KeyEvent.
*;
import javax.swing.*;
import javax.swing.*;
import java.util.*;;
import javax.imageio.ImageIO;
private final int WIDTH = 800, HEIGHT = 800, HALF_WIDTH = WIDTH/2, HALF_HEIGHT = HEIGHT/2;
private boolean running;
private Thread mainLoop, collisionLoop
;
private Image rearBuffer
;
private int space;
private int maxObjects, objectCount;
private int[] particleSpeedX, particleSpeedY;
private int particleWidth, particleHeight;
private int maxSpeed;
private int randSpeed;
private int signToggle;
// The class constructor
public Particles() {
frame
= new JFrame("particles Demo");
frame.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE);
frame.addKeyListener(this);
c.setMinimumSize(d);
c.setMaximumSize(d);
c.setPreferredSize(d);
c.add(this);
frame.pack();
frame.setVisible(true);
init();
}
public void init() {
//Initialize variables
space = 0;
maxObjects = 400;
objectCount = 0;
particleWidth = 7;
particleHeight = 7;
particleSpeedX = new int[maxObjects];
particleSpeedY = new int[maxObjects];
maxSpeed = 5;
randSpeed = 0;
signToggle = 1;
// Initialize the particles
for(int i = 0; i < maxObjects; i++) {
particle
[i
] = new Rectangle(0
+space, HALF_HEIGHT, particleWidth, particleHeight
);
particleSpeedX
[i
] = (int)(Math.
random()*maxSpeed
)+1;
particleSpeedY
[i
] = (int)(Math.
random()*maxSpeed
)+1;
space += particleWidth+1;
}
// Set the rearbuffer up for double buffering
rearBuffer = this.createImage(WIDTH, HEIGHT);
rbc = rearBuffer.getGraphics();
//Set up the run flags and thread
running = true;
mainLoop.start();
}
// Set up a seperate function to handle drawing, and pass it into paint
public void render() {
rbc.
setColor(Color.
black); // Set the background color and fill it
rbc.fillRect(0, 0, WIDTH, HEIGHT);
rbc.
setColor(Color.
white); // Set the color of the particles
// Render each particle based on its rectangle information
for(int i = 0; i < maxObjects; i++) {
rbc.drawOval(particle[i].x, particle[i].y, particle[i].width, particle[i].height);
rbc.fillOval(particle[i].x, particle[i].y, particle[i].width, particle[i].height);
}
}
// Check collisions with the window boundary
public void checkBounds() {
for(int i = 0; i < maxObjects; i++) {
// Handle all the collision for the bottom and right.
// The speed must always be negative in order for the particles
// to move back inside of the window.
if(particle[i].x + particle[i].width >= WIDTH) {
particleSpeedX
[i
] = Math.
abs(particleSpeedX
[i
])*-1;
}
else if(particle[i].y + particle[i].height >= HEIGHT) {
particleSpeedY
[i
] = Math.
abs(particleSpeedY
[i
])*-1;
}
// Handle the collisions for the top and left.
// The speed has to be positive for the particles to move back
// inside of the window
else if(particle[i].x <= 0) {
particleSpeedX
[i
] = Math.
abs(particleSpeedX
[i
]);
}
else if(particle[i].y <= 0) {
particleSpeedY
[i
] = Math.
abs(particleSpeedY
[i
]);
}
}
}
// Keep the particles moving based on their x and y speeds
public void moveParticles() {
for(int i = 0; i < maxObjects; i++) {
particle[i].x += particleSpeedX[i];
particle[i].y += particleSpeedY[i];
}
}
// Get the distance between two circles for perfect circle collision
int distance( int x1, int y1, int x2, int y2 ) {
//Return the distance between the two points
return (int)Math.
sqrt( Math.
pow( x2
- x1, 2
) + Math.
pow( y2
- y1, 2
) );
}
// Work in progress, check for collisions between particles
public void particleCollisions() {
for(int i = 0; i < maxObjects; i++) {
for(int k = i+1; k <= maxObjects-1; k++) {
if(distance(particle[i].x, particle[i].y, particle[k].x, particle[k].y) <= ((particle[i].width/2)+(particle[k].width/2))) {
/* if(particleSpeedX[i] < 0) {
particleSpeedX[i] = Math.abs(particleSpeedX[i]);
}
if(particleSpeedY[i] < 0) {
particleSpeedY[i] = particleSpeedX[i] = Math.abs(particleSpeedX[i]);
}
particleSpeedX[i] = Math.abs(particleSpeedX[k])*-1;
particleSpeedY[i] = Math.abs(particleSpeedY[k])*-1;*/
particleSpeedX[i] *= -1;
particleSpeedY[i] *= -1;
particleSpeedX[k] *= -1;
particleSpeedY[k] *= -1;
}
}
}
}
public void paintComponent
(Graphics g
) {
super.paintComponent(g);
render();
g.drawImage(rearBuffer, 0, 0, null);
}
paint(g);
}
public void run() {
while(running) {
particleCollisions();
moveParticles();
checkBounds();
repaint();
try {
}
}
}
}
}
}
switch (e.getKeyCode()) {
case VK_SPACE:
for (int i = 0; i < maxObjects; i++) {
randSpeed
= (int)(Math.
random()*maxSpeed
)+1;
signToggle *= -1;
if(signToggle < 0) {
particleSpeedX[i] = randSpeed*signToggle;
particleSpeedY[i] = randSpeed*signToggle;
}
else if(signToggle > 0) {
particleSpeedX
[i
] = Math.
abs(randSpeed
*signToggle
);
particleSpeedY
[i
] = Math.
abs(randSpeed
*signToggle
);
}
}
break;
case VK_ENTER:
space = 0;
for(int i = 0; i < maxObjects; i++) {
particle[i].x = 0+space;
particle[i].y = HALF_HEIGHT;
particleSpeedX
[i
] = (int)(Math.
random()*maxSpeed
)+1;
particleSpeedY
[i
] = (int)(Math.
random()*maxSpeed
)+1;
space += particleWidth+1;
}
break;
}
}
private void moveMouse(int x, int y) {
}
public static void main
(String[] args
) {
public void run() {
new Particles();
}
});
}
}