South Mail Java Programming Experiment 4-Thread program design (pointer clock)

Source: Internet
Author: User

South Mail Java Programming Experiment 4-Thread program design (pointer clock)


Experimental Purpose:

The purpose of this experiment is to train the students to combine the relevant knowledge points of Java thread (including thread scheduling, thread synchronization, etc.) organically and apply them comprehensively, and to design the multi-thread programming ability in the experiment.


Experimental content:

Design and write a program that writes a pointer clock, and the application thread implements the clock to move around.


Experimental Design:

The main is to control the rotation of the hands of the hour and minute hand of the degree, this directly through the coordinates of the trigonometric function value, the thread aspect, a second sleep, and then through the conversion relationship between seconds and seconds to change the position of three corresponding indicator needle on the clock


Experiment Code:

Import java.awt.*;import javax.swing.*;import java.util.*;p ublic class Javaclock extends JFrame {public Javaclock () {        Clockpaint cp = new Clockpaint (20, 20, 70);        This.add (CP);        This.setsize (200, 200);        This.setlocation (260, 120);        This.settitle ("pointer Clock");        This.setvisible (TRUE);    This.setresizable (FALSE);    } public static void Main (string[] s) {new Javaclock ();    }}class Clockpaint extends JPanel implements Runnable {int x, y, R; int hour, minute, second;    Time, minute, second final double rad = math.pi/180;        public clockpaint (int x, int y, int r) {this.x = x;        This.y = y;        THIS.R = R;        Calendar now = new GregorianCalendar ();        Get time converted into degrees second = Now.get (calendar.second) * 6;        minute = Now.get (calendar.minute) * 6;        Hour = (Now.get (calendar.hour_of_day)-) * + now.get (calendar.minute)/12 * 6;        Thread t = new thread (this);    T.start (); } public void PAInt (Graphics g) {super.paint (g);        G.setcolor (Color.White);        G.fillrect (0, 0, R * 3, R * 3);        G.setcolor (Color.Black);        G.drawoval (x, Y, R * 2, R * 2);        Seconds G.setcolor (Color.green);        int x1 = (int) ((r-10) * Math.sin (RAD * second));        int y1 = (int) ((r-10) * Math.Cos (RAD * second));        G.drawline (x + R, y + R, x + R + x1, y + r-y1);        Minute hand g.setcolor (Color.Blue);        x1 = (int) ((r-r/2.5) * Math.sin (RAD * minute));        y1 = (int) ((r-r/2.5) * Math.Cos (RAD * minute));        G.drawline (x + R, y + R, x + R + x1, y + r-y1);        Hour G.setcolor (color.red);        x1 = (int) ((R-R/1.5) * Math.sin (RAD * hour));        y1 = (int) ((R-R/1.5) * Math.Cos (RAD * hour));        G.drawline (x + R, y + R, x + R + x1, y + r-y1);        Digital G.setcolor (Color.Black);        int d = 28;            for (int i = 1; i <=; i++) {x1 = (int) ((r-10) * Math.sin (RAD * d));y1 = (int) ((r-10) * Math.Cos (RAD * d));            g.DrawString (i + "", X + R + x1-4, X + r-y1 + 5);        D + = 30;        }//tick mark d = 0;                for (int i = 0; i <; i++) {int len = 0;//control the length of each split point, the split line on the number point is a bit longer if (d% 30 = = 0) {            len = 5;            } else {len = 2;                } for (int j = 1; j <= Len; j + +) {x1 = (int) ((r-j) * Math.sin (RAD * d));                y1 = (int) ((r-j) * Math.Cos (RAD * d));            g.DrawString (".", X + R + x1-1, X + r-y1 + 1);        } D + = 6;            }} public void Run () {while (true) {try {thread.sleep (1000);//Sleep one second } catch (Exception ex) {} second + = 6;                Every second, the second hand moves 6 degrees if (second = = | | second = = | | second = = | | | second = = | | second = 300) {  minute + = 1;    Every 10 seconds, the minute hand moves 1 degrees.        }//greater than one minute, the minute hand and the hour hand begin to appear change if (second = =) {second = 0;                minute + = 1;                if (minute = = | | | minute = 144 | | minute = = 216 | | minute = 288) {hour + = 6;                    } if (minute >=) {minute = 0;                Hour + = 6;                } if (hour >=) {hour = 0;        }} this.repaint (); }    }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

South Mail Java Programming Experiment 4-Thread program design (pointer clock)

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.