一個美麗的java煙花程序
<img src="http://img.blog.csdn.net/20150625104525974?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzkxMDM1Nw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /> import java.awt.*; import java.applet.*; import java.awt.event.*; import javax.swing.*; public class ChatApplet extends Applet implements MouseListener,Runnable { int x,y; int top,point; /***對小程序進行變量和顏色的初始化。*/ public void init() { x = 0; y = 0; //設置背景色為黑色 setBackground(Color.black); addMouseListener(this); } public void paint(Graphics g) { } /** *使該程序能夠作為應用程序執行。 */ public static void main(String args[]) { ChatApplet applet = new ChatApplet(); JFrame frame = new JFrame("TextAreaNew"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e){ System.exit(0); } }); frame.getContentPane().add( applet, BorderLayout.CENTER); frame.setSize(800,400); applet.init(); applet.start(); frame.setVisible(true); } /***程序主線程。對一個煙花進行繪制。
*/ public void run() { //變量初始化 Graphics g1; g1 = getGraphics(); int y_move,y_click,x_click; int v; x_click = x; y_click = y; y_move = 400; v = 3; int r,g,b; while(y_move > y_click) { g1.setColor(Color.black); g1.fillOval(x_click,y_move,5,5); y_move -= 5; r = (((int)Math.round(Math.random()*4321))%200)+55; g = (((int)Math.round(Math.random()*4321))%200)+55; b = (((int)Math.round(Math.random()*4321))%200)+55; g1.setColor(new Color(r,g,b)); g1.fillOval(x_click,y_move,5,5); for(int j = 0 ;j<=10;j++) { if(r>55) r -= 20; if(g>55) g -= 20; if(b>55) b -=20; g1.setColor(new Color(r,g,b)); g1.fillOval(x_click,y_move+j*5,5,5); } g1.setColor(Color.black); g1.fillOval(x_click,y_move+5*10,5,5); try { Thread.currentThread().sleep(v++); } catch (InterruptedException e) {} } for(int j=12;j>=0;j--) { g1.setColor(Color.black); g1.fillOval(x_click,y_move+(j*5),5,5); try { Thread.currentThread().sleep((v++)/3); } catch (InterruptedException e) {} } y_move = 400; g1.setColor(Color.black); while(y_move > y_click) { g1.fillOval(x_click-2,y_move,9,5); y_move -= 5; } v = 15; for(int i=0;i<=25;i++) { r = (((int)Math.round(Math.random()*4321))%200)+55; g = (((int)Math.round(Math.random()*4321))%200)+55; b = (((int)Math.round(Math.random()*4321))%200)+55; g1.setColor(new Color(r,g,b)); g1.drawOval(x_click-3*i,y_click-3*i,6*i,6*i); if(i<23) { g1.drawOval(x_click-3*(i+1),y_click-3*(i+1),6*(i+1),6*(i+1)); g1.drawOval(x_click-3*(i+2),y_click-3*(i+2),6*(i+2),6*(i+2)); } try { Thread.currentThread().sleep(v++); } catch (InterruptedException e) {} g1.setColor(Color.black); g1.drawOval(x_click-3*i,y_click-3*i,6*i,6*i); }} /***對鼠標事件進行監聽。
*臨聽其鼠標按下事件。
*當按下鼠標時,產生一個新線程。*/ public void mousePressed(MouseEvent e) { x = e.getX(); y = e.getY(); Thread one; one = new Thread(this); one.start(); one = null; } /** *實現MouseListener接中的方法。為一個空方法。
*/ public void mouseReleased(MouseEvent e) { } /** *實現MouseListener接中的方法。
為一個空方法。 */ public void mouseEntered(MouseEvent e) { } /** *實現MouseListener接中的方法。為一個空方法。
*/ public void mouseExited(MouseEvent e) { } /** *實現MouseListener接中的方法。為一個空方法。 */ public void mouseClicked(MouseEvent e) { } }
一個美麗的java煙花程序