Java小时钟课程设计.docVIP

  • 4
  • 0
  • 约7.3千字
  • 约 11页
  • 2017-05-06 发布于重庆
  • 举报
Java小时钟课程设计

利用JAVA实现一个时钟的小程序 1 设计方法 在设计简单小时钟时,需要编写1个Java源文件:Clock.java 2 程序功能图及程序相关说明 2.1 主功能框 图1 程序基本框图 2.2 程序分析 程序中引入的包 package Clock import java.awt.Clock //颜色类 import java.awt.Graphics //图形类 import java.awt.event.WindowAdapter //窗口事件适配器 import java.awt.event.WindowEvent //窗口事件类 import java.util.Calendar //日历类 import java.util.GregorianCalendar //使用GregorianCalendar对象的get方(参数)获取********************************************************** * 类名:Clock * * 作用:定义小时钟 * * 继承的父类:JFrame * ********************************************************** public class clock extends JFrame{ 定义的对象: ClockPaint size //窗口大小 Resizable //调整窗口大小 Location // 初始位置 Title //窗口标题 Visible //窗口可视化 WindowListener,WindowClosing //创建窗口,并实现关闭功能 定义子类:ClockPaint 继承的父类名:JPanel 继承的接口名:Runnable class ClockPaint extends JPanel implements Runnable int h, m, s // 小时,分钟,秒 主要成员方法: public ClockPaint this.x = x this.y = y this.r = r 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) / 12 * 6 // 获得小时 主要成员方法: public void paint(Graphics g) // 清屏 super.paint(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) // 秒针 g.setColor(Color.RED) int x1 = (int) ((r - 10) * Math.sin(rad * s)) int y1 = (int) ((r - 10) * Math.cos(rad * s)) 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 * m)) y1 = (int) ((r - r / 2.5) * Math.cos(rad * m)) g.drawLine(x + r, y + r, x + r + x1, y + r - y1) // 时针

文档评论(0)

1亿VIP精品文档

相关文档