下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精选优质文档-倾情为你奉上线程程序设计一、 课题内容和要求内容:设计和编写一个编写一个指针式时钟程序,应用线程实现时钟的走动。要求:本实验旨在通过实验,培养学生将JAVA 线程的相关知识点(包括线程调度,线程同步等)有机结合并加以综合应用,在实验中设计多线程程序的能力。二、设计思路分析class Clock:一个指针式时钟的主类class Layout: 添加窗口和时钟组件class ClockPaint:定义时钟组件三、概要设计 public class Clock extends JFrame public static void main(String s) ;class Layout
2、extends JFrame public Layout();class ClockPaint extends JPanel implements Runnable int x, y, r; int h, m, s; double rad = Math.PI / 180; public ClockPaint(int x, int y, int r);public void paint(Graphics g);public void run();时钟的绘制:运行时钟:四、详细设计 import java.awt.*;import javax.swing.*;import java.util.*;
3、public class Clock extends JFrame public static void main(String s) new Layout();class Layout extends JFrame / 添加窗口和时钟组件public Layout() ClockPaint cp = new ClockPaint(20, 20, 70);add(cp);setBounds(260, 120, 200, 200);setResizable(false);this.setTitle("指针式时钟");this.setVisible(true);class Cl
4、ockPaint extends JPanel implements Runnable / 定义时钟组件int x, y, r;/ 时钟的位置坐标和半径int h, m, s;/ 小时,分钟,秒double rad = Math.PI / 180;/ 定义弧度public ClockPaint(int x, int y, int r) / 构造函数this.x = x;this.y = y;this.r = r;Calendar now = Calendar.getInstance();/ 初始化日历对象s = now.get(Calendar.SECOND) * 6;/ 获得初始秒转换成度数
5、m = now.get(Calendar.MINUTE) * 6;/ 获得初始分钟转换成度数h = (now.get(Calendar.HOUR_OF_DAY) - 12) * 30+ now.get(Calendar.MINUTE) * 6 / 12;/ 获得初始小时转换成度数加分钟实现连贯Thread t = new Thread(this);/ 新建线程t.start();/ 启动线程public void paint(Graphics g) / 绘制时钟g.setColor(Color.BLACK);/ 画背景g.fillRect(0, 0, r * 3, r * 3);g.setCo
6、lor(Color.WHITE);/ 画圆g.drawOval(x, y, r * 2, r * 2);int d = 0;/ 每6度画一个小点int x1, y1, x2, y2;for (int i = 0; i < 60; i+) x1 = (int) (r - 2) * Math.sin(rad * d);y1 = (int) (r - 2) * Math.cos(rad * d);g.drawString(".", x + r + x1 - 1, x + r - y1 + 1);d += 6;d = 30;/ 从30度开始每30度画一个数字和一线for (i
7、nt i = 1; i <= 12; i+) x1 = (int) (r - 14) * Math.sin(rad * d);y1 = (int) (r - 14) * Math.cos(rad * d);g.drawString(i + "", x + r + x1 - 4, x + r - y1 + 5);x1 = (int) (r - 6) * Math.sin(rad * d);y1 = (int) (r - 6) * Math.cos(rad * d);x2 = (int) (r - 2) * Math.sin(rad * d);y2 = (int) (r
8、- 2) * Math.cos(rad * d);g.drawLine(x + r + x2, y + r - y2, x + r + x1, y + r - y1);d += 30;g.setColor(Color.RED);/ 按时间画秒针x1 = (int) (0.8 * r) * Math.sin(rad * s);y1 = (int) (0.8 * r) * Math.cos(rad * s);g.drawLine(x + r, y + r, x + r + x1, y + r - y1);g.setColor(Color.BLUE);/ 按时间画分针x1 = (int) (0.6
9、* r) * Math.sin(rad * m);y1 = (int) (0.6 * r) * Math.cos(rad * m);g.drawLine(x + r, y + r, x + r + x1, y + r - y1);g.setColor(Color.YELLOW);/ 按时间画时针x1 = (int) (0.4 * r) * Math.sin(rad * h);y1 = (int) (0.4 * r) * Math.cos(rad * h);g.drawLine(x + r, y + r, x + r + x1, y + r - y1);g.setColor(Color.GREE
10、N);/ 显示时间Calendar now1 = Calendar.getInstance();g.drawString(now1.get(Calendar.HOUR_OF_DAY) + ":"+ now1.get(Calendar.MINUTE) + ":"+ now1.get(Calendar.SECOND), 0, 10);public void run() / 运行时钟while (true) try Thread.sleep(1000);/ 中断一秒后重新运行 catch (Exception ex) System.out.println(ex);s += 6;/ 秒针走6度if (s >= 360) / 秒针走完一分钟后重置s = 0;m += 6;if (m = 72 | m = 144 | m = 216 | m = 288) / 分针走完5分钟后重置h += 6;if (m >= 360) / 分针走完一小时后重置m = 0;h += 6;if (h >= 360) / 时针走完12小时后重置h = 0;this.repaint();/ 重新绘制时钟五、测试数据及其结果
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年四川三河职业学院单招综合素质考试题库带答案详解(模拟题)
- 2026年四川化工职业技术学院单招职业倾向性测试题库(含答案详解)
- PDCA方法在血透室护理信息化建设中的应用
- 10.2任务二 短期借款业务核算与应用
- 民航就业指导教程书
- 完美日记品牌营销案例拆解
- 2026年青岛市按摩康复医院公开招聘卫生类岗位工作人员(2名)考试备考试题及答案解析
- 2026四川宜宾高县建高华西矿业有限公司第一批员工招聘1人笔试模拟试题及答案解析
- 2025年湖北省黄石市高职单招职业技能考试试题及答案解析
- 2026安徽蚌埠市12345政务服务便民热线岗位招聘20人考试备考题库及答案解析
- 《接地电阻测量技术》课件
- 【初中 语文】第18课《井冈翠竹》教学设计++2024-2025学年统编版语文七年级下册
- 2024版《大学生健康教育》课件
- 2025年中华联合财产保险股份有限公司招聘笔试参考题库含答案解析
- 课题申报参考:文艺与经济融合现象研究
- 外科学绪论教学课件
- 毕业设计(6)年产420万吨热轧带钢车间设计
- 环境设计专业的职业规划
- 泵站养护服务方案
- 抽水蓄能电站下水库面板堆石坝填筑碾压试验报告
- 输电线路杆塔及电力金具用防松螺母
评论
0/150
提交评论