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 }