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

📄 spotlight.java

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

public class SpotLight extends Applet implements Runnable
{
   private String myText;
   private Font font;
   private int fontSize;
   private Thread mythread;
   private int spotPosition = 50;
   private int myTextSize = 20;
   private int myTextWidth = 0;
   private int fontHeight,baseLine,w;
   
   public void init()
   {
      String fonts, temp;
      myText = getParameter("text");
      if(myText == null)
         myText = "你好!";
      fonts = getParameter("fontSize");         
      if(fonts == null)
         fontSize = 20;
      else
         fontSize = Integer.parseInt(fonts);
         
      font = new Font("TimesRoman", Font.BOLD, fontSize);   
      FontMetrics fm = getFontMetrics(font);
      fontHeight = fm.getHeight();
      baseLine = getSize().height / 2 + fontHeight / 3;
      myTextWidth = fm.stringWidth(myText);
      w = fm.stringWidth(myText);      
      w = (getSize().width - w) / 2;
      spotPosition = w;
      setBackground(Color.black);      
   }
   
   public void start()
   {
      if(mythread == null)
	  {
         mythread = new Thread(this);
         mythread.start();
      }
   }
   
   public void stop()
   {
      mythread.stop();
      mythread = null;
   }
   
   public void run()
   {
      while(true)
	  {
         repaint();
         try { mythread.sleep(50); }
		 catch(InterruptedException e) { }
      }
   }
   
   public void update(Graphics g)
   {
       paint(g);
   }
 
//利用clipRect()方法,每次调用显示方法paint()时,
//先用兰色画一遍文字,再用白色在裁剪区中画一遍文字  
   public void paint(Graphics g)
   {
      g.setFont(font);
      g.setColor(Color.blue);
      g.drawString(myText, w, baseLine); //第一遍显示
      g.clipRect(spotPosition, 0, myTextSize, getSize().height); //设置裁剪区域
      g.setColor(Color.white);
      g.drawString(myText, w, baseLine); //第二遍显示
      spotPosition = (spotPosition + 1) % (myTextWidth + 100); //移动光标位置
   }
}

⌨️ 快捷键说明

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