辽 宁 工 业 大 学
JAVA程序设计课程设计(论文)
题目: 时钟
院(系): 软件学院
专业班级: 软件技术 班
学 号:
学生姓名:
指导教师: 丁宝柱
教师职称: 助 教
起止时间: 2009.12.1至2009.12.16
程序设计专题(报告)任务及评语
院(系):软件学院 教研室:软件教研室
目 录
第1章 课程设计的目的与要求................................................ 1
1.1 课程设计目的........................................................ 1
1.2 课程设计的实验环境.................................................. 1
1.3 课程设计的预备知识.................................................. 1
1.4 课程设计要求........................................................ 1
第2章 课程设计内容........................................................ 2
2.1课程设计主要内容.................................................... 2
2.2概要设计............................................................ 2
2.2.1自定义类说明.................................................. 2
2.3详细设计............................................................ 3
2.4测试分析........................................................... 16
2.4.1程序运行情况................................................. 16
2.4.2程序异常处理................................................. 16
第3章 课程设计总结........................................................ 17
参考资料 18
第1章 课程设计的目的与要求
1.1 课程设计目的
《JAVA程序设计》是计算机相关专业的必修专业基础课程,其实践性、应用性很强。实践教学环节是必不可少的一个重要环节。本课程的程序设计专题实际是计算机相关专业学生学习完《JAVA程序设计》课程后,进行的一次全面的综合训练,JAVA程序设计的设计目的是加深对理论教学内容的理解和掌握,使学生较系统地掌握程序设计及其在网络开发中的广泛应用,基本方法及技巧,为学生综合运用所学知识,利用软件工程为基础进行软件开发、并在实践应用方面打下一定基础。
1.2 课程设计的实验环境
硬件要求能运行Windows 9.X操作系统的微机系统。JAVA程序设计语言及相应的集成开发环境,J2SDK和ECLIPSE开发工具。
1.3 课程设计的预备知识
熟悉JAVA语言及ECLIPSE开发工具。
1.4 课程设计要求
按课程设计指导书提供的课题,要求学生在自行完成各个操作环节,并能实现且达到举一反三的目的,完成一个项目解决一类问题。要求学生能够全面、深入理解和熟练掌握所学内容,并能够用其分析、设计和解答类似问题;对此能够较好地理解和掌握,能够进行简单分析和判断;能编写出具有良好风格的程序;掌握JAVA程序设计的基本技能和面向对象的概念和方法;了解多线程、安全和网络等编程技术。同时培养学生进行分析问题、解决问题的能力;培养学生进行设计分析、设计方法、设计操作与测试、设计过程的观察、理解和归纳能力的提高。
第2章 课程设计内容
2.1课程设计主要内容
我设计的时钟有的界面良好,比较简洁美观,程序有很强的实用性,实现程序与电脑的时间的同步。可以显示时钟,也可以显示分针秒针,并可以在相应位置调整时间。而且初始运行会自动与电脑的时间校对,一般默认为同步,但还可以自己再次调节,提高了实用性。
本系统共包括1个java源文件。1、Clock源文件是本程序的主函数其作用是初始化棋盘。2、setCurrentTime源文件实现电脑设置时间。3、paintHourPointer源文件为时针.4、paintSecondPointer源文件实现人与电脑设置秒针.5、paintMinuteDot源文件人与电脑设置分针.
2.2概要设计
2.2.1自定义类说明
**********************************************************
* 类名: Clock *
* 作用: 自定义主类,对鼠标拖拽的初始界面进行声明*
* 继承的父类: JComponent类 *
* 实现的接口: 没有 *
**********************************************************
表1-成员变量表
表2-方法表
2.3详细设计
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.Calendar;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.UIManager;
public class Clock extends JComponent
{
private static final Color INTEGRAL_COLOR = new Color(0, 128, 128);
private int radius;
private Calendar currentTime = Calendar.getInstance();
private double s = 0.03;
public Clock(int radius)
{
this.radius = radius;
}
public void setCurrentTime(Date time) //设置当前时间
{
this.currentTime.setTime(time);
}
public void setCurrentTime(long millis)
{
this.currentTime.setTimeInMillis(millis);
}
public Dimension getPreferredSize()
{
Insets insets = getInsets();
int r = (int) (radius == -1 ? 0 : radius*(1+s))+1;
return new Dimension(r * 2 + insets.left + insets.right,r * 2 + insets.top + insets.bottom); //返回一个指定宽、高的Dimension
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Insets insets = getInsets();
int wid = getWidth() - insets.left - insets.right;
int hei = getHeight() - insets.top - insets.bottom;
int r = (int) ((Math.min(wid, hei)) / 2 / (1+s));
g2d.translate(insets.left + r * (1+s), insets.top + r * (1+s));
g2d.scale(1, -1);
for (int i = 0; i < 60; i++) {
int angle = 90 - i * 6;
double pos[] = calcPos(r, angle);
paintMinuteDot(r, g2d, pos[0], pos[1], i % 5 == 0);
}
paintHourPointer(r, g2d);
paintMinutePointer(r, g2d);
paintSecondPointer(r, g2d);
paintCenterPoint(g2d);
g2d.scale(1, -1);
g2d.translate(-insets.left - r * (1+s), -insets.top - r * (1+s));
}
private void paintCenterPoint(Graphics2D g2d)
{
g2d.setColor(Color.BLUE);
Rectangle2D rect = new Rectangle2D.Double(-2, -2, 4, 4);
g2d.fill(rect);
}
private void paintMinutePointer(int r, Graphics2D g2d)
{
int minute = currentTime.get(Calendar.MINUTE);
int second = currentTime.get(Calendar.SECOND);
double angle = 90 - (minute + second / 60.0) * 6;
Shape pointerShape = createPointerShape(r * 0.8, r * 0.04, r * 0.08, angle);
g2d.setColor(Color.LIGHT_GRAY);
g2d.fill(pointerShape);
g2d.setColor(Color.DARK_GRAY);
g2d.draw(pointerShape);
}
private void paintHourPointer(int r, Graphics2D g2d)
{
int hour = currentTime.get(Calendar.HOUR);
int minute = currentTime.get(Calendar.MINUTE);
int second = currentTime.get(Calendar.SECOND);
double angle = 90 - (hour + minute / 60.0 + second / 3600.0) * 30;
Shape pointerShape = createPointerShape(r * 0.6, r * 0.06, r * 0.1, angle);
g2d.setColor(Color.LIGHT_GRAY);
g2d.fill(pointerShape);
g2d.setColor(Color.DARK_GRAY);
g2d.draw(pointerShape);
}
private Shape createPointerShape(double r1, double r2, double r3, double angle)
{
GeneralPath gp = new GeneralPath();
double[] pos = calcPos(r1, angle);
double[] pos1 = calcPos(r2, angle + 90);
gp.append(new Line2D.Double(pos[0], pos[1], pos1[0], pos1[1]), true);
double[] pos2 = calcPos(r3, angle + 180);
gp.lineTo((float)pos2[0], (float)pos2[1]);
double[] pos3 = calcPos(r2, angle + 270);
gp.lineTo((float)pos3[0], (float)pos3[1]);
gp.closePath();
return gp;
}
private void paintSecondPointer(int r, Graphics2D g2d)
{
g2d.setColor(Color.BLACK);
int second = currentTime.get(Calendar.SECOND);
int angle = 90 - second * 6;
double pos[] = calcPos(r * 0.9, angle);
double pos1[] = calcPos(r * 0.2, angle + 180);
Line2D line = new Line2D.Double(pos1[0], pos1[1], pos[0], pos[1]);
g2d.draw(line);
}
private void paintMinuteDot(int r, Graphics2D g2d, double x, double y, boolean flag)
{
g2d.setColor(flag ? Color.RED : Color.BLACK);
if (flag) {
//Rectangle2D rect = new Rectangle2D.Double(
Ellipse2D rect = new Ellipse2D.Double(
x - r * s, y - r * s, r * s * 2, r * s * 2);
g2d.fill(rect);
}
else {
//Rectangle2D rect = new Rectangle2D.Double(
Ellipse2D rect = new Ellipse2D.Double(
x - r * 0.02, y - r * 0.02, r * 0.04, r * 0.04);
g2d.fill(rect);
}
}
private double[] calcPos(double r, double angle)
{
double radian = Math.toRadians(angle);
double x = r * Math.cos(radian);
double y = r * Math.sin(radian);
return new double[] {x, y};
}
public static void main(String[] args)
{
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
final Clock clock = new Clock(50);
clock.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JFrame f = new JFrame( "软件081班 071404011 孙庆贺 ");
//f.setBounds(380,200,500,600);
f.getContentPane().add(clock, BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
new Thread() {
public void run()
{
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
clock.setCurrentTime(System.currentTimeMillis());
clock.repaint();
}
}
}.start();
}
}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);
//时针
g.setColor(Color.CYAN);
x1 = (int)((r - r / 1.5) * Math.sin(rad * h));
y1 = (int)((r - r / 1.5) * Math.cos(rad * h));
g.drawLine(x + r, y + r, x + r + x1, y + r - y1);
//数字
g.setColor(Color.YELLOW);
int d = 29;
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++) {
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;
}
//显示时间
Calendar now1 = new GregorianCalendar();
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) {
}
s+=6;
if (s >= 360) {
s = 0;
m+=6;
if (m == 72 || m == 144 || m == 216 || m == 288) {
h+=6;
}
if (m >= 360) {
m = 0;
h+=6;
}
if (h >=360) {
h = 0;
}}
this.repaint();
}}}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 = new GregorianCalendar();
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;//获得小时
Thread t = new Thread(this);
t.start();
}
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));
//定义MyTimer类
class MyTimer1 extends JFrame
{ static int count=0; //判断是否重定义了时间
//构造函数
public MyTimer1()
{
//定义窗口大小
setSize(320, 200);
//定义窗口标题
setTitle("测试自定义时钟类!");
Container c = getContentPane();
// new ClockCanvas("北京时间", "GMT+8")
c.add(new ClockCanvas("北京时间", "GMT+8"));
} }
//定义接口
interface TimerListener1
{
void timeElapsed(Timer1 t);
}
class Timer1 extends Thread //类Timer1
{
private TimerListener1 target;
private int interval;
public Timer1(int i, TimerListener1 t)
{
target = t;
interval = i;
setDaemon(true);
}
public void run()
{ try {
while (!interrupted())
{
sleep(interval);
target.timeElapsed(this);
} }
catch(InterruptedException e) {}
} }
class ClockCanvas extends JPanel //clockcanvas
implements TimerListener1
{
static int seconds = 0;
private String city;
private GregorianCalendar calendar;
//构造函数
public ClockCanvas(String c, String tz)
{
city = c;
calendar = new GregorianCalendar(TimeZone.getTimeZone(tz));
Timer1 t = new Timer1(1000, this);
t.start();
setSize(180, 180);
}
//绘制钟面
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawOval(100, 5, 120, 120);
g.drawOval(101, 6, 118, 118);
//分离时间
double hourAngle = 2 * Math.PI
* (seconds - 3 * 60 * 60) / (12 * 60 * 60);
double minuteAngle = 2 * Math.PI
* (seconds - 15 * 60) / (60 * 60);
double secondAngle = 2 * Math.PI
* (seconds - 15) / 60; }
public void timeElapsed(Timer1 t) {
calendar.setTime(new Date());
if(MyTimer1.count==1) {int a=1;
seconds=MyTimer.intHour*60*60+MyTimer.intMinute*60+MyTimer.intSecond;
seconds+=a;//a为秒自加
repaint();}
else
{ seconds = calendar.get(Calendar.HOUR) * 60 * 60
+ calendar.get(Calendar.MINUTE) * 60
+ calendar.get(Calendar.SECOND);
repaint();}
} }
//定义时钟类
class MyTimer
implements TimerListener
{
//定义时钟类的属性
static int intHour,intMinute,intSecond;
//构造函数
public MyTimer()
{
setCurrentTimeAsSystemTime();
Timer t = new Timer(1000, this); //实例Timer类,里面有run方法
t.start();
}
//显示当前时间
public void displayCurrentTime()
{
JOptionPane.showMessageDialog(null,intHour+":"+intMinute+":"+intSecond); }
//设定当前时间
public void setCurrentTime() {
//从对话框输入时,分秒
String strTemp=JOptionPane.showInputDialog(null,"请输入当前小时(24小时制):");
int iHour=Integer.parseInt(strTemp);
strTemp=JOptionPane.showInputDialog(null,"请输入当前分:");
int iMinute=Integer.parseInt(strTemp);
strTemp=JOptionPane.showInputDialog(null,"请输入当前秒:");
int iSecond=Integer.parseInt(strTemp);
//设定当前时间为对话框输入的时间
if(iHour>=0&&iHour<24)
intHour=iHour;
transform(angle);
g.setcolor(color);
//设定当前时间为系统时间,构造函数调用
public void setCurrentTimeAsSystemTime() {
//定义Date类的一个对象,用来获取系统时间
Date timeCurrent=new Date();
catch(InterruptedException e) {} } }
2.4测试分析
2.4.1程序运行情况
当程序正常运行的时候,它能清晰的显示时钟界面。默认为与系统时间同步,如果出现错误,就会进行归零。运行界面如图2.1:
图2.1运行界面
2.4.2程序异常处理
当线程在活动之前或活动期间处于正在等待、休眠或占用状态且该线程被中断时,抛出该异常。有时候,一种方法可能希望测试当前线程是否已被中断,如果已被中断,则立即抛出此异常。利用try……catch来捕获出现的异常。
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
System.out.println(“抛出异常”)
第3章 课程设计总结
本次Java课程设计我的题目是时钟,题目不算难,但我做的很艰苦。主要就是动手能力太差,理论知识掌握的不全面,自以为会不少,但真做起来,却不知道从哪下手了。又回去温习理论知识,请教老师,上网查资料,忙的不亦乐乎。不过经过努力总算是做出来了,还通过此次课程设计,将我本学期所学的JAVA知识得到巩固和应用。在设计的过程中我遇到的很多问题,在老师的帮助和自己的思考下还是很好的完成了。这此课程设计我懂得了写程序不能脱离实际,只凭主观办事,要努力拓宽知识面,拓展思维。
事情就是如此,努力代表收获。通过这次课程设计,我才真正领悟到“艰苦奋斗”这一词的真正含义,我才感受到早期电子设计者为现代的社会付出。设计确实有些辛苦,但苦中也有乐,在如今单一的理论学习中,很少有机会能有实践的机会,但现在可以,这些天的设计中得到的东西却不比理论课上得到的少多少,我感觉我和Java的距离更加近了;我想说,确实很累,但当我看到自己所做的成果时,心中也不免产生兴奋; 学习有成果了么。
对Java编程有了一个基本的了解。这些天温习的内容越多,敲得代码越多,自己的学习积极性也越在提高,感觉也越充实。越来越发现现在学习的只是Java的基础,但涉及的面还是很广泛的,这些都为以后进一步学习Java的高级特性打下了扎实的基础。随着课程难度的慢慢加强,今后遇到的困难也必将会越来越多,但我相信自己一定能做好这些的。
在本次Java课程设计中,我要非常感谢我的指导老师丁老师,不但在课堂上教给了我很多的Java的编程知识,编程思想,而且在这次课程设计中更教会了我动手的能力,真正的编出了自己的东西,还要感谢我的同学,他们在我遇到困难时也给了我很多帮助。
参考资料
1. 季民 J2EE案例开发 中国水利水电出版 20##-08
2. [美]Joe Walnes Java开放源码编程 电子工业出版 20##-07
3.郝玉龙 J2EE编程技术 北方交大出版 20##-02
4. 朱喜福 Java程序设计 人民邮电出版 20##-04
5. 王路群 Java高级程序设计 中国水利水电出版社 20##-8
6. 王碧波 JBuilder 10入门与提高 清华大学出版 20##-11
7. 吴其庆 Java程序设计实例教程 冶金工业出版社 20##-11
8. 柳西玲 Java语言应用开发基础 清华大学出版社 20##-2
9. 朱战立 Java程序设计实用教程 人民邮电出版 20##-03
10. 冯博 Java网络编程 清华大学出版 20##-10