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

📄 javaclock.java

📁 画面优美的时钟.(附详细代码)
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            {//机械表的界面
            
              //**********************分针************************
                double d = (double)Math.min(l, i1) * 0.75D;//定出分针的各个顶点
                double d1 = (double)Math.min(l, i1) * 0.040000000000000001D;
                double d2 = 3.1415926535897931D * ((double)j / 30D + (double)k / 1800D);
                xPoint[0] = (int)Math.round((double)l - 2D * d1 * Math.sin(d2)) - 1;
                xPoint[1] = (int)Math.round((double)l - d1 * Math.cos(d2));
                xPoint[2] = (int)Math.round((double)l + d * Math.sin(d2)) + 1;
                xPoint[3] = (int)Math.round((double)l + d1 * Math.cos(d2));
                yPoint[0] = (int)Math.round((double)i1 + 2D * d1 * Math.cos(d2)) - 1;
                yPoint[1] = (int)Math.round((double)i1 - d1 * Math.sin(d2));
                yPoint[2] = (int)Math.round((double)i1 - d * Math.cos(d2)) + 1;
                yPoint[3] = (int)Math.round((double)i1 + d1 * Math.sin(d2));
                                
                g1.setColor(mHandColor);//分针颜色
                g1.fillPolygon(xPoint, yPoint, 4);//以多半形的形式画出分针(常见的拉长菱形)
               
                if(j < 30)//下面的是根据分针在左边还是右边来画出边线,使有光照的效果
                {
                    g1.setColor(Color.white);
                } else
                {
                    g1.setColor(Color.black);
                }
                g1.drawLine(xPoint[0], yPoint[0], xPoint[1], yPoint[1]);
                g1.drawLine(xPoint[1], yPoint[1], xPoint[2], yPoint[2]);
                if(j < 30)
                {
                    g1.setColor(Color.black);
                } else
                {
                    g1.setColor(Color.white);
                }
                g1.drawLine(xPoint[2], yPoint[2], xPoint[3], yPoint[3]);
                g1.drawLine(xPoint[3], yPoint[3], xPoint[0], yPoint[0]);
                
                
              //*********************时针**********************
                //时针的各个顶点
                double d3 = 3.1415926535897931D * ((double)i / 6D + (double)j / 360D);
                d = (double)Math.min(l, i1) * 0.5D;
                d1 = (double)Math.min(l, i1) * 0.050000000000000003D;
                xPoint[0] = (int)Math.round((double)l - 2D * d1 * Math.sin(d3)) - 1;
                xPoint[1] = (int)Math.round((double)l - d1 * Math.cos(d3));
                xPoint[2] = (int)Math.round((double)l + d * Math.sin(d3)) + 1;
                xPoint[3] = (int)Math.round((double)l + d1 * Math.cos(d3));
                yPoint[0] = (int)Math.round((double)i1 + 2D * d1 * Math.cos(d3)) - 1;
                yPoint[1] = (int)Math.round((double)i1 - d1 * Math.sin(d3));
                yPoint[2] = (int)Math.round((double)i1 - d * Math.cos(d3)) + 1;
                yPoint[3] = (int)Math.round((double)i1 + d1 * Math.sin(d3));
                
                g1.setColor(hHandColor);//时针颜色
                g1.fillPolygon(xPoint, yPoint, 4);//画多边形的形式作出时针
               
                if(i >= 0 && i <= 6 || i >= 12 && i <= 18)//画出光照效果的边线
                {
                    g1.setColor(Color.white);
                } else
                {
                    g1.setColor(Color.black);
                }
                g1.drawLine(xPoint[0], yPoint[0], xPoint[1], yPoint[1]);
                g1.drawLine(xPoint[1], yPoint[1], xPoint[2], yPoint[2]);
                if(i >= 0 && i <= 6 || i >= 12 && i <= 18)
                {
                    g1.setColor(Color.black);
                } else
                {
                    g1.setColor(Color.white);
                }
                g1.drawLine(xPoint[2], yPoint[2], xPoint[3], yPoint[3]);
                g1.drawLine(xPoint[3], yPoint[3], xPoint[0], yPoint[0]);
                
                //********************秒针*********************
                d = (double)Math.min(l, i1) * 0.75D;
                g1.setColor(sHandColor);
                double d4 = (3.1415926535897931D * (double)k) / 30D;
                g1.drawLine(l, i1, (int)Math.round((double)l + d * Math.sin(d4)), (int)Math.round((double)i1 - d * Math.cos(d4)));
            }
            
            
            
             else
            {//电子表界面
                g1.setFont(fontText);
                g1.setColor(fontColor);
                String s = (i >= 10 ? Integer.toString(i) : "0" + i) 
                         + ":" + (j >= 10 ? Integer.toString(j) : "0" + j) + ":" 
                         + (k >= 10 ? Integer.toString(k) : "0" + k);
                FontMetrics fontmetrics = g1.getFontMetrics();
               
                int j1 = dimension.height >> 1;
                if(j1 < 0)
                {
                    j1 = 0;
                }
                int k1 = dimension.width - fontmetrics.stringWidth(s) >> 1;
                if(k1 < 0)
                {
                    k1 = 0;
                }
                g1.drawString(s, k1, j1);//以字符串的形式画出电子表的界面
            }
            
            oldHour = i;//将当前的时分秒值记录下来,为下一步的比较做准备
            oldMinute = j;
            oldSecond = k;
        }
        
        g.drawImage(imageBuffer, 0, 0, null);//更新背景
    }


//**********************************paint函数***********************************
    public void paint(Graphics g)
    {  
        Dimension dimension = size();
        if(tracker.isErrorAny())
        {
            g.setColor(Color.white);
            g.fillRect(0, 0, dimension.width, dimension.height);
            return;
        }
        if(tracker.checkAll(true))
        {
            DrawBackground(g);
        }
    }


//*******************************MouseDown时间******************************
    //更改isAnalog的值,就是机械表和电子表画面的转换标志 
    public boolean mouseDown(Event event, int i, int j)
    {
        isAnalog = !isAnalog;
        return true;
    }


//**************************DrawBackground函数*******************************  
    private void DrawBackground(Graphics g)
    {
        Dimension dimension = size();
        g.setColor(backColor);
        g.fillRect(0, 0, dimension.width, dimension.height);
        if(backImage != null)
        {//有背景图则画出背景图
            int i = backImage.getWidth(this);
            int k = backImage.getHeight(this);
            if(i < 0 || k < 0)
            {
                return;
            }
            g.drawImage(backImage, dimension.width - i >> 1, dimension.height - k >> 1, null);
        }
        if(isAnalog)
        {//如果isAnalog为真,即是电子表界面,则画出电子表界面:表示"时分"的点
            int j = dimension.width >> 1;
            int l = dimension.height >> 1;
            double d = (double)Math.min(j, l) * 0.90000000000000002D;
            for(int i1 = 1; i1 <= 12; i1++)
            {//"时"刻度点
                double d1 = 3.1415926535897931D * (0.5D - (double)i1 / 6D);
                int k1 = (int)Math.floor((double)j + d * Math.cos(d1));
                int l1 = (int)Math.floor((double)l - d * Math.sin(d1));
                g.setColor(hPointColor);
                g.fill3DRect(k1 - 2, l1 - 2, 4, 4, true);
            }

            for(int j1 = 1; j1 <= 60; j1++)
            {//"分"刻度点
                if(j1 % 5 != 0)
                {
                    double d2 = (3.1415926535897931D * (double)j1) / 30D;
                    int i2 = (int)Math.floor((double)j + d * Math.cos(d2));
                    int j2 = (int)Math.floor((double)l - d * Math.sin(d2));
                    g.setColor(mPointColor);
                    g.fill3DRect(i2 - 2, j2 - 2, 3, 3, false);
                }
            }

        }
    }

   public JavaClock()
    {//默认值
        isAnalog = true;
        fontColor = Color.black;
        backColor = Color.lightGray;
        hHandColor = Color.blue;
        mHandColor = Color.blue;
        sHandColor = Color.black;
        hPointColor = Color.red;
        mPointColor = Color.lightGray;
        xPoint = new int[4];
        yPoint = new int[4];
        oldHour = -1;
        oldMinute = -1;
        oldSecond = -1;
    }

    static 
    {//构造颜色的函数
        colors = (new Object[][] {
            new Object[] {
                "BLACK", Color.black
            }, new Object[] {
                "BLUE", Color.blue
            }, new Object[] {
                "CYAN", Color.cyan
            }, new Object[] {
                "DARKGRAY", Color.darkGray
            }, new Object[] {
                "GRAY", Color.gray
            }, new Object[] {
                "GREEN", Color.green
            }, new Object[] {
                "LIGHTGRAY", Color.lightGray
            }, new Object[] {
                "MAGENTA", Color.magenta
            }, new Object[] {
                "ORANGE", Color.orange
            }, new Object[] {
                "PINK", Color.pink
            }, new Object[] {
                "RED", Color.red
            }, new Object[] {
                "WHITE", Color.white
            }, new Object[] {
                "YELLOW", Color.yellow
            }
        });
    }
}

⌨️ 快捷键说明

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