⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stayed.java

📁 Java 范例实战 光盘使用说明 ========================== 本光盘的文件结构如下所示: =====================================
💻 JAVA
字号:
import java.applet.Applet;
import java.applet.AppletContext;
import java.awt.*;
import java.util.Hashtable;

public class Stayed extends Applet implements Runnable {
    Font theFont = null;
    Image bufferImg = null;
    Graphics bufferG = null;
    FontMetrics fontM = null;
    Dimension d = null;
    Thread stayThread = null;
    String msgText = null;
    Color backGround = null,
          foreGround = null,
          border = null;
    int hour = 0,
        minute = 0,
        second = 0;

    public Stayed()
    {
    }

    public Color RGB_Color(String s, Color c)
    {
        Color color;
        
        try
        {
            color = new Color(Integer.parseInt(s, 16));
        }
        catch(NumberFormatException _ex)
        {
            color = c;
        }
        
        return color;
    }

    public void init()
    {
        String s, s1;
        byte fontStyle = 0;
        int fontSize;

        if((s = getParameter("fontName")) == null)
            s = "TimesRoman";
            
        try
        {
            fontSize = Integer.parseInt(getParameter("fontSize"));
        }
        catch(NumberFormatException _ex)
        {
            fontSize = 12;
        }
        
        if ((s1 = getParameter("fontStyle")) != null) {
        
            if (s1.indexOf("B") != -1)
                fontStyle |= 1;
        
            if (s1.indexOf("I") != -1)
                fontStyle |= 2;
        
        }
        
        theFont = new Font(s, fontStyle, fontSize);
        fontM = getFontMetrics(theFont);
        foreGround = RGB_Color(getParameter("foreColor"),
                               Color.lightGray);
        backGround = RGB_Color(getParameter("backColor"),
                               Color.black);
        border = RGB_Color(getParameter("borderColor"),
                               Color.red);
        d = getSize();
        
        try
        {
            bufferImg = createImage(d.width, d.height);
            bufferG = bufferImg.getGraphics();
        }
        catch(Exception _ex) {}
        
    }

    public void start()
    {
        
        if(stayThread == null)
        {
            stayThread = new Thread(this);
            stayThread.start();
        }
        
    }

    public void stop()
    {
        
        if(stayThread != null)
            stayThread.stop();
        
        stayThread = null;
    }

    public String computeTime()
    {
        String s, s1;

        second++;
        
        if(second >= 60)
        {
            second = 0;
            minute++;
        }
        
        if(second < 10)
            s1 = "0"+second;
        else
            s1 = Integer.toString(second);
        
        if(minute >= 60)
        {
            minute = 0;
            hour++;
        }
        
        if(minute < 10)
            s = "0"+minute;
        else
            s = Integer.toString(minute);
        
        return hour + " hr " + s + " min " + s1 + " sec";
    }

    public void run()
    {
        
        do
        {
            msgText = computeTime();
            repaint();
        
            try
            {
                Thread.sleep(1000L);
            }
            catch(InterruptedException _ex) { }
        
        }
        while(true);
        
    }

    public void paint(Graphics g)
    {
        bufferG.setFont(theFont);
        bufferG.setColor(backGround);
        bufferG.fillRect(0, 0, d.width, d.height);
        bufferG.setColor(border);
        bufferG.drawRect(0, 0, d.width - 1, d.height - 1);
        bufferG.setColor(foreGround);
        int i = (d.width - fontM.stringWidth(msgText)) / 2;
        bufferG.drawString(msgText, i, 12);
        g.drawImage(bufferImg, 0, 0, null);
    }

    public void update(Graphics g)
    {
        paint(g);
    }
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -