⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fireworks.java

📁 几个基础的java Applet程序
💻 JAVA
字号:
import java.awt.*;
import java.applet.Applet;
//定义FireWorks类
public class FireWorks extends Applet implements Runnable
{
  Dimension	d;//定义一个Dimension对象
  //定义一个字体对象
  Font 		font = new Font("Helvetica", Font.BOLD, 36);
  FontMetrics	fm;//定义一个字体矩阵  
  Graphics	goff;//定义一个Graphics对象
  Image		ii;//定义一个Image对象
  Thread	thethread;//定义一个线程
  String	s;
  final int	numrockets=8;
  final int	num=16;
  int[]		xcoord, ycoord, xspeed, yspeed;
  int[]		count;
  boolean[]	exploding;
  //获取小应用程序信息
  public String getAppletInfo()
  {
    return("Fireworks - By Ali");
  }
  //初始化
  public void init()
  {
    Graphics g;
    int i;
    d = size();//获取小应用程序大小
    g=getGraphics();
    g.setFont(font);//设置字体
    fm = g.getFontMetrics();//获取字体矩阵
    xcoord=new int[numrockets*num];
    ycoord=new int[numrockets*num];
    xspeed=new int[numrockets*num];
    yspeed=new int[numrockets*num];
    count=new int[numrockets];
    exploding=new boolean[numrockets];
    for (i=0; i<numrockets*num; i++)
    {
      xcoord[i]=-20000;
      ycoord[i]=-20000;
      xspeed[i]=0;
      yspeed[i]=0;
    }
    for (i=0; i<numrockets; i++)
    {
      count[i]=1+i*16;
      exploding[i]=true;
    }
    s=getParameter("Text");//获取所要显示的文字
  }
  //paint()方法
  public void paint(Graphics g)
  {
    Graphics gg;
    if (goff==null && d.width>0 && d.height>0)
    {
      //创建一个可以使用的双缓冲图像
      ii = createImage(d.width, d.height);
      goff = ii.getGraphics();
      goff.setFont(font);
    }
    if (goff==null || ii==null)
      return;
    goff.setColor(Color.black);//设置颜色
    goff.fillRect(0, 0, d.width, d.height);//填充矩形
    FireWorks(goff);//调用FireWorks
    goff.setColor(new Color(128, 192, 255));//设置字体颜色
    //将文字画在屏幕上
    goff.drawString(s,(d.width-fm.stringWidth(s)) / 2, d.height/2 );
    g.drawImage(ii, 0, 0, this);
  }
  //FireWorks()方法
  public void FireWorks(Graphics g)
  {
    int		i,j,index;
    int		x,y,xspd,yspd;

    for (i=0; i<numrockets; i++)
    {
      if (!exploding[i] && yspeed[i*num]>0)
      { // 爆炸
        exploding[i]=true;
        for (j=0; j<num; j++)
        {
          index=i*num+j;
          yspeed[index]=(int)((Math.random()*28.0))-15;
          xspeed[index]=(int)((Math.random()*31.0))-16;
          if (xspeed[index]>=0) xspeed[index]++;
        }
      }
      for (j=0; j<num; j++)
      {
        index=i*num+j;
        if (exploding[i])
        {
          switch(i&3)
          {
            case 0:
              g.setColor(new Color(192,(count[i])+32,(count[i])+127));
              break;
            case 1:
              g.setColor(new Color(count[i]+32,192,count[i]+127));
              break;
            case 2:
              g.setColor(new Color(192, 192, count[i]+32));
              break;
            default:
              g.setColor(new Color(count[i]+32, count[i]+127, 192));
          }
        }
        else
          g.setColor(Color.white);
        g.fillRect(xcoord[index]>>3, ycoord[index]>>3,2,2);
        xcoord[index]+=xspeed[index];
        ycoord[index]+=yspeed[index];
        yspeed[index]++;
      }
      count[i]--;
      if (count[i]<=0)
      {
        count[i]=128;
        exploding[i]=false;
        x=((int)((Math.random()*d.width)))<<3;
        y=d.height<<3;
        yspd=(int)((Math.random()*28))-58;
        xspd=(int)((Math.random()*15.0))-8;
          if (xspd>=0) xspd++;
        for (j=0; j<num; j++)
        {
          index=i*num+j;
          xcoord[index]=x;
          ycoord[index]=y;
          xspeed[index]=xspd;
          yspeed[index]=yspd;
        }
      }
    }
  }
  //run()方法
  public void run()
  {
    long	starttime;
    Graphics	g;
    //设置线程的优先权
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    g=getGraphics();
    while(true)
    {
       //获取系统当前时间
       starttime=System.currentTimeMillis();
      try
      {
        paint(g);
        starttime += 30;
        //使线程进入睡眠状态
        Thread.sleep(Math.max(0, starttime-System.currentTimeMillis()));
      }
      catch (InterruptedException e)//抛出异常
      {
        break;
      }
    }
  }
  //启动线程
  public void start()
  {
    if (thethread == null) {
      thethread = new Thread(this);
      thethread.start();
    }
  }
  //停止线程
  public void stop()
  {
    if (thethread != null) {
      thethread.stop();
      thethread = null;
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -