`
dkplus
  • 浏览: 17523 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

阴影文字

阅读更多
  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. //跳动文字  
  10.   
  11. public class ShadowTextApplet extends Applet implements Runnable{     
  12.    String message;  //待显示的文本信息  
  13.    Thread thread;  //实现文字运动的线程  
  14.    int fontHeight,speed,baseline; //字体高度,运动速度和基线  
  15.    Color textColor,bgColor,shadomColor; //文字颜色、背景颜色与阴影颜色  
  16.    Image newImage;  //实现跳动的Image对象  
  17.    Graphics newGraphics;  //实现跳动的Graphics对象  
  18.    boolean normal; //文字是否跳动的标志  
  19.    Font font; //显示字体  
  20.    FontMetrics fontMetric; //显示字体的FontMetrics对象  
  21.   
  22.    public void init(){ //初始化  
  23.         Graphics graphics = getGraphics(); //得到graphics对象  
  24.        Dimension dim=getSize(); //得到尺寸  
  25.        fontHeight=dim.height-10//根据Applet尺寸设置文字高度  
  26.        newImage=createImage(dim.width,dim.height); //创建newImage对象  
  27.        newGraphics = newImage.getGraphics(); //得到Graphics对象  
  28.        message=getParameter("text"); //得到显示文字  
  29.        if (message==null){          
  30.             message="阴影文字"//设置默认文字  
  31.        }  
  32.          
  33.        int textWidth=dim.width-(message.length() + 1)*5-10//设置文字宽度  
  34.        do{  
  35.         graphics.setFont(new Font("TimesRoman"1, fontHeight)); //设置显示字体  
  36.           fontMetric = graphics.getFontMetrics(); //得到FontMetric对象  
  37.           if(fontMetric.stringWidth(message)>textWidth) //根据文字宽度调整其高度  
  38.              fontHeight--;  
  39.        }  
  40.        while(fontMetric.stringWidth(message) > textWidth);{  
  41.         baseline = getSize().height - fontMetric.getMaxDescent(); //调整显示基线位置  
  42.        }  
  43.        font = new Font("TimesRoman"1, fontHeight); //得到字体实例  
  44.          
  45.        String param; //参数字符串  
  46.        if((param = getParameter("TEXTCOLOR")) == null//得到文本颜色  
  47.         textColor = Color.black; //设置默认文本颜色  
  48.        else  
  49.           textColor = new Color(Integer.parseInt(param));  //设置文本颜色  
  50.        if((param = getParameter("BGCOLOR")) == null)  //得到背景颜色  
  51.            bgColor = Color.white;  //设置默认背景颜色  
  52.        else  
  53.            bgColor = new Color(Integer.parseInt(param));   
  54.        if((param = getParameter("SHADOMCOLOR")) == null)  //得到阴影颜色  
  55.            shadomColor = Color.lightGray;  //设置默认阴影颜色  
  56.        else  
  57.            shadomColor = new Color(Integer.parseInt(param));   
  58.        if((param = getParameter("NORMAL")) != null//是否是静态文本  
  59.            normal = (Integer.valueOf(param).intValue()!=0); //参数值不为零,则为静态文本  
  60.        setBackground(bgColor); //设置背景颜色  
  61.        if((param = getParameter("SPEED")) != null//得到运动速度  
  62.            speed = Integer.valueOf(param).intValue();  
  63.        if(speed == 0)  
  64.            speed = 200;  //设置默认运动速度     
  65.         thread = new Thread(this); //实例化运动文字线程  
  66.     }  
  67.   
  68.     public void start(){ //开始运行线程  
  69.         if(thread == null) {  
  70.                 thread = new Thread(this); //实例化线程  
  71.         }  
  72.         thread.start(); //线程运行  
  73.     }  
  74.   
  75.     public void run(){  //线程运行主体  
  76.         while(thread!=null) {   
  77.             try{  
  78.                 Thread.sleep(speed); //线程休眠,即跳动间隔时间  
  79.             }  
  80.             catch(InterruptedException ex) {}  
  81.             repaint();  //重绘屏幕  
  82.         }  
  83.         System.exit(0);  //退出程序  
  84.     }  
  85.   
  86.   
  87.     public void paint(Graphics g) {  //绘制Applet  
  88.         if(normal) {  //如果是静态文本  
  89.             g.setColor(bgColor);  //设置当前颜色  
  90.             g.fillRect(00, getSize().width, getSize().height);  //绘制填充矩形  
  91.             g.setColor(textColor); //设置当前颜色  
  92.             g.setFont(font); //设置当前字体  
  93.             g.drawString(message, (getSize().width - fontMetric.stringWidth(message)) / 2, baseline); //绘出字符串  
  94.         }  
  95.     }  
  96.   
  97.     public void update(Graphics g){  //更新Applet  
  98.         newGraphics.setColor(bgColor); //设置当前颜色  
  99.         newGraphics.fillRect(00, getSize().width, getSize().height); //绘制填充矩形  
  100.         newGraphics.setColor(textColor); //设置当前颜色  
  101.         newGraphics.setFont(font); //设置字体  
  102.         if(!normal){ //如果是跳动文字  
  103.                 java.util.Random r=new java.util.Random();    
  104.             int xpoint = r.nextInt(fontMetric.stringWidth(message));  //生成随机X坐标  
  105.               
  106.             font = new Font("TimesRoman",Font.BOLD,30); //设置字体  
  107.             newGraphics.setFont(font);  //设置当前字体  
  108.                   
  109.             newGraphics.setColor(shadomColor); //设置当前颜色  
  110.             newGraphics.drawString(message,xpoint+3,baseline +3); //绘制阴影  
  111.               
  112.             newGraphics.setColor(textColor); //设置文本颜色  
  113.             newGraphics.drawString(message,xpoint,baseline); //绘字符串  
  114.                   
  115.         }   
  116.         else {  //如果是静态文本  
  117.             font = new Font("TimesRoman",Font.BOLD,30); //设置字体  
  118.             newGraphics.setFont(font);  //设置当前字体  
  119.                   
  120.             newGraphics.setColor(shadomColor); //设置当前颜色  
  121.             newGraphics.drawString(message,xpoint+3,baseline +3); //绘制阴影  
  122.               
  123.             newGraphics.setColor(textColor); //设置文本颜色  
  124.             newGraphics.drawString(message,xpoint,baseline); //绘字符串  
  125.           }  
  126.         g.drawImage(newImage, 00this); //绘制Image  
  127.     }  
  128. }  
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics