import javax.swing.Timer;
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Random;
import java.awt.event.*;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
public class BubblePanel extends JPanel {
Random rand = new Random();
ArrayList<Bubble> bubbleList;
int size = 25;
Timer timer;
int delay = 93;
JSlider slider;
public BubblePanel(){
timer = new Timer(delay,new BubbleListener());
bubbleList = new ArrayList<Bubble>();
setBackground(Color.BLACK);
JPanel panel = new JPanel();
add(panel);
JButton btnNewButton_Pause = new JButton("Pause");
btnNewButton_Pause.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton)e.getSource();
if(btn.getText().equals("Pause")){
timer.stop();
btn.setText("Start");
}
else
{
timer.start();
btn.setText("Pause");
}
}
});
JLabel lblNewLabel = new JLabel("move speed");
panel.add(lblNewLabel);
slider = new JSlider();
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
int speed = slider.getValue() + 1;
int delay = 1000/speed;
timer.setDelay(delay);
}
});
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.setMinorTickSpacing(5);
slider.setMaximum(120);
slider.setMajorTickSpacing(30);
panel.add(slider);
panel.add(btnNewButton_Pause);
JButton btnNewButton_Clear = new JButton("Clear");
btnNewButton_Clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bubbleList = new ArrayList<Bubble>();
repaint();
}
});
panel.add(btnNewButton_Clear);
testBubbles();
addMouseListener(new BubbleListener());
addMouseMotionListener(new BubbleListener());
addMouseWheelListener(new BubbleListener());
timer.start();
}
private class BubbleListener extends MouseAdapter implements ActionListener{
public void mousePressed(MouseEvent e)
{
bubbleList.add(new Bubble(e.getX(),e.getY(),size));
repaint();
}
public void mouseDragged(MouseEvent e)
{
bubbleList.add(new Bubble(e.getX(),e.getY(),size));
repaint();
}
public void mouseWheelMoved(MouseWheelEvent e)
{
size += e.getUnitsToScroll();
}
public void actionPerformed(ActionEvent e) {
for(Bubble b:bubbleList)
b.update();
repaint();
}
}
public void paintComponent(Graphics canavas){
super.paintComponent(canavas);
for(Bubble b : bubbleList){
b.draw(canavas);
}
}
public void testBubbles(){
for( int n = 0; n < 100; n++){
int x = rand.nextInt(600);
int y = rand.nextInt(400);
int size = rand.nextInt(50);
bubbleList.add( new Bubble(x,y,size));
}
repaint();
}
private class Bubble{
private int x;
private int y;
private int size;
private Color color;
private int xspeed, yspeed;
private final int MAX_SPEED = 5;
public Bubble(int newX, int newY, int newSize){
x = newX;
y = newY;
// x = (newX/newSize) * newSize + newSize/2;
// y = (newY/newSize) * newSize + newSize/2;
size = newSize;
color = new Color(rand.nextInt(256),
rand.nextInt(256),
rand.nextInt(256),
rand.nextInt(256)
);
xspeed = rand.nextInt(MAX_SPEED * 2 +1) - MAX_SPEED;
yspeed = rand.nextInt(MAX_SPEED * 2 +1) - MAX_SPEED;
}
public void draw(Graphics canavas){
canavas.setColor(color);
canavas.fillOval(x - size/2, y - size/2, size, size);
}
public void update(){
//y -= 5;
x += xspeed;
y += yspeed;
if(x - size/2 <= 0 || x + size/2 >= getWidth())
xspeed = -xspeed;
if(y - size/2 <= 0 || y + size/2 >= getHeight())
yspeed = -yspeed;
}
}
}