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

📄 wavetext.java

📁 java的几个小程序
💻 JAVA
字号:
import java.awt.*;
import java.applet.*;

public class WaveText extends Applet implements Runnable {   //有线程运行接口


  String myString = null;

  int direct = 1;

  int Hrad = 12;

  int Vrad = 12;
  Thread mythread = null;  //设置一个线程

  char[] words;

  int phase = 0;

  Image offI;

  Graphics offG;

  Color[] colors;

  private Font f;         //字体

  private FontMetrics fm;    //字模



  public void init() {


     String param = null;

     myString = getParameter("word");

     setBackground(Color.black);

     words =  new char [myString.length()];
     myString.getChars(0,myString.length(),words,0);   //将字符串中的各个字符保存到数组中

     offI = createImage(getSize().width,getSize().height);

     offG = offI.getGraphics();
     f = new Font("TimesRoman",Font.BOLD,36);
     fm=getFontMetrics(f);
 

     offG.setFont(f);

 

     float hue;
    //颜色的色元
     colors = new Color[myString.length()];
     for (int i = 0; i < myString.length(); i++) {
        hue = ((float)i)/((float)myString.length());
        colors[i] = new Color(Color.HSBtoRGB(hue,1.0f,1.0f));  //颜色分配
     }


   }

    //线程开始的类




   public void start() { 

     if(mythread == null) {   //如果线程为空,则

       mythread = new Thread(this);
   //开始新的线程
       mythread.start();     //开始

     }

   }

      //终止线程



   public void stop() {

      if (mythread != null) {   //如果线程不为空,则
         mythread.stop();   //终止线程,使它

         mythread = null;     //为空
      }

   }

        //运行线程


   public void run() {

      while (mythread != null) {

         try {

            mythread.sleep(200);   //让线程沉睡200毫秒

         }catch (InterruptedException e) { }


	 repaint();       //重新绘制界面
      }


   }


   public void update(Graphics g) {      //重写update方法,解决闪烁问题

      int x, y;

      double angle;

      offG.setColor(Color.black);
      offG.fillRect(0,0,getSize().width,getSize().height);

      phase+=direct;

      phase%=8;

            for(int i=0;i<myString.length();i++) {

         angle = ((phase-i*direct)%8)/4.0*Math.PI;
         x = 20+fm.getMaxAdvance()*i+(int) (Math.cos(angle)*Hrad);

         y = 60+  (int) (Math.sin(angle)*Vrad);
        offG.setColor(colors[(phase+i)%myString.length()]);

                       offG.drawChars(words,i,1,x,y);

      }


      paint(g);

   }

   



 public void paint(Graphics g) {


      g.drawImage(offI,0,0,this);
  }


}

⌨️ 快捷键说明

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