1 /* 2 * To change this license header, choose License Headers in Project Properties. 3 * To change this template file, choose Tools | Templates 4 * and open the template in the editor. 5 */ 6 package newpackage; 7 8 import java.applet.Applet; 9 import java.awt.*; 10 11 public class Text3DApplet extends Applet implements Runnable { 12 13 Image image; //绘制文字的Image对象 14 Graphics graphics; //绘制文字的Graphics对象 15 Thread thread; //显示三维文字线程 16 int width,height; //显示宽度、高度 17 String message; //显示信息 18 int fontSize; //文字尺寸 19 Font font; //字体 20 21 public void init() { 22 Dimension dim=getSize(); //得到Applet的尺寸 23 width = dim.width; //得到宽度 24 height = dim.height; //得到高度 25 image = createImage(width, height); //得到Image实例 26 graphics= image.getGraphics(); //得到Grahpics实例 27 message = getParameter("text"); //从HTML文件中得到显示信息 28 if (message == null) { //如果信息为空 29 message="三维文字"; //设置默认信息 30 } 31 fontSize = 30; //设置字体大小 32 } 33 34 35 public void start() { 36 if (thread == null) { 37 thread = new Thread(this); //实例化线程 38 thread.start(); //运行线程 39 } 40 } 41 42 public void run() { //线程运行主体 43 while (thread != null) { 44 try { 45 Thread.sleep(50L); //线程休眠 46 } catch (InterruptedException ex) { 47 } 48 repaint(); //重绘屏幕 49 } 50 } 51 52 public void update(Graphics g) { 53 font = new Font("TimesRoman", 1, fontSize); //得到字体实例 54 graphics.setFont(font); //设置显示字体 55 int j = (int) (255 * Math.random()); //变量,用于生成渐变颜色 56 int k = (int) (255 * Math.random()); 57 int l = (int) (255 * Math.random()); 58 try { 59 Thread.sleep(2000); //线程休眠 60 } catch (InterruptedException ex) { 61 } 62 graphics.setColor(Color.orange); //设置当前颜色 63 graphics.fillRect(0, 0, width, height); //填充背景 64 for (int i = 0; i < 6; i++) { //三维深度 65 graphics.setColor( //设置渐变颜色 66 new Color( 67 255 - ((255 - j) * i) / 10, 68 255 - ((255 - k) * i) / 10, 69 255 - ((255 - l) * i) / 10)); 70 graphics.drawString(message, 15 - i, height - 15-i); //绘制字符串 71 } 72 g.drawImage(image, 0, 0, this); //绘制Image到屏幕 73 } 74 75 public void paint(Graphics g) { 76 update(g); 77 } 78 }