JAVA版烟火

package com.gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Fire {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		MyPanel myPanel=new MyPanel();
		
		JFrame j=new JFrame();
		j.setBounds(600, 500, 300, 500);
		j.setDefaultCloseOperation(3);		
		j.add(myPanel);
		j.setVisible(true);	
	}
}

class MyPanel extends JPanel implements MouseListener,Runnable{

	public MyPanel(){
		this.setBackground(Color.black);
		this.addMouseListener(this);
	}
	
	Random r=new Random();
	int xx;
	int yy;	
	
	@Override
	public void mousePressed(MouseEvent e) {
		xx=e.getX();
		yy=e.getY();
		new Thread(this).start();
	}	
	
	@Override
	public void run() {
		//拿到鼠标点击的坐标
		
		int click_x=xx;
		int click_y=yy;
		
		int up_x=click_x;
		int up_y=467;
		Graphics g=this.getGraphics();
		//小球上升
		while(up_y>=click_y){
			g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255),r.nextInt(255)));
			g.fillOval(up_x, up_y, 5, 5);
			up_y=up_y-5;
			try {
				Thread.sleep(100);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			g.setColor(Color.black);
			g.fillOval(up_x, up_y+5,5,5);			
		}		
		//小球炸开
		for(int i=0;i<20;i++){
			g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255),r.nextInt(255)));
			g.drawOval(click_x-i*3, click_y-i*3, i*6, i*6);
			
			try {
				Thread.sleep(50);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			g.setColor(Color.black);
			g.drawOval(click_x-i*3, click_y-i*3, i*6, i*6);
		}
	}	
	
	@Override
	public void mouseClicked(MouseEvent e) {		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

}

你可能感兴趣的:(JAVAGUI)