A classic Java applet clock program (i)

Source: Internet
Author: User
Tags abstract add date array cos net sin thread
Program

Blink of an eye a year again, oneself again old one year old, depressed ah. Take advantage of some Zhong Cai New year, and quickly send a few more articles to add something to the past year.

The program is found from the Internet and is a simple clock display program.

Code Features:
The clock code provides a variety of interfaces that can be set in HTML files, changing the colorful clock model,
Parameter description see inside the code info array

The clock drawing uses the double buffering graph processing mechanism, namely draws the graph in the buffer first, then displays the graph to the webpage to be able to prevent the flicker effectively.

In addition, the clock is constantly drawn through a thread to continuously read the system time, if the time has changed to draw. With one thread drawing, a thread processing Web page is more compliant with the Applet Setup specification.

Code section:

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

Class Hms extends Date
{
Public Hms (double localoffset) {//If the time zone is set in the HTML file, set it to the local time zone
Super ();
Long Tzoffset=gettimezoneoffset () *60l*1000l;
Localoffset *= 3600000.0;
SetTime (GetTime () + Tzoffset + (long) localoffset);
}

Public Hms () {//If no time zone is set, use local time
Super ();
}

Public double get_hours ()//Time conversion, converting a time like 5:30 to 5.5
{
Return (double) super.gethours () + (double) getminutes ()/60.0;
}
}

Abstract class Clockhand//Abstract classes that provide the use of hour, minute, and second seconds
{
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)//The drawing position of the hour hand, minute hand, and second hand, calculated by 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 used by the second hand
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)//initialization point
{
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)//Draw second hand, line segment
{
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,//initialization point
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)//Draw hand minute hand, for polygons
{
transform (angle);
G.setcolor (color);
G.fillpolygon (transx,transy,numberofpoints);
}
}

The HMS Class is a class for time conversion
Clockhand is the abstract base class for drawing pointers, while providing a way to convert angles to coordinates
Sweephand is the second hand drawing the class used
Hmhand is the hand hour hand drawing the class used

The main function looks at the following part


Blink of an eye a year again, oneself again old one year old, depressed ah. Take advantage of some Zhong Cai New year, and quickly send a few more articles to add something to the past year.

The program is found from the Internet and is a simple clock display program.

Code Features:
The clock code provides a variety of interfaces that can be set in HTML files, changing the colorful clock model,
Parameter description see inside the code info array

The clock drawing uses the double buffering graph processing mechanism, namely draws the graph in the buffer first, then displays the graph to the webpage to be able to prevent the flicker effectively.

In addition, the clock is constantly drawn through a thread to continuously read the system time, if the time has changed to draw. With one thread drawing, a thread processing Web page is more compliant with the Applet Setup specification.

Code section:

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

Class Hms extends Date
{
Public Hms (double localoffset) {//If the time zone is set in the HTML file, set it to the local time zone
Super ();
Long Tzoffset=gettimezoneoffset () *60l*1000l;
Localoffset *= 3600000.0;
SetTime (GetTime () + Tzoffset + (long) localoffset);
}

Public Hms () {//If no time zone is set, use local time
Super ();
}

Public double get_hours ()//Time conversion, converting a time like 5:30 to 5.5
{
Return (double) super.gethours () + (double) getminutes ()/60.0;
}
}

Abstract class Clockhand//Abstract classes that provide the use of hour, minute, and second seconds
{
protected int basex[], basey[];
protected int transx[],transy[];
protected int numberofpoints;

    public Clockhand (int originx, int originy, int length,int thickness,int points) {
  & nbsp;     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)//The drawing position of the hour hand, minute hand, and second hand, calculated by 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 used by the second hand
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)//initialization point
{
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)//Draw second hand, line segment
{
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,//initialization point
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)//Draw hand minute hand, for polygons
{
transform (angle);
G.setcolor (color);
G.fillpolygon (transx,transy,numberofpoints);
}
}

The HMS Class is a class for time conversion
Clockhand is the abstract base class for drawing pointers, while providing a way to convert angles to coordinates
Sweephand is the second hand drawing the class used
Hmhand is the hand hour hand drawing the class used

The main function looks at the following part



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.