import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.*;
public class AnimationTest implements ActionListener, Runnable {
AnimationPanel animationPanel = new AnimationPanel();
Thread thread;
boolean running = false;
public void actionPerformed(ActionEvent e) {
String ac = e.getActionCommand();
if(ac.equals("START"))
start();
if(ac.equals("STOP"))
stop();
if(ac.equals("CLEAR"))
animationPanel.clear();
}
private void start() {
if(!running) {
running = true;
thread = new Thread(this);
thread.setPriority(Thread.NORM_PRIORITY);
thread.start();
}
}
private void stop() {
running = false;
if(thread != null)
thread.interrupt();
thread = null;
}
public void run() {
while(running) {
try {
Thread.sleep(250);
} catch(InterruptedException e) {
running = false;
System.out.println("interrupted");
}
animationPanel.addDot();
}
}
private JPanel getAnimationPanel() {
return animationPanel;
}
private JPanel getUIPanel() {
JPanel panel = new JPanel();
String[] ids = { "start", "stop", "clear" };
for(int i = 0; i < ids.length; i++) {
JButton button = new JButton(ids[i]);
button.setActionCommand(ids[i].toUpperCase());
button.addActionListener(this);
panel.add(button);
}
return panel;
}
public static void main(String[] args) {
AnimationTest test = new AnimationTest();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(test.getAnimationPanel());
f.add(test.getUIPanel(), "Last");
f.pack();
f.setLocation(200,200);
f.setVisible(true);
}
}
class AnimationPanel extends JPanel {
Random seed = new Random();
BufferedImage image;
Path2D.Double path;
public void addDot() {
int x = seed.nextInt(100) - 5 + 300;
int y = 225 - seed.nextInt(100);
Graphics2D g2 = image.createGraphics();
g2.setPaint(new Color(210,200,230));
g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4));
g2.dispose();
repaint();
}
public void clear() {
int w = image.getWidth();
int h = image.getHeight();
Graphics2D g2 = image.createGraphics();
g2.setBackground(getBackground());
g2.clearRect(0, 0, w, h);
g2.dispose();
repaint();
}
protected void paintComponent(Graphics g) {
if(image == null)
init();
// Looks like you want the dots to appear
// under/behind the graph.
g.drawImage(image, 0, 0, this);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.blue);
g2.draw(path);
}
public Dimension getPreferredSize() {
return new Dimension(600,450);
}
private void init() {
int w = getWidth();
int h = getHeight();
int type = BufferedImage.TYPE_INT_RGB;
image = new BufferedImage(w, h, type);
Graphics2D g2 = image.createGraphics();
g2.setBackground(getBackground());
g2.clearRect(0, 0, w, h);
g2.dispose();
path = initWave(w, h);
}
private Path2D.Double initWave(int w, int h) {
Path2D.Double sineWave = new Path2D.Double();
for(int x = 0; x < w; x++) {
double y = h/2.0 - Math.sin(((double)x/w)*2*Math.PI)*(h-2)/2.0;
if(x > 0)
sineWave.lineTo(x, y);
else
sineWave.moveTo(x, y);
}
return sineWave;
}
}