Using thread to realize dynamic display system time _java

Source: Internet
Author: User
Tags current time int size

Write applet applets that display the current system time in different colors and fonts by receiving parameters in an HTML document.

Import java.awt.*; 
Import Java.applet.Applet; 
Import java.util.*; 
Import Java.awt.Graphics; The public class clock extends applet implements Runnable//inherits the applet class and implements the Runnable interface {thread clockthread=null;//Create an empty threads C 
 Alendar now; 
 Private String S1; 
 private int size;   
 int r1,g1,b1; 
 public void init ()//initialization method {Size=integer.parseint (GetParameter ("size"));//Get font size} public void Start () {  
 if (clockthread==null) {clockthread=new thread (this, "Clock2");//Create Thread Object Clockthread clockthread.start ();//Start thread execution } The public void Run ()//Implement the Run () method of the Runnable interface {thread mythread=thread.currentthread ();//Create the Threading object Mythread whi Le (clockthread==mythread) {repaint ()//Call paint method try {thread.sleep (1000) by repaint method;//Hibernate 1 sec} catch (in Terruptedexception e) {}}} public void Paint (Graphics g) {r1= (int) (Math.random () *255);//By calling the Math class's Rando M produces random number g1= (int) (Math.random () *255); 
 Then the three primary colors were set by random number, red-green-blue b1= (int) (Math.random () *255); CoLor c=new Color (R1,G1,B1);   Create a Color object G.setcolor (c); Set color now=calendar.getinstance (); Gets the system current time S1=now.get (now. HOUR) + "Time" +now.get (now. MINUTE) + "min" +now.get (now. 
 SECOND) + "seconds"; Font f=new font ("", 1,size); 
 Sets the font g.setfont (f);  g.DrawString (s1,10,50);  
 Displays the string for the specified size color} public void Stop ()//Call stop method, stop thread {clockthread=null; } <pre class= "html" name= "code" > 

This is how to use the thread to realize the dynamic display of the system time, I hope to help you learn.

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: 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.