A classic Java Applet program (2)

xiaoxiao2021-03-06  39

Then the part of the above, the following is the Applet main program part:

Public class myclock extends applet imports runnable = 0; // Background image sequence number, for array using Static Final INT logo = 1; // logo image serial number, for array using static final string javex = "bear" ; // The text static final double minsec = 0.104719755; // 分 分 分 分 度 度;; 度 度 (度; ((;;; ((((度 ((度 度Arc

Thread clockthread = null; // Use multiple threaded mechanisms, constantly displaying pictures with another thread

// Provide the default parameters, if you do not give parameters in the HTML file; 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 dial is drawn during the first time, it will only draw the pointer at other times.

// Clock position INT X1, Y1;

// Top Triangular scale graphic coordinate int Xpoints [] = new int [3], ypoints [] = new int = new int [3];

/ / Save the current time, convert to (Hours Minutes / 60) HMS Cur_Time;

// Second, minute, hourly Sweephand Sweep; Hmhand MinuteHand, HOURHAND

// Time to draw, minute, second Double LastHour; int lastminute, lastsecond;

// Display the font font font of the letter on the dial.

// Image Display Using Buffer Mechanism, Offscrimage and OffsCRGC Information Image OFFSCRGC; graphics offscrgc;

// Used to test background pictures and logo images Mediatracker Tracker;

INT MINDIMENSITION; // If the background area is not square, ensure that the clock is displayed in the middle to display int originx; // The upper left corner X coordinate int originy in the square area in the middle area INT Originy; // The upper left corner of the square area of ​​the square area Double Tzdifference = 0; // When the time interval, the west is negative, east is positive

Boolean localonly = false; // Whether to use only local time, if false can display the time zone time according to the incoming time zone

/ / Save the type of parameters Description Public string [] [] getParameterInfo () {string [] [] info = {{"width", "int", "the length of the applet, in pixels"}, {"Height "," Int "," Applet "}, {" bgcolor "," string "," background color, EG FF0000 "}, {" facecolor "," string "," dial color " }, {"SweepColor", "String", "Seconds Color"}, {"MinuteColor", "String", "Hour Color"}, {"Hourcolor", "String", "Hethers"}}}}}} "," String "," text color "}, {" casecolor "," string "}}, {" trimcolor "," string "," blank area color "}, {" bgimageurl "," String "," Background Image Address "}, {" Logostring "," String "," LOGO Character "}, {" LogoImageURL "," String "," LOGO Image Address "}, {" Timezone "," Real ", "Difference between time intervals"}, {"localonly", "int", "Do you take into account the time zone difference"}}}}}}};

// Display information public string getappletinfo () {return "copyright, copy must, protect genuine, everyone is responsible";

Void Showurler (Exception E) {string error: " E; showstatus (errormsg); system.rr.println (ErrorMSG);} // is equivalent to turning the clock to 100 × 100 size, percent is relative Coordinate Private Int Size (INT) (Double) Percent / 100.0 * (Double) MINDIMENSITION

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 corresponding 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) {locally = 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 Can't show up!

// SzimagesURL array stores the file name of the picture in the HTML incoming parameter SzimagesURL [Background] = getParameter ("bgimageter"); szimagesURL [logo] = getParameter ("logoimageurl";

// Test the picture for (int i = 0; i <2; i ) {if (SzimagesURL [i]! = Null) {// According to the code of the codebase parameter and the image file name, the test is existing, if there is no No picture 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.waitforl (i);} catch (interruptedException e) {}} else images [i] = null;} // Get the time of the corresponding time zone Cur_time = (LOCALONLY)? New HMS (): New HMS (tzdifference); LastHour = -1.0; lastminute = -1; lastsecond = -1;

X1 = width / 2; y1 = height / 2;

// Convert the round clock in the upper left corner coordinate MINDIMENSITION = Math.min (Width, Height); Originx = (width-mindimension) / 2; Originy = (Height-Mindimension) / 2;

/ / Calculate the coordinate XPoints [1] = x1-size (3) of the three points of the above triangles; Xpoints [2] = x1 size (3); Xpoints [0] = x1; Ypoints [1] = Y1- Size (38); YPOINTS [2] = Y1-size (38); YPOINTS [0] = Y1-Size (27);

/ / Calculate the second needle, minute, hourly graphics Sweep = New Sweephand (x1, y1, size (40), 3); minutehand = new hmhand (x1, y1, size (40), size (6), 6); HourHand = New Hmhand (x1, y1, size (25), size (8), 6);

/ / Configuration Font Font = New Font ("TXT", Font.Bold, Size (10)); // Configuration Buffer Inner Graphic OFFSCRIMAGE = CREATEIMAGE (Width, Height); OffsCRGC = Offscribhip.getgraphics ();

System.out.println (GetAppletInfo ());

Public void start () // Start launch display thread {if (clockthread == null) {clockthread = new thread (} 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; // calculates the second needle with respect to 12-point scale Sweep.draw (FaceColor, Angle, G); / / draws second hand according to LastSecond

// If the time is changed, it will redraw the needle separation 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; G.FillRect (Originx Size (12), Y1-size (2), Size (10), Size (4)); // Draw the left horizontal strip G.FillRect (x1-size (2), Originy MINDIMENSION-SIZE (22), Size (4), Size (10)); // Draw the following horizontal strip G.FillPolygon (XPoints, Ypoints, 3); // Draw the top triangle for ( i = 1; i <12; i = 3) // Draw a circle for the 2, 3, 5, 6, 8, 9, 11 of the dial, plot the circle for (j = I; j

// Set font g.setfont (font); fontmetrics fm = g.getFontMetrics ();

// Draw the Logo string g.drawstring (logostring, x1-fm.stringwidth / 2, y1-size (12));

// Get the date String Day = integer.tostring (cur_time.getdate (), 10);

// Draw the date on the right side of the dial on the right side G. DrawString (Day, Originx Mindimension-size (14) -fm.stringwidth (day), Y1 Size (5)); / / Outside the block G. DrawRect (Originx MINDIMENSION-SIZE (14) -fm.stringwidth (day) -size (2), Y1-size (5) -size (2), fm.StringWidth (day) size (4), size (10) size (4));

IF (images [logo]! = null) {// If logo picture exists, draw X = ORIGINX (Mindimension-Images [logo] .Getwidth (this)) / 2; y = Y1 (Mindimension / 2 - Size (22) - 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 clock

Lastminute = cur_time.getminutes (); minutehand.draw (MinuteColor, Minsec * Lastminute, g); // Drawing

G.SetColor (MinuteColor); // Draw a round G. Filloval (x1-size (4), y1-size (4), size (8), size (8)); G.SetColor (Sweepcolor) ; // Draw a circular G. Filloval (x1-size (3), y1-size (3), size (6), size (6));

LastSecond = CUR_TIME.GETSECONDS (); // Get new seconds, redraw angle = minsec * lastsecond; Sweep.draw (Sweepcolor, Angle, G);

G.SetColor (TRIMCOLOR); G.Filloval (x1-size (1), Y1-size (1), size (2), size (2)); // Seconds the tail of the center of the center should be a screw, a hollow treatment ^ _ ^}

Private color parsecolorstring (String ColorString) // Parameter Introduction is a string (specified as 16 credit 6-bit string) {if (colorstring.Length () == 6) {int R = integer.Valueof (Colorstring.substring (0, 2), 16) .intValue (); // The first two bits are R value int g = integer.Valueof (2, 4), 16) .intValue (); // is g value INT B = Integer.Valueof (4, 6), 16) .intValue (); // Final is B value Return New Color (R, G, B); // Get color} else return color.lightgray // String does not meet the specification, the default is gray} public void run () {repaint (); // First redraw a time / / every 500ms to get the current time and redraw while (null! = Clockthread ) {CUR_TIME = (localonly)? New HMS (): New HMS (TZDIFerence); repaint ();

Try {thread.sleep (500);} catch (interruptedException e) {}}}

Public Void Paint (Graphics G) // Draw a picture in the buffer, then display {INT I, X0, Y0, X2, Y2;

// If the background image is not provided, use the BGColor to draw background IF (Images [Background] == Null) {OFFSCRGC.SETCOLOR (BGColor); Offscrgc.FillRect (0, 0, Width, Height);} else / / Otherwise Use background images Offscrgc.drawImage (images [background], 0, 0, this);

// Draw the outer frame to the partial offscrgc.setcolor (cascolor);

// Reduce the range of the garden (not full of region) to prevent some places from being taken off of Offscrgc.Filloval (Originx 1, Originy 1, Mindimension-2, Mindimension-2);

// Draw the dial Offscrgc.SetColor (Facecolor); Offscrgc.Filloval (Originx Size (5), Originy Size (5), Mindimension - Size (10), MINDIMENSION - SIZE (10));

// Draw an outer frame offscrgc.setColor (Trimcolor); Offscrgc.Drawoval (Originx 1, Originy 1, MINDIMENSION-2, MINDIMENSION-2); // Draw Inner Frame Offscrgc.drawoval (Originx Size (5) , Originy Size (5), MINDIMENSION - SIZE (10), MINDIMENSION - SIZE (10));

OFFSCRGC.SETCOLOR (TextColor);

// Draw, a total of 60 minutes, X0, Y0 is the position of the scale, X1, Y1 center position, X2, Y2 is the scale stop position (X0 = 5 && i% 5 == 0)) {// Draw a long line (relative round) X0 = (int) every 5) X0 = (int) (x1 size) (40) * Math.sin (minsec * i)); Y0 = (int) (Y1 Size (40) * Math.cos (MINSEC * i));} else {// Other partial draw short line X0 = (int (x1 size (42) * math.sin (minsec * i)); Y0 = (int) (Y1 Size (42) * Math.cos (Minsec * i));} x2 = (int) (x1 Size (44) * Math.sin (MINSEC * i)); Y2 = (int) (Y1 Size (44) * Math.cos (Minsec * i)); Offscrgc.Drawline (X0, Y0, X2, Y2 }

Drawhands (OFFSCRGC); // Draw Pointer G.drawImage (Offscrimage, 0, 0, this); // Draw the generated buffer graphics to the page

ISPAINTED = true; // Do not draw the dial} when Update is next Update

Public synchronized void update (graphics g) {if (! ispainted) // Draw the dial Paint (g); Else {// has drawn dials, only draws the pointer, first draw in the buffer, then display drawhands (OffsCRGC) ); G.drawImage (OFFSCRIMAGE, 0, 0, this);}}}

Example of a test for HTML:

转载请注明原文地址:https://www.9cbs.com/read-74206.html

New Post(0)