zoukankan      html  css  js  c++  java
  • 一个java 线程 的小例子

    import java.applet.*;
    import java.awt.*;
    
    public class WalkingText extends Applet implements Runnable {
       protected String mesg = null;
       protected int  xloc, yloc, width, height, textWidth, textHeight;
       protected Thread t;
       protected boolean done = false;
       /** How long to nap for each move */
       protected int napTime = 150;
    
       /** Applet Initializer */
       public void init() {
          xloc = 0;
          
          //获得屏幕的大小
          width = getSize().width;
          height = getSize().height;
    
          if ((mesg = getParameter("text")) == null)
             mesg = "Hello World of Java";
    
          String pSize = getParameter("fontsize");
          if (pSize == null)
             pSize = "12";
    
          String fontName = getParameter("fontName");
          if (fontName == null)
             fontName = "Helvetica";
    
          // System.out.println("Font is " + pSize + " point " + fontName);
          Font f = new Font(fontName, Font.PLAIN, Integer.parseInt(pSize));
          setFont(f);
    
          FontMetrics fm = getToolkit().getFontMetrics(f);
          textWidth = fm.stringWidth(mesg);
          textHeight = fm.getHeight();
         
    
          // use textHeight in y coordinate calculation
          yloc = height - ((height-textHeight) / 2);
       }
    
       /** This is important: we create a thread, so we must kill it */
       public void stop() {
          done = true;
          t = null;
       }
    
       /** create the thread and start it. */
       public void start() {
          if (t == null)
             t = new Thread(this);
          done = false;
          t.start();
       }
    
       // Usage:
       public String[][] getParameterInfo() {
          String[][] params = {
             { "text", "text", "Text to walk across the screen" },
             { "fontName", "text", "Name of font to display in" },
             { "fontsize", "int", "How big to make the text" },
          };
    
          return params;
       }
    
    
       /** Run is called by the Thread class when there is work to do */
       public void run() {
          while (!done) {
             if ((xloc+=5) > getSize().width)
                xloc = 0;
             repaint();
             try {
                Thread.sleep(napTime);
             } catch (Exception e) {
                System.out.println("Who dares to interrupt my Sleep()? " + e);
             };
          }
       }
    
       /** Paint is called by Applet to redraw the screen */
       public void paint(Graphics g) {
          g.drawString(mesg, xloc, yloc);
       }
    }
    

    ==============================================================================

    本博客已经废弃,不在维护。新博客地址:http://wenchao.ren


    我喜欢程序员,他们单纯、固执、容易体会到成就感;面对压力,能够挑灯夜战不眠不休;面对困难,能够迎难而上挑战自我。他
    们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想“用智慧开创属于自己的事业”。我想说的是,其
    实我是一个程序员

    ==============================================================================
  • 相关阅读:
    MySQL 主主同步配置和主从配置步骤
    Nginx 禁止访问某个目录或文件的设置方法
    LNMP笔记:更改网站文件和MySQL数据库的存放目录
    centos最小安装 setuptools安装
    mkdir命令
    linux的mount(挂载)命令详解
    Java中wait()和notify()方法的使用
    剑指 offer面试题22 栈的压入和弹出序列
    剑指 offer面试题20 顺时针打印矩阵
    Leetcode 23.Merge Two Sorted Lists Merge K Sorted Lists
  • 原文地址:https://www.cnblogs.com/rollenholt/p/2061417.html
Copyright © 2011-2022 走看看