
import javax.swing.*;
import java.awt.*;
import java.util.*;
class FireworkWindow extends JFrame{
public static void main(String[] args) {
// TODO 自动生成的方法存根
FireworkWindow fw=new FireworkWindow();
}
public FireworkWindow()
{
FireworkPanel fp=new FireworkPanel();
Thread t=new Thread(fp);
t.start();
this.add(fp);
this.setLocation(100, 20);
this.setSize(1000,700);
this.setVisible(true);
}
}
class FireworkPanel extends JPanel implements Runnable{
int timesDown=10;
int n=10;
Vector<Ball> bs=new Vector<Ball>();
public FireworkPanel()
{
for(int i=0;i<n;i++)
{
Ball b=new Ball();
bs.add(b);
}
}
public void paint(Graphics g)
{
super.paint(g);
g.fillRect(0, 0, 1000, 700);
String t=String.valueOf(timesDown);
g.setColor(Color.white);
g.setFont(new Font("微软雅黑",Font.ITALIC,500));
g.drawString(t, 300, 500);
g.setColor(Color.orange);
for(int i=0;i<bs.size();i++)
{
bs.get(i).setX((int)(Math.random()*900));
bs.get(i).setY((int)(Math.random()*600));
g.fillOval(bs.get(i).getX(), bs.get(i).getY(), 20, 20);
System.out.println(Math.random()*600);
System.out.println(bs.get(i).getX()+" "+ bs.get(i).getY());
}
g.setColor(Color.red);
for(int i=0;i<bs.size();i++)
{
bs.get(i).setX((int)(Math.random()*900));
bs.get(i).setY((int)(Math.random()*600));
g.fillOval(bs.get(i).getX(), bs.get(i).getY(), 20, 20);
System.out.println(Math.random()*600);
System.out.println(bs.get(i).getX()+" "+ bs.get(i).getY());
}
g.setColor(Color.yellow);
for(int i=0;i<bs.size();i++)
{
bs.get(i).setX((int)(Math.random()*900));
bs.get(i).setY((int)(Math.random()*600));
g.fillOval(bs.get(i).getX(), bs.get(i).getY(), 20, 20);
System.out.println(Math.random()*600);
System.out.println(bs.get(i).getX()+" "+ bs.get(i).getY());
}
g.setColor(Color.green);
for(int i=0;i<bs.size();i++)
{
bs.get(i).setX((int)(Math.random()*900));
bs.get(i).setY((int)(Math.random()*600));
g.fillOval(bs.get(i).getX(), bs.get(i).getY(), 20, 20);
System.out.println(Math.random()*600);
System.out.println(bs.get(i).getX()+" "+ bs.get(i).getY());
}
g.setColor(Color.blue);
for(int i=0;i<bs.size();i++)
{
bs.get(i).setX((int)(Math.random()*900));
bs.get(i).setY((int)(Math.random()*600));
g.fillOval(bs.get(i).getX(), bs.get(i).getY(), 20, 20);
System.out.println(Math.random()*600);
System.out.println(bs.get(i).getX()+" "+ bs.get(i).getY());
}
g.setColor(Color.pink);
for(int i=0;i<bs.size();i++)
{
bs.get(i).setX((int)(Math.random()*900));
bs.get(i).setY((int)(Math.random()*600));
g.fillOval(bs.get(i).getX(), bs.get(i).getY(), 20, 20);
System.out.println(Math.random()*600);
System.out.println(bs.get(i).getX()+" "+ bs.get(i).getY());
}
}
@Override
public void run() {
while(true)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.repaint();
timesDown--;
if(timesDown==-1)
timesDown=10;
}
}
}
class Ball {
int x,y;
int speed=1;
public Ball(){}
public Ball(int x,int y)
{
this.x=x;
this.y=y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}