import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.*;
import javax.swing.*;
class SimpleBalls {
private Point mousePoint
;
public static void main
(String[] args
) { new SimpleBalls();
}
public SimpleBalls() {
@Override
public void run() {
try {
.getSystemLookAndFeelClassName());
}
frame.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE); Balls balls = new Balls();
frame.add(balls);
frame.setSize(400, 400);
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 {
private List<Ball> ballsUp;
public Balls() {
ballsUp = new ArrayList<Ball>(25);
@Override
mousePoint = e.getPoint();
// System.out.println(mousePoint);
}
@Override
mousePoint = null;
}
};
addMouseListener(handler);
addMouseMotionListener(handler);
for (int index = 0; index < 10 + random(10); index++) {
ballsUp.
add(new Ball
(new Color(random
(255), random
(255),
random(255))));
}
}
@Override
protected void paintComponent
(Graphics g
) { super.paintComponent(g);
for (Ball ball : ballsUp) {
ball.paint(g2d);
}
g2d.dispose();
}
public List<Ball> getBalls() {
return ballsUp;
}
}
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();
for (Ball ball : getParent().getBalls()) {
int x = random(width);
int y = random(height);
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()) {
@Override
public void run() {
getParent().repaint();
}
});
for (Ball ball : getParent().getBalls()) {
move(ball, mousePoint);
}
// for (Ball ball : getParent().getBalls()) {
// move(ball, mousePoint);
// }
try {
ex.printStackTrace();
}
}
}
public Balls getParent() {
return parent;
}
public void move
(Ball ball,
Point mouse
) {
try {
Point p
= ball.
getLocation(); Point speed
= ball.
getSpeed();
int vx = speed.x;
int vy = speed.y;
int x = p.x;
int y = p.y;
// ----------------------
if (mouse != null) {
int xDistance
= Math.
abs(x
+ size.
width - mouse.
x); int yDistance
= Math.
abs(y
+ size.
height - mouse.
y);
if (xDistance < yDistance) {
if (x + size.width < mouse.x) {
if (vx > 0) {
vx *= -1;
}
} else {
if (vx > 0) {
vx *= -1;
}
}
} else {
if (y + size.height < mouse.y) {
if (vy > 0) {
vy *= -1;
}
} else {
if (vy > 0) {
vy *= -1;
}
}
}
}
// ----------------------
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
));
e.printStackTrace();
}
}
}
public class Ball {
private int dimeter;
public Ball
(Color color
) { dimeter = 10 + rnd.nextInt(50);
setColor(color);
speed
= new Point(10 - random
(20),
10 - random
(20));
}
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;
}
if (p != null) {
g2d.setColor(getColor());
g2d.fillOval(p.x, p.y, size.width, size.height);
}
}
}
}