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

📄 javaclock.java

📁 画面优美的时钟.(附详细代码)
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//JavaClock.java

import java.applet.Applet;
import java.awt.*;
import java.util.Date;

public class JavaClock extends Applet
                       implements Runnable
{
//*************************定义变量*****************************
    static final Object colors[][];
    static final Object cityZone[][] = {
        {
            "Hawaii", "-10"
        }, {
            "Alaska", "-9"
        }, {
            "Arizona", "-7"
        }, {
            "Mexico", "-6"
        }, {
            "Brasilia", "-3"
        }, {
            "GMT", "0"
        }, {
            "Paris", "1"
        }, {
            "Cairo", "2"
        }, {
            "Moscow", "3"
        }, {
            "Jakarta", "7"
        }, {
            "Hong Kong", "8"
        }, {
            "Tokyo", "9"
        }, {
            "Sydney", "10"
        }
    };
    Thread threadClock;
    boolean isAnalog;
    Font fontText;
    Color fontColor;
    Color backColor;
    Color hHandColor;
    Color mHandColor;
    Color sHandColor;
    Color hPointColor;
    Color mPointColor;
    int xPoint[];
    int yPoint[];
    Image backImage;
    MediaTracker tracker;
    Image imageBuffer;
    int fromGMT;
    int currentZone;
    int oldHour;
    int oldMinute;
    int oldSecond;
    
//*****************************************************************
    public void setCityZone(String s)
    {//由得到的地区参数设置currentZone的值
        currentZone = fromGMT;
        for(int i = 0; i < cityZone.length; i++)
        {
            if(s.indexOf((String)cityZone[i][0]) != -1)
            {
                currentZone = Integer.parseInt((String)cityZone[i][1]);
            }
        }

    }

//*****************************************************************
    private Color findColor(String s, Color color)
    {//用同一函数处理从html文件得到的各个部分的颜色
     //包括前景色,背景色,时分秒的指针颜色,表盘上的时刻指示点
        if(s != null)
        {
            s = s.toUpperCase();
            if(s.charAt(0) == '#')
            {
                return new Color(Integer.parseInt(s.substring(1), 16));
            }
            for(int i = 0; i < colors.length; i++)
            {
                if(s.compareTo((String)colors[i][0]) == 0)
                {
                    return (Color)colors[i][1];
                }
            }

        }
        return color;
    }
    
//*****************************************************************
    public void init()
    {//得到并处理参数,分别设置字体,大小,各个部分的颜色
     //包括"前景色,背景色,时分秒的指针颜色,表盘上的时刻指示点"
     //并初始化日期时间,isAnalog等值 
        String s = getParameter("typeface");
        if(s == null)
        {
            s = "Helvetica";
        }
        
        int i;
        try
        {
            i = Integer.parseInt(getParameter("fontsize"), 10);
        }
        catch(NumberFormatException _ex)
        {
            i = 16;
        }
        fontText = new Font(s, 0, i);
        
        backColor = findColor(getParameter("backcolor"), backColor);
        fontColor = findColor(getParameter("fontcolor"), fontColor);
        hHandColor = findColor(getParameter("hhandcolor"), hHandColor);
        mHandColor = findColor(getParameter("mhandcolor"), mHandColor);
        sHandColor = findColor(getParameter("shandcolor"), sHandColor);
        hPointColor = findColor(getParameter("hpointcolor"), hPointColor);
        mPointColor = findColor(getParameter("mpointcolor"), mPointColor);
        
        tracker = new MediaTracker(this);
        
        s = getParameter("backimage");
        if(s != null)
        {
            backImage = getImage(getCodeBase(), s);
            tracker.addImage(backImage, 0);
        } else
        {
            backImage = null;
        }
        
        s = getParameter("analog");
        if(s != null && s.indexOf("false") > -1)
        {
            isAnalog = false;
        } else
        {
            isAnalog = true;
        }
        
        Date date = new Date();
        fromGMT = -Math.round((float)date.getTimezoneOffset() / 60F);
        currentZone = fromGMT;
    }


//********************************************************************* 
    public void start()
    {
        if(imageBuffer == null)//画背景图
        {
            Dimension dimension = size();
            imageBuffer = createImage(dimension.width, dimension.height);
        }
        if(threadClock == null)//开始线程
        {
            threadClock = new Thread(this);
            threadClock.start();
        }
    }
    
    
//**********************************************************************
    public void stop()
    {//停止
        if(threadClock != null)
        {
            threadClock.stop();
            threadClock = null;
            imageBuffer = null;
        }
    }


//**********************************************************************
    public void run()
    {//运行线程,如果当前线程是threadClock,repaint,等待50L的时间,返回
        try
        {
            tracker.waitForAll();
        }
        catch(InterruptedException _ex)
        {
            return;
        }
        while(Thread.currentThread() == threadClock) 
        {
            repaint();
            try
            {
                Thread.sleep(50L);
            }
            catch(InterruptedException _ex)
            {
                return;
            }
        }
    }


//**********************************************************************
    public void update(Graphics g)
    {//更新画面
        Date date = new Date();
        
        int i = (date.getHours() + currentZone) - fromGMT;//以24h制表示的时
        if(i >= 24)
        {
            i -= 24;
        }
        if(i < 0)
        {
            i += 24;
        }
        
        int j = date.getMinutes();//分
        int k = date.getSeconds();//秒
        
        if(i != oldHour || j != oldMinute || k != oldSecond)
        {//如果当前时分秒的值和前记录值不同,则重新画时钟界面
            Graphics g1 = imageBuffer.getGraphics();
            DrawBackground(g1);
            Dimension dimension = size();
            int l = dimension.width >> 1;
            int i1 = dimension.height >> 1;
            
            if(isAnalog)

⌨️ 快捷键说明

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