import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Calendar;
import java.util.Random;
public class Bubbles extends JFrame implements ActionListener,Runnable{
JPanel control,canvas;
JComboBox size,shape,action;
JColorChooser jcolor;
JFrame f = new JFrame(); //Frame窗口
JButton color_Frame=new JButton("选择颜色");
JButton ok=new JButton("开始运动");
JLabel label=new JLabel("泡泡属性选择:");
long c1,c2;
long dif_time;
String time;
String box1_chose,box2_chose,box3_chose;
Thread ko_Thread;
int command1=0;
int M_x,M_y;
static int w_h=10;
ImageIcon img= new ImageIcon( "bg1"+".jpg");
public Bubbles(){
super("峰峰泡泡盒");
this.setSize(600,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLayout(new BorderLayout());
ko_Thread=new Thread();
control=new JPanel();
canvas=new JPanel(){
public void paintComponent(Graphics g){
g.drawImage(img.getImage(),0,0,null);
super.paintComponent(g);
}
};
canvas.setOpaque(false);
control.setLayout(new FlowLayout());
size=new JComboBox();
shape=new JComboBox();
action=new JComboBox();
jcolor=new JColorChooser();
size.addItem("大");
size.addItem("中");
size.addItem("小");
size.addItem("随机");
size.addItem("清空再输入数字");
size.setEditable(true);
action.addItem("静止");
action.addItem("运动");
shape.addItem("圆形");
shape.addItem("椭圆形");
control.add(label);
control.add(size);
control.add(action);
control.add(shape);
control.add(color_Frame);
control.add(ok);
color_Frame.addActionListener(this);
ok.addActionListener(this);
canvas.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
M_x=e.getX();
M_y=e.getY();
c1=System.currentTimeMillis();//得到当前系统时间
box1_chose=(String) size.getSelectedItem();
box2_chose=(String) action.getSelectedItem();
box2_chose=(String) shape.getSelectedItem();
repaint();
command1=1;
start();
System.out.print(action.getSelectedIndex());
}
@Override
public void mouseReleased(MouseEvent e){
c2=System.currentTimeMillis();//得到当前系统时间
dif_time=c2-c1;
time=""+dif_time;
stop();
System.out.print(dif_time);
}
});
//control.setSize(500, 100);
this.add(control, BorderLayout.SOUTH);
//canvas.setSize(500, 400);
//canvas.setBackground(Color.WHITE);
this.add(canvas,BorderLayout.CENTER);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==color_Frame){
f.add(jcolor);
f.setSize(350, 300);
f.setVisible(true);//显示窗口
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
if(e.getSource()==ok){
box1_chose=(String) size.getSelectedItem();
box2_chose=(String) action.getSelectedItem();
box2_chose=(String) shape.getSelectedItem();
command1=1;
start();
System.out.println(action.getSelectedIndex());
repaint();
}
}
public static void main(String[] args) {
Bubbles bubbles=new Bubbles();
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(jcolor.getColor());
//g.fillOval(M_x, M_y, w_h,w_h);
/*if(command1==1){
switch(size.getSelectedIndex())
{
case 0: w_h=200; break;
case 1: w_h=100;break;
case 2: w_h=50;break;
case 3:{
Random r=new Random();
w_h=r.nextInt(30)*10;
}break;
default :
w_h=Integer.parseInt((String)size.getEditor().getItem());
break;}
g.fillOval(M_x, M_y, w_h,w_h);
}*/
g.fillOval(M_x, M_y, w_h,w_h);
}
//线程开始
public void start(){
if(ko_Thread==null){
ko_Thread=new Thread(this);
ko_Thread.start();
}
}
//结束
public void stop(){
if(ko_Thread!=null){
ko_Thread=null;
}
}
//执行
public void run(){
Thread thisThread=Thread.currentThread();
while(ko_Thread==thisThread){
if(action.getSelectedIndex()==1){
Move();
}
else w_h=w_h+5;
repaint();
try{
Thread.sleep(100);
}catch(InterruptedException e){}
}
}
public void Move(){
if(action.getSelectedIndex()==1){
Random Move=new Random();
int direction=Move.nextInt(3);
switch(direction){
case 1: M_x=M_x-10;break;
case 2: M_y=M_y-10;break;
case 3: M_x=M_x+10;break;
case 0: M_y=M_y+10;break;
}
}
}
}