ITeye博客

阴影文字

dkplus · 2016-09-11 17:38 · 大杂烩 · 编程语言 · Markdown

  1. 最近写了一段阴影文字的代码
  2. /*
  3. dkplus专业搜集和编写实用电脑软件教程,搜集各种软件资源和计算机周边(java网络编程、seo网站优化、web开发,lnmp,java网络编程,毕业论文设计),独立制作视频和ppt和音频微信公众号,点击进入 dkplus官方博客http://dkplus.iteye.com 微信搜索dkplus关注公众号可获取海量计算机周边资源。
  4. */
  5. import java.awt.*;
  6. import java.applet.*;
  7. import java.util.Random;
  8. //跳动文字
  9. public class ShadowTextApplet extends Applet implements Runnable{
  10. String message;  //待显示的文本信息
  11. Thread thread;  //实现文字运动的线程
  12. int fontHeight,speed,baseline; //字体高度,运动速度和基线
  13. Color textColor,bgColor,shadomColor; //文字颜色、背景颜色与阴影颜色
  14. Image newImage;  //实现跳动的Image对象
  15. Graphics newGraphics;  //实现跳动的Graphics对象
  16. boolean normal; //文字是否跳动的标志
  17. Font font; //显示字体
  18. FontMetrics fontMetric; //显示字体的FontMetrics对象
  19. public void init(){ //初始化
  20. Graphics graphics = getGraphics(); //得到graphics对象
  21. Dimension dim=getSize(); //得到尺寸
  22. fontHeight=dim.height-10; //根据Applet尺寸设置文字高度
  23. newImage=createImage(dim.width,dim.height); //创建newImage对象
  24. newGraphics = newImage.getGraphics(); //得到Graphics对象
  25. message=getParameter("text"); //得到显示文字
  26. if (message==null){
  27. message="阴影文字"; //设置默认文字
  28. }
  29. int textWidth=dim.width-(message.length() + 1)*5-10; //设置文字宽度
  30. do{
  31. graphics.setFont(new Font("TimesRoman", 1, fontHeight)); //设置显示字体
  32. fontMetric = graphics.getFontMetrics(); //得到FontMetric对象
  33. if(fontMetric.stringWidth(message)>textWidth) //根据文字宽度调整其高度
  34. fontHeight--;
  35. }
  36. while(fontMetric.stringWidth(message) > textWidth);{
  37. baseline = getSize().height - fontMetric.getMaxDescent(); //调整显示基线位置
  38. }
  39. font = new Font("TimesRoman", 1, fontHeight); //得到字体实例
  40. String param; //参数字符串
  41. if((param = getParameter("TEXTCOLOR")) == null) //得到文本颜色
  42. textColor = Color.black; //设置默认文本颜色
  43. else
  44. textColor = new Color(Integer.parseInt(param));  //设置文本颜色
  45. if((param = getParameter("BGCOLOR")) == null)  //得到背景颜色
  46. bgColor = Color.white;  //设置默认背景颜色
  47. else
  48. bgColor = new Color(Integer.parseInt(param));
  49. if((param = getParameter("SHADOMCOLOR")) == null)  //得到阴影颜色
  50. shadomColor = Color.lightGray;  //设置默认阴影颜色
  51. else
  52. shadomColor = new Color(Integer.parseInt(param));
  53. if((param = getParameter("NORMAL")) != null) //是否是静态文本
  54. normal = (Integer.valueOf(param).intValue()!=0); //参数值不为零,则为静态文本
  55. setBackground(bgColor); //设置背景颜色
  56. if((param = getParameter("SPEED")) != null) //得到运动速度
  57. speed = Integer.valueOf(param).intValue();
  58. if(speed == 0)
  59. speed = 200;  //设置默认运动速度
  60. thread = new Thread(this); //实例化运动文字线程
  61. }
  62. public void start(){ //开始运行线程
  63. if(thread == null) {
  64. thread = new Thread(this); //实例化线程
  65. }
  66. thread.start(); //线程运行
  67. }
  68. public void run(){  //线程运行主体
  69. while(thread!=null) {
  70. try{
  71. Thread.sleep(speed); //线程休眠,即跳动间隔时间
  72. }
  73. catch(InterruptedException ex) {}
  74. repaint();  //重绘屏幕
  75. }
  76. System.exit(0);  //退出程序
  77. }
  78. public void paint(Graphics g) {  //绘制Applet
  79. if(normal) {  //如果是静态文本
  80. g.setColor(bgColor);  //设置当前颜色
  81. g.fillRect(0, 0, getSize().width, getSize().height);  //绘制填充矩形
  82. g.setColor(textColor); //设置当前颜色
  83. g.setFont(font); //设置当前字体
  84. g.drawString(message, (getSize().width - fontMetric.stringWidth(message)) / 2, baseline); //绘出字符串
  85. }
  86. }
  87. public void update(Graphics g){  //更新Applet
  88. newGraphics.setColor(bgColor); //设置当前颜色
  89. newGraphics.fillRect(0, 0, getSize().width, getSize().height); //绘制填充矩形
  90. newGraphics.setColor(textColor); //设置当前颜色
  91. newGraphics.setFont(font); //设置字体
  92. if(!normal){ //如果是跳动文字
  93. java.util.Random r=new java.util.Random();
  94. int xpoint = r.nextInt(fontMetric.stringWidth(message));  //生成随机X坐标
  95. font = new Font("TimesRoman",Font.BOLD,30); //设置字体
  96. newGraphics.setFont(font);  //设置当前字体
  97. newGraphics.setColor(shadomColor); //设置当前颜色
  98. newGraphics.drawString(message,xpoint+3,baseline +3); //绘制阴影
  99. 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. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。