一個漂亮的java煙花程式

來源:互聯網
上載者:User

<img src="http://img.blog.csdn.net/20140617111511890?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzkxMDM1Nw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />package org.jetic.web.chat;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)  {  } }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.