A classic Java applet program (ii)

Source: Internet
Author: User
Tags array final html page int size sin thread time interval tostring
The program goes on to the above section, following is the applet main program section:


public class Myclock extends Applet implements Runnable
{
static final int background=0; The serial number of the background image for use in the array
static final int logo=1; The serial number of the logo image for use in the array
Static final String javex= "Bear"; The text displayed on the dial
static final double minsec=0.104719755; The radian between the minute hand and the second hand on the dial (60).
static final double hour=0.523598776; The radian between the Dials (24) of the clock.

Thread clockthread = null; Use multithreading to constantly display pictures with another thread

The default parameter provided, if the HTML file contains no parameters to use
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]; Picture of background and logo

Boolean ispainted=false; If the background and the dial are drawn when the first time is loaded, the other time the redraw draws only the pointer

The position of the center of the clock
int x1,y1;

The coordinates of the tick shape of the top triangle
int xpoints[]=new int[3], ypoints[]=new int[3];

Save current time, convert to (double) (Hours + minutes/60)
Hms Cur_time;

Seconds, minute, hour
Sweephand sweep;
Hmhand Minutehand,
Hourhand;

Time, minutes, seconds for drawing
Double Lasthour;
int Lastminute,lastsecond;

The font that shows the letters on the date and the dial
Font font;

Image display using buffering mechanism, Offscrimage and OFFSCRGC store the information of the picture inside the buffer
Image Offscrimage;
Graphics OFFSCRGC;

For testing background pictures and logo pictures
Mediatracker Tracker;


int mindimension; If the background area is not square, make sure the clock appears in the middle.
int Originx; The upper-left corner of the square area where the clock graph is located
int originy; The y-coordinate of the upper-left corner of the square area where the clock graphic is

Double tzdifference=0; The difference between the time interval, the West is negative, the east is positive

Boolean localonly=false; Whether to use local time only, or false to display the time zone according to the time zone passed in


Save the type description of the parameter
Public string[][] GetParameterInfo ()
{
String[][] Info = {
{"width", "int", "Length of applet, in Pixels"},
{"Height", "int", "applet width, in pixels"},
{"bgcolor", "string", "background color, e.g FF0000"},
{"Facecolor", "string", "Dial Color"},
{"Sweepcolor", "string", "Seconds Color"},
{"Minutecolor", "string", "Minute Hand Color"},
{"Hourcolor", "string", "Clockwise Color"},
{"TextColor", "string", "Text Color"},
{"Casecolor", "string", "Color In Box"},
{"Trimcolor", "string", "White Space Color"},
{"Bgimageurl", "string", "Background picture Address"},
{"Logostring", "string", "logo character"},
{"Logoimageurl", "string", "Logo picture Address"},
{"TimeZone", "real", "difference of time interval"},
{"Localonly", "int", "whether to consider time zone differences"}
};
return info;
}

Display information
Public String Getappletinfo ()
{
Return "All rights reserved, copy must investigate, protect genuine, everyone is responsible";
}

void Showurlerror (Exception e)
{
String errormsg = "URL error:" +E;
Showstatus (ERRORMSG);
System.err.println (ERRORMSG);
}

Equivalent to turning the clock into a 100x100, percent is the relative coordinates.
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);

Get the parameters provided by the HTML page and convert it to the appropriate format
String paramstring = getparameter ("WIDTH");
if (paramstring!= null)
width = integer.valueof (paramstring). Intvalue ();

paramstring = GetParameter ("HEIGHT");
if (paramstring!= null)
Height = integer.valueof (paramstring). Intvalue ();

paramstring = GetParameter ("TIMEZONE");
if (paramstring!= null)
Tzdifference = double.valueof (paramstring). Doublevalue ();

paramstring = GetParameter ("localonly");
if (paramstring!= null && integer.valueof (paramstring). Intvalue ()!= 0) {
Localonly=true;
Tzdifference=0.;
}

paramstring = GetParameter ("bgcolor");
if (paramstring!= null)
Bgcolor=parsecolorstring (paramstring);

paramstring = GetParameter ("Facecolor");
if (paramstring!= null)
Facecolor=parsecolorstring (paramstring);

paramstring = GetParameter ("Sweepcolor");
if (paramstring!= null)
Sweepcolor=parsecolorstring (paramstring);

paramstring = GetParameter ("Minutecolor");
if (paramstring!= null)
Minutecolor=parsecolorstring (paramstring);

paramstring = GetParameter ("Hourcolor");
if (paramstring!= null)
Hourcolor=parsecolorstring (paramstring);

paramstring = GetParameter ("TextColor");
if (paramstring!= null)
Textcolor=parsecolorstring (paramstring);

paramstring = GetParameter ("Casecolor");
if (paramstring!= null)
Casecolor=parsecolorstring (paramstring);

paramstring = GetParameter ("Trimcolor");
if (paramstring!= null)
Trimcolor=parsecolorstring (paramstring);

logostring = GetParameter ("logostring");
if (logostring = null)
Logostring=javex;
else if (Logostring.length () > 8)
logostring= logostring.substring (0,8); Limit 8 letters, otherwise it won't show up!

The Szimagesurl array holds the file name of the picture based on the HTML incoming parameter
Szimagesurl[background] = getparameter ("Bgimageurl");
Szimagesurl[logo] = getparameter ("Logoimageurl");

Test picture
for (int i=0; i<2; i++) {
if (Szimagesurl[i]!= null) {
According to the CODEBASE parameter and image filename to get the path of image, the test exists, if it does not exist without pictures
try{
Imagesurl[i]=new URL (GetCodeBase (), szimagesurl[i]);
catch (Malformedurlexception e)
{
Showurlerror (e);
Imagesurl[i]=null;
Images[i]=null;
}
if (Imagesurl[i]!= null) {
Showstatus ("Load Picture:" + imagesurl[i].tostring ());
Images[i]=getimage (Imagesurl[i]);
if (Images[i]!= null)
Tracker.addimage (Images[i],i);
Showstatus ("");
}
try{
Tracker.waitforall (i);
}catch (Interruptedexception e) {}
}
else Images[i]=null;
}

Get time in the appropriate time zone
Cur_time= (localonly)? New HMS (): New HMS (Tzdifference);
lasthour=-1.0;
Lastminute=-1;
Lastsecond=-1;

X1=WIDTH/2;
Y1=HEIGHT/2;

Converts a circular clock to the upper-left corner of the background
mindimension= math.min (width, height);
originx= (width-mindimension)/2;
originy= (height-mindimension)/2;

Calculate the coordinates of the three dots above the triangle.
Xpoints[1]=x1-size (3); Xpoints[2]=x1+size (3); xpoints[0]=x1;
Ypoints[1]=y1-size (Ypoints[2]=y1-size); (38); Ypoints[0]=y1-size (27);

Calculates the point coordinates of the second hand, the minute hand, and the clockwise figure
Sweep=new Sweephand (X1,y1,size (40), 3);
Minutehand=new Hmhand (x1,y1,size), size (6), 6);
Hourhand=new Hmhand (x1,y1,size), size (8), 6);

Construct font
Font=new Font ("TXT", Font.bold,size (10));

To construct a graphic within a buffer
Offscrimage = CreateImage (width,height);
OFFSCRGC = Offscrimage.getgraphics ();

System.out.println (Getappletinfo ());
}

public void Start ()//Start display thread
{
if (Clockthread = = null) {
Clockthread = new Thread (this);
}
Clockthread.start ();
}

public void Stop ()/stop display
{
Clockthread = null;
}

private void Drawhands (Graphics g)
{

Double angle;
int i,j;
int x,y;

Angle=minsec * Lastsecond; Calculate the angle of the second hand relative to the 12-point scale according to Lastsecond
Sweep.draw (Facecolor, Angle, g);/Draw the second hand

Re-draw the hour hand if the time and the minute change
if (Cur_time.getminutes ()!= lastminute) {
Minutehand.draw (FACECOLOR,MINSEC*LASTMINUTE,G);
if (cur_time.get_hours ()!= lasthour)
Hourhand.draw (FACECOLOR,HOUR*LASTHOUR,G);
}

G.setcolor (TextColor);
G.fillrect (Originx+size), Y1-size (2), size (a), size (4)); Draw left horizontal bar
G.fillrect (X1-size (2), Originy + mindimension-size (), Size (4), size (10)); Draw the horizontal bar below
G.fillpolygon (Xpoints, ypoints, 3); Draw the top triangle
for (i=1;i<12;i+=3)/////Draw circle in 2, 3, 5, 6, 8, 9, 11 clockwise Dial
for (j=i;j<i+2;j++) {
x= (int) (X1+math.sin (hour*j) *size (35));
y= (int) (Y1-math.cos (hour*j) *size (35));
G.filloval (X-size (3), Y-size (3), size (6), size (6));
}

Set font
G.setfont (font);
FontMetrics Fm=g.getfontmetrics ();

Draw the logo string at the top
g.DrawString (Logostring,x1-fm.stringwidth (logostring)/2,y1-size (12));

Get the date
String day=integer.tostring (Cur_time.getdate (), 10);

Draw the date to the right of the dial
g.DrawString (Day,
Originx + mindimension-size ()-fm.stringwidth (day),
Y1+size (5));

Draw a frame outside.
G.drawrect (Originx + mindimension-size ()-fm.stringwidth (Day)-size (2),
Y1-size (5)-size (2),
Fm.stringwidth (Day) +size (4),
Size (a) +size (4));

if (Images[logo]!= null) {//If the LOGO picture exists, draw it at the bottom
x = Originx + (Mindimension-images[logo].getwidth (this))/2;
y = y1 + (MINDIMENSION/2-size ()-images[logo].getheight (This))/2;
if (x > 0 && y > 0)
Offscrgc.drawimage (Images[logo], x, y, this);
}

Lasthour=cur_time.get_hours ();
Hourhand.draw (HOURCOLOR,HOUR*LASTHOUR,G); Draw the hour hand

Lastminute=cur_time.getminutes ();
Minutehand.draw (MINUTECOLOR,MINSEC*LASTMINUTE,G); Draw the minute hand

G.setcolor (Minutecolor); Draw a circle at the tail of the minute
G.filloval (X1-size (4), Y1-size (4), size (8), size (8));
G.setcolor (Sweepcolor); Draw a circle at the tail of the second hand
G.filloval (X1-size (3), Y1-size (3), size (6), size (6));

Lastsecond=cur_time.getseconds (); Get a new number of seconds, repaint
Angle=minsec*lastsecond;
Sweep.draw (Sweepcolor, angle,g);

G.setcolor (Trimcolor);
G.filloval (X1-size (1), y1-size (1), size (2), size (2)); The center part of the tail of the second hand should be a screw, knockout treatment ^_^
}

Private Color parsecolorstring (string colorstring)//parameter passed in as String (16 in 6-bit string specified)
{
if (Colorstring.length () ==6) {
int R = integer.valueof (colorstring.substring (0,2),). Intvalue (); The first two bits are R values
int G = integer.valueof (colorstring.substring (2,4),). Intvalue (); Middle is G value
int B = integer.valueof (colorstring.substring (4,6),). Intvalue (); The last value is B
return new Color (R,G,B); Get color
}
else return color.lightgray; string does not conform to specification, default is gray
}

public void Run ()
{
Repaint (); First redraw each time you start
Get now time every 500ms and redraw once
while (null!= clockthread) {
Cur_time= (localonly)? New HMS (): New HMS (Tzdifference);
Repaint ();

try{
Thread.Sleep (500);
catch (Interruptedexception e) {}
}
}

public void Paint (Graphics g)//first draw the picture in the buffer and show it again
{
int i,x0,y0,x2,y2;

If a background picture is not provided, draw the background with bgcolor
if (images[background] = = null) {
Offscrgc.setcolor (bgcolor);
Offscrgc.fillrect (0,0,width,height);
}
else//otherwise use the background picture directly
Offscrgc.drawimage (Images[background], 0, 0, this);

Draw the outer frame to the part between the dials
Offscrgc.setcolor (Casecolor);

Reduce the scope of the garden in moderation (not filling the whole area) to prevent some places from being intercepted
Offscrgc.filloval (Originx+1,
Originy+1,
MinDimension-2,
MINDIMENSION-2);

Draw a Dial
Offscrgc.setcolor (Facecolor);
Offscrgc.filloval (Originx + size (5),
Originy + size (5),
Mindimension-size (10),
Mindimension-size (10));

Draw outer Border
Offscrgc.setcolor (Trimcolor);
Offscrgc.drawoval (Originx+1,
Originy+1,
MinDimension-2,
MINDIMENSION-2);

Draw an inner frame line
Offscrgc.drawoval (Originx + size (5),
Originy + size (5),
Mindimension-size (10),
Mindimension-size (10));

Offscrgc.setcolor (TextColor);

Draw scale, a total of 60 ticks, x0, y0 for the start of the tick position, X1, y1 center position, x2, y2 for the tick-stop position (X0&LT;X2,Y0&LT;Y2)
for (i=0;i<60;i++) {
if (i==0 | | (i>=5 && i%5 = 0)) {////each 5-grid draw a long line (relative center)
x0= (int) (x1+size) *math.sin (minsec*i));
y0= (int) (y1+size) *math.cos (minsec*i));
}
else{//Other parts draw stub
x0= (int) (x1+size) *math.sin (minsec*i));
y0= (int) (y1+size) *math.cos (minsec*i));
}
x2= (int) (x1+size) *math.sin (minsec*i));
y2= (int) (y1+size) *math.cos (minsec*i));
Offscrgc.drawline (X0,Y0,X2,Y2);
}

Drawhands (OFFSCRGC); Draw pointer
G.drawimage (Offscrimage,0,0,this); Draw the generated buffer graph onto the page

Ispainted=true; Do not draw the dial on the next update
}

Public synchronized void Update (Graphics g)
{
if (!ispainted)//Draw Dial
Paint (g);
else{//has drawn the dial, drawing only the pointer, first in the buffer and then displayed
Drawhands (OFFSCRGC);
G.drawimage (Offscrimage,0,0,this);
}
}
}


An example of the HTML used for testing:


<HTML>
<TITLE> My Clock program </TITLE>
<BODY>
<p>
<applet code= "Myclock.class" width= "height=" >
<param name= "bgcolor" value= "FFFFFF" >
<param name= "Facecolor" value= "FFFFFF" >
<param name= "Sweepcolor" value= "FF0000" >
<param name= "Minutecolor" value= "008080" >
<param name= "Hourcolor" value= "000080" >
<param name= "TextColor" value= "000000" >
<param name= "Casecolor" value= "000080" >
<param name= "Trimcolor" value= "C0C0C0" >
<param name= "Logoimageurl" value= "Java.gif" >
<param name= "TIMEZONE" value= "8" >
</p>
</BODY>
</HTML>




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.