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

📄 billsclock.java

📁 一个简单的APPLET时钟程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * BillsClock.java - 27 Nov 1998 - Version 1.05
 *(formerly javex.java)
 *
 * Copyright 1996-98 by Bill Giel
 *
 * E-mail: bgiel@ct2.nai.net
 * WWW: http://www.nai.net/~rvdi/~bgiel/bill.htm
 *
 *
 * Revision 1.01 - revised hms class to calculate GMT. This permits the clock
 * 10 Feb 96       to be used to display the time at any location by supplying
 *                 a TIMEZONE parameter in the HTML call.
 *
 * Revision 1.02 - revised timezone to accept real numbers, for places like
 * 11 Feb 96       India, with a 5.5 hour time difference. I learn something
 *                 new everyday!
 *
 * Revision 1.03 - fixed loop in run() to exit if clockThread==null, rather
 *                 than simple for(;;)
 *
 * Revision 1.04 - renamed file and applet class to billsClock; added
 * 12 Jun 96       parameter LOCALONLY for displaying viewer's local time
 *
 * Revision 1.05 - changed TZ algebraic sign to conform with astronomic convention
 * 28 Nov 98       
 *
 *
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
 * without fee is hereby granted, provided that any use properly credits
 * the author, i.e. "Bill's Clock courtesy of <A HREF="mailto:bgiel@ct2.nai.net">
 * Moondog Software</A>.
 *
 *
 * THE AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
 * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE
 * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 *
 */

import java.awt.*;
import java.applet.*;
import java.util.*;
import java.net.*;

class Hms extends Date
{
    //Note that localOffset is hours difference from GMT
    //west of Greenwich meridian is negative, east is positive.
    //i.e. New York City (Eastern Standard Time) is -5
    //     Eastern Daylight Time is -4

    public Hms(double localOffset){
        super();
        long tzOffset=getTimezoneOffset()*60L*1000L;
        localOffset *= 3600000.0;
        setTime(getTime() + tzOffset + (long)localOffset);
    }

    public Hms(){
        super();
    }

    public double get_hours()
    {
        return (double)super.getHours()+(double)getMinutes()/60.0;
    }
}

abstract class ClockHand
{
    protected int baseX[], baseY[];
    protected int transX[],transY[];
    protected int numberOfPoints;

    public ClockHand(int originX, int originY, int length,int thickness,int points){
        baseX= new int[points]; baseY=new int[points];
        transX= new int[points]; transY=new int[points];
        initiallizePoints(originX,originY,length,thickness);
        numberOfPoints=points;
    }

    abstract protected void initiallizePoints(  int originX,
                                                int originY,
                                                int length,
                                                int thickness);

    abstract public void draw(Color color, double angle, Graphics g);

    protected void transform(double angle)
    {
        for(int i=0;i<numberOfPoints;i++){
            transX[i]=(int)(    (baseX[0]-baseX[i]) * Math.cos(angle) -
                                (baseY[0]-baseY[i]) * Math.sin(angle) +
                                 baseX[0]);

            transY[i]=(int)(    (baseX[0]-baseX[i]) * Math.sin(angle) +
                                (baseY[0]-baseY[i]) * Math.cos(angle) +
                                 baseY[0]);
        }
    }
}

class SweepHand extends ClockHand
{
    public SweepHand(int originX,int originY, int length, int points)
    {
        super(originX,originY,length,0,points);
	}

    protected void initiallizePoints(int originX,int originY, int length, int unused)
    {
        unused=unused;  //We don't use the thickness parameter in this class
                        //This comes from habit to prevent compiler warning
                        //concerning unused arguments.

        baseX[0]=originX; baseY[0]=originY;
        baseX[1]=originX; baseY[1]=originY-length/5;
        baseX[2]=originX; baseY[2]=originY+length;
    }

    public void draw(Color color, double angle, Graphics g)
    {
        transform(angle);
        g.setColor(color);
        g.drawLine(transX[1],transY[1],transX[2],transY[2]);
    }
}

class HmHand extends ClockHand
{
    public HmHand(int originX,int originY, int length,int thickness, int points){
        super(originX,originY,length,thickness,points);
    }

    protected void initiallizePoints(   int originX,
                                        int originY,
                                        int length,
                                        int thickness)
    {
        baseX[0]=originX;
        baseY[0]=originY;

        baseX[1]=baseX[0]-thickness/2;
        baseY[1]=baseY[0]+thickness/2;

        baseX[2]=baseX[1];
        baseY[2]=baseY[0]+length- thickness;

        baseX[3]=baseX[0];
        baseY[3]=baseY[0]+length;

        baseX[4]=baseX[0]+thickness/2;
        baseY[4]=baseY[2];

        baseX[5]=baseX[4];
        baseY[5]=baseY[1];
    }

    public void draw(Color color,double angle, Graphics g)
    {
        transform(angle);
        g.setColor(color);
        g.fillPolygon(transX,transY,numberOfPoints);
    }
}

public class BillsClock extends Applet implements Runnable
{
    //some DEFINE'd constants
    static final int BACKGROUND=0;              //Background image index
    static final int LOGO=1;                    //Logo image index
    static final String JAVEX="J***X";          //Default text on clock face
    static final double MINSEC=0.104719755;     //Radians per minute or second
    static final double HOUR=0.523598776;       //Radians per hour

    Thread clockThread = null;

    //User options, see getParameterInfo(), below.
    int width = 100;
    int height = 100;
    Color bgColor = new Color(0,0,0);
    Color faceColor = new Color(0,0,0);
    Color sweepColor = new Color(255,0,0);
    Color minuteColor = new Color (192,192,192);
    Color hourColor = new Color (255,255,255);
    Color textColor = new Color (255,255,255);
    Color caseColor = new Color (0,0,0);
    Color trimColor = new Color (192,192,192);
    String logoString=null;

    Image images[] = new Image[2]; //Array to hold optional images

    boolean isPainted=false; //Force painting on first update, if not painted

    //Center point of clock
    int x1,y1;

    //Array of points for triangular icon at 12:00
    int xPoints[]=new int[3], yPoints[]=new int[3];

    //Class to hold time, with method to return (double)(hours + minutes/60)
    Hms cur_time;

    //The clock's seconds, minutes, and hours hands.
    SweepHand sweep;
    HmHand  minuteHand,
            hourHand;

    //The last parameters used to draw the hands.
    double lastHour;
    int lastMinute,lastSecond;

    //The font used for text and date.
    Font font;

    //Offscreen image and device context, for buffered output.
    Image offScrImage;
    Graphics offScrGC;

    // Use to test background image, if any.
    MediaTracker tracker;


    int minDimension;   // Ensure a round clock if applet is not square.
    int originX;        // Upper left corner of a square enclosing the clock
    int originY;        // with respect to applet area.

    double tzDifference=0;

    boolean localOnly=false;


    //Users' parameters - self-explanatory?
    public String[][] getParameterInfo()
    {
        String[][] info = {
            {"width",       "int",      "width of the applet, in pixels"},
            {"height",      "int",      "height of the applet, in pixels"},
            {"bgColor",     "string",   "hex color triplet of the background, i.e. 000000 for black <black>"},
            {"faceColor",   "string",   "hex color triplet of clock face, i.e. 000000 for black <black>"},
            {"sweepColor",  "string",   "hex color triplet of seconds hand, i.e. FF0000 for red <red>"},
            {"minuteColor", "string",   "hex color triplet of minutes hand, i.e. C0C0C0 for lt.gray <lt.gray>"},
            {"hourColor",   "string",   "hex color triplet of hours hand, i.e. FFFFFF for white <white>"},
            {"textColor",   "string",   "hex color triplet of numbers, etc., i.e. FFFFFF for white <white>"},
            {"caseColor",   "string",   "hex color triplet of case, i.e. 000000 for black <black>"},
            {"trimColor",   "string",   "hex color triplet of case outliners, i.e. C0C0C0 for lt.gray <lt.gray>"},
            {"bgImageURL",  "string",   "URL of background image, if any <null>"},
            {"logoString",  "string",   "Name to display on watch face <JAVEX>"},
            {"logoImageURL","string",   "URL of logo image to display on watch face <null>"},
            {"timezone",    "real",     "Timezone difference from GMT (decimal hours,- West/+ East)<0>"},
            {"localonly",   "int",      "Non-zero will cause clock to display current local time <0>"}
        };
        return info;
    }

    //Applet name, author and info lines
    public String getAppletInfo()
    {
        return "billsClock 1.05 (C) 1996-98 by Bill Giel<bgiel@ct2.nai.net>";
    }

    void showURLerror(Exception e)
    {
        String errorMsg = "JAVEX URL error: "+e;
        showStatus(errorMsg);
        System.err.println(errorMsg);
    }

    // This lets us create clocks of various sizes, but with the same
    // proportions.
    private int size(int percent)
    {
        return (int)((double)percent/100.0 * (double)minDimension);
    }

    public void init()
    {
        URL imagesURL[] = new URL[2];
        String szImagesURL[] = new String[2];
        tracker = new MediaTracker(this);

        String paramString    = getParameter( "WIDTH"  );
        if( paramString != null )
            width = Integer.valueOf(paramString).intValue();

⌨️ 快捷键说明

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