码迷,mamicode.com
首页 > 编程语言 > 详细

南邮JAVA程序设计实验4 线程程序设计(指针式时钟)

时间:2015-07-03 16:01:34      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:南邮java程序设计实验

南邮JAVA程序设计实验4  线程程序设计(指针式时钟)


实验目的:

本实验旨在通过实验,培养学生将JAVA 线程的相关知识点(包括线程调度,线程同步等)有机结合并加以综合应用,在实验中设计多线程程序的能力。


实验内容:

设计和编写一个编写一个指针式时钟程序,应用线程实现时钟的走动。


实验设计:

主要是控制时针分针秒针的转动度数,这个直接通过坐标的三角函数值求得,线程方面,隔一秒休眠一下,然后通过时分秒的换算关系来改变三个对应指示针在时钟上的位置


实验代码:

import java.awt.*;
import javax.swing.*;
import java.util.*;

public 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("指针时钟");
        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; //时,分,秒
    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();
        //获得时间转换成度数
        second = now.get(Calendar.SECOND) * 6;
        minute = now.get(Calendar.MINUTE) * 6;
        hour = (now.get(Calendar.HOUR_OF_DAY) - 12) * 30 + 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);

        //秒针
        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);

        //分针
        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);

        //时针
        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);

        //数字
        g.setColor(Color.BLACK);
        int d = 28;
        for (int i = 1; i <= 12; 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;
        }

        //刻度线
        d = 0;
        for (int i = 0; i < 60; i++) {
            int len = 0; //控制每个分割点的长度,数字点上的分割线长一点
            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); //休眠一秒
            }
            catch (Exception ex) {

            }
            second += 6; //每一秒,秒针动6度
            if (second == 60 || second == 120 || second == 180 || second == 240 || second == 300) {
                minute += 1;  //每10秒,分针动1度
            }
            //大于一分钟,分针和时针开始出现改变
            if (second == 360) {
                second = 0;
                minute += 1;
                if (minute == 72 || minute == 144 || minute == 216 || minute == 288) {
                    hour += 6;
                }
                if (minute >= 360) {
                    minute = 0;
                    hour += 6;
                }
                if (hour >= 360) {
                    hour = 0;
                }
            }
            this.repaint();
        }
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

南邮JAVA程序设计实验4 线程程序设计(指针式时钟)

标签:南邮java程序设计实验

原文地址:http://blog.csdn.net/tc_to_top/article/details/46740667

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!