JAVA线程程序设计实验报告_第1页
JAVA线程程序设计实验报告_第2页
JAVA线程程序设计实验报告_第3页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、线程程序设计一、课题内容和要求内容:设计和编写一个编写一个指针式时钟程序,应用线程实现时钟的走 动。要求:本实验旨在通过实验,培养学生将JAVA线程的相关知识点(包括线程调度,线程同步等)有机结合并加以综合应用,在实验中设计多线程程序的能 力。二、设计思路分析class Clock: 一个指针式时钟的主类class Layout: 添加窗口和时钟组件class ClockPai nt: 定义时钟组件三、概要设计public class Clock exte nds JFrame public static void main( Stri ng s);class Layout exte nds

2、JFrame public Layout();class ClockPa int exte nds JPa nel impleme nts Runn able int x, y, r;int h, m, s;double rad = Math.PI / 180;public ClockPa in t(i nt x, int y, int r);public void pain "Graphics g);public void run();时钟的绘制:运行时钟:()IIhrcad.sk-cpdOOO)中断一秒石車新运行s += 6秒"针走b度5 = O,ID +»

3、6h + 6m = 0*h +工 6(结束)*四、详细设计import java.awt.*;import javax.swi ng.*;import java.util.*;public class Clock exte nds JFrame public static void main(String s) new Layout。;class Layout extends JFrame / 添加窗口和时钟组件 public Layout() ClockPai nt cp = new ClockPai nt(20, 20, 70); add(cp);setBounds(260, 120, 2

4、00, 200);setResizable(false);this.setTitle(" 指针式时钟 ");this.setVisible(true); 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) / 构造函数this.x = x;this.y = y;this.

5、r = r;Calendar now = Calendar.getInstance();/ 初始化日历对象s = now.get(Calendar.SECOND) * 6;/ 获得初始秒转换成度数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();/ 启动线程pub

6、lic void paint(Graphics g) / 绘制时钟g.setColor(Color.BLACK);/ 画背景g.fillRect(0, 0, r * 3, r * 3);g.setColor(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 *

7、d); g.drawString(".", x + r + x1 - 1, x + r - y1 + 1); d += 6;d = 30;/ 从 30 度开始每 30度画一个数字和一线for (int 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) * M

8、ath.sin(rad * d);y1 = (int) (r - 6) * Math.cos(rad * d);x2 = (int) (r - 2) * Math.sin(rad * d);y2 = (int) (r - 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

9、.cos(rad * s); g.drawLine(x + r, y + r, x + r + x1, y + r - y1);g.setColor(Color.BLUE);/ 按时间画分针 x1 = (int) (0.6 * 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 *

10、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.GREEN);/ 显示时间Calendar now1 = Calendar.getInstance(); g.drawString(now1.get(Calendar.HOUR_OF_DAY) + " : " + now1.get(Calendar.MINUTE) + " : " + now1.get(Calendar.SECOND),

11、 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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论