---
title: "阴影文字"
author: "dkplus"
date: "2016-09-11T17:38:04Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2016-09-11T17:38:04Z"
id: 2323813
url: "https://dkplus.iteye.com/blog/2323813"
---

001. 最近写了一段阴影文字的代码
002. /*
003. dkplus专业搜集和编写实用电脑软件教程，搜集各种软件资源和计算机周边（java网络编程、seo网站优化、web开发,lnmp,java网络编程,毕业论文设计），独立制作视频和ppt和音频微信公众号，点击进入 dkplus官方博客http://dkplus.iteye.com 微信搜索dkplus关注公众号可获取海量计算机周边资源。
004. \*/
005. import java.awt.\*;
006. import java.applet.\*;
007. import java.util.Random;
008. //跳动文字
009. public class ShadowTextApplet extends Applet implements Runnable{
010. String message;  //待显示的文本信息
011. Thread thread;  //实现文字运动的线程
012. int fontHeight,speed,baseline; //字体高度,运动速度和基线
013. Color textColor,bgColor,shadomColor; //文字颜色、背景颜色与阴影颜色
014. Image newImage;  //实现跳动的Image对象
015. Graphics newGraphics;  //实现跳动的Graphics对象
016. boolean normal; //文字是否跳动的标志
017. Font font; //显示字体
018. FontMetrics fontMetric; //显示字体的FontMetrics对象
019. public void init(){ //初始化
020. Graphics graphics = getGraphics(); //得到graphics对象
021. Dimension dim=getSize(); //得到尺寸
022. fontHeight=dim.height-10; //根据Applet尺寸设置文字高度
023. newImage=createImage(dim.width,dim.height); //创建newImage对象
024. newGraphics = newImage.getGraphics(); //得到Graphics对象
025. message=getParameter("text"); //得到显示文字
026. if (message==null){
027. message="阴影文字"; //设置默认文字
028. }
029. int textWidth=dim.width-(message.length() + 1)\*5-10; //设置文字宽度
030. do{
031. graphics.setFont(new Font("TimesRoman", 1, fontHeight)); //设置显示字体
032. fontMetric = graphics.getFontMetrics(); //得到FontMetric对象
033. if(fontMetric.stringWidth(message)&gt;textWidth) //根据文字宽度调整其高度
034. fontHeight--;
035. }
036. while(fontMetric.stringWidth(message) &gt; textWidth);{
037. baseline = getSize().height - fontMetric.getMaxDescent(); //调整显示基线位置
038. }
039. font = new Font("TimesRoman", 1, fontHeight); //得到字体实例
040. String param; //参数字符串
041. if((param = getParameter("TEXTCOLOR")) == null) //得到文本颜色
042. textColor = Color.black; //设置默认文本颜色
043. else
044. textColor = new Color(Integer.parseInt(param));  //设置文本颜色
045. if((param = getParameter("BGCOLOR")) == null)  //得到背景颜色
046. bgColor = Color.white;  //设置默认背景颜色
047. else
048. bgColor = new Color(Integer.parseInt(param));
049. if((param = getParameter("SHADOMCOLOR")) == null)  //得到阴影颜色
050. shadomColor = Color.lightGray;  //设置默认阴影颜色
051. else
052. shadomColor = new Color(Integer.parseInt(param));
053. if((param = getParameter("NORMAL")) != null) //是否是静态文本
054. normal = (Integer.valueOf(param).intValue()!=0); //参数值不为零,则为静态文本
055. setBackground(bgColor); //设置背景颜色
056. if((param = getParameter("SPEED")) != null) //得到运动速度
057. speed = Integer.valueOf(param).intValue();
058. if(speed == 0)
059. speed = 200;  //设置默认运动速度
060. thread = new Thread(this); //实例化运动文字线程
061. }
062. public void start(){ //开始运行线程
063. if(thread == null) {
064. thread = new Thread(this); //实例化线程
065. }
066. thread.start(); //线程运行
067. }
068. public void run(){  //线程运行主体
069. while(thread!=null) {
070. try{
071. Thread.sleep(speed); //线程休眠,即跳动间隔时间
072. }
073. catch(InterruptedException ex) {}
074. repaint();  //重绘屏幕
075. }
076. System.exit(0);  //退出程序
077. }
078. public void paint(Graphics g) {  //绘制Applet
079. if(normal) {  //如果是静态文本
080. g.setColor(bgColor);  //设置当前颜色
081. g.fillRect(0, 0, getSize().width, getSize().height);  //绘制填充矩形
082. g.setColor(textColor); //设置当前颜色
083. g.setFont(font); //设置当前字体
084. g.drawString(message, (getSize().width - fontMetric.stringWidth(message)) / 2, baseline); //绘出字符串
085. }
086. }
087. public void update(Graphics g){  //更新Applet
088. newGraphics.setColor(bgColor); //设置当前颜色
089. newGraphics.fillRect(0, 0, getSize().width, getSize().height); //绘制填充矩形
090. newGraphics.setColor(textColor); //设置当前颜色
091. newGraphics.setFont(font); //设置字体
092. if(!normal){ //如果是跳动文字
093. java.util.Random r=new java.util.Random();
094. int xpoint = r.nextInt(fontMetric.stringWidth(message));  //生成随机X坐标
095. font = new Font("TimesRoman",Font.BOLD,30); //设置字体
096. newGraphics.setFont(font);  //设置当前字体
097. newGraphics.setColor(shadomColor); //设置当前颜色
098. newGraphics.drawString(message,xpoint+3,baseline +3); //绘制阴影
099. newGraphics.setColor(textColor); //设置文本颜色
100. newGraphics.drawString(message,xpoint,baseline); //绘字符串
101. }
102. else {  //如果是静态文本
103. font = new Font("TimesRoman",Font.BOLD,30); //设置字体
104. newGraphics.setFont(font);  //设置当前字体
105. newGraphics.setColor(shadomColor); //设置当前颜色
106. newGraphics.drawString(message,xpoint+3,baseline +3); //绘制阴影
107. newGraphics.setColor(textColor); //设置文本颜色
108. newGraphics.drawString(message,xpoint,baseline); //绘字符串
109. }
110. g.drawImage(newImage, 0, 0, this); //绘制Image
111. }
112. }