java小程序 烟花制作 完整源码


import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;

public class Fireworks extends Applet implementsMouseListener,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[])

  {
    Fireworksapplet = new Fireworks();
    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();
  inty_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(intj=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(inti=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;
   }
 
  public void mouseReleased(MouseEvent e)
  
  }
 
  public void mouseEntered(MouseEvent e)
  {
  }
 
  public void mouseExited(MouseEvent e)
  {
  }
 
  public void mouseClicked(MouseEvent e)
  {
  }
 
}

  • 17
    点赞
  • 181
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
下面是一个简单的 Java 程序,用于绘制放烟花的效果: ```java import java.awt.*; import javax.swing.*; public class Fireworks extends JPanel { private final int MAX_PARTICLES = 1000; // 最大粒子数 private Particle[] particles; // 粒子数组 private int numParticles; // 当前粒子数 public Fireworks() { setPreferredSize(new Dimension(500, 500)); setBackground(Color.BLACK); particles = new Particle[MAX_PARTICLES]; numParticles = 0; } public void addParticle(int x, int y, Color color) { if (numParticles < MAX_PARTICLES) { particles[numParticles++] = new Particle(x, y, color); } } public void update() { for (int i = 0; i < numParticles; i++) { if (!particles[i].update()) { particles[i] = particles[--numParticles]; particles[numParticles] = null; } } } @Override public void paintComponent(Graphics g) { super.paintComponent(g); for (int i = 0; i < numParticles; i++) { particles[i].draw(g); } } public static void main(String[] args) throws InterruptedException { JFrame frame = new JFrame("Fireworks"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Fireworks fireworks = new Fireworks(); frame.add(fireworks); frame.pack(); frame.setVisible(true); while (true) { fireworks.addParticle((int) (Math.random() * fireworks.getWidth()), (int) (Math.random() * fireworks.getHeight()), new Color((int) (Math.random() * 256), (int) (Math.random() * 256), (int) (Math.random() * 256))); fireworks.update(); fireworks.repaint(); Thread.sleep(10); } } private static class Particle { private int x, y; // 粒子位置 private int dx, dy; // 粒子速度 private Color color; // 粒子颜色 private int life; // 粒子寿命 public Particle(int x, int y, Color color) { this.x = x; this.y = y; this.color = color; dx = (int) (Math.random() * 11) - 5; dy = (int) (Math.random() * 11) - 5; life = (int) (Math.random() * 51) + 50; } public boolean update() { x += dx; y += dy; life--; return life > 0; } public void draw(Graphics g) { g.setColor(color); g.fillOval(x, y, 5, 5); } } } ``` 这个程序使用了 Java 的 Swing 组件来绘制图形,通过重写 `paintComponent` 方法来绘制粒子效果。程序会不断地在窗口中添加粒子,并在每一帧更新所有粒子的位置和寿命,最后再绘制出来。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值