求:模拟时钟转动程序的c++课程设计

能模拟机械钟表行走,还要准确的利用数字显示日期和时间,在屏幕上显示一个活动时钟,按任意键时程序退出.... 能模拟机械钟表行走,还要准确的利用数字显示日期和时间,在屏幕上显示一个活动时钟,按任意键时程序退出. 展开
 我来答
wlingfeng
2007-09-02 · TA获得超过119个赞
知道小有建树答主
回答量:110
采纳率:0%
帮助的人:130万
展开全部
C++的没得时间做,给你一个我做的Java程序做参考吧.
import java.applet.Applet;
import java.awt.*;
import java.util.*;
import java.awt.event.*;

public class ShowTime extends Applet {

TextField txt_Hour;
TextField txt_Min;
TextField txt_Sec;
Label lab_Hour;
Label lab_Min;
Label lab_Sec;
TextField txt_show;
final int Clock_X=20; //表显示区域的左上X坐标
final int Clock_Y=40;
final int Clock_Size=150; //表盘大小
final int Clock_CenX=Clock_X+(Clock_Size/2); //表盘中心X坐标
final int Colok_CenY=Clock_Y+(Clock_Size/2); //表盘中心Y坐标
Color bgcolor=Color.lightGray;

Point Old_SecPoint1; //用来保存秒针上一次的坐标位置,以便在下一次画的时候覆盖上一次画的指针
Point Old_SecPoint2;
Point Old_MinPoint1;
Point Old_MinPoint2;
Point Old_HourPoint1;
Point Old_HourPoint2;

public void init()
{
txt_Hour=new TextField();
txt_Min=new TextField();
txt_Sec=new TextField();
lab_Hour=new Label("H:");
lab_Min=new Label("M:");
lab_Sec=new Label("S:");
txt_show=new TextField("",20);
txt_show.setBackground(Color.cyan);
this.add(lab_Hour);
this.add(txt_Hour);
this.add(lab_Min);
this.add(txt_Min);
this.add(lab_Sec);
this.add(txt_Sec);
show s=new show();
s.start();
Old_SecPoint1=new Point(0,0);
Old_SecPoint2=new Point(0,0);
Old_MinPoint1=new Point(0,0);
Old_MinPoint2=new Point(0,0);
Old_HourPoint1=new Point(0,0);
Old_HourPoint2=new Point(0,0);
}
public void paint(Graphics g)
{
g.setColor(bgcolor);
g.fillOval(Clock_X, Clock_Y, Clock_Size, Clock_Size);
g.setColor(Color.black);
for(int i=0;i<360;i+=6)
{
Point p1;
Point p2;
if(i%30==0)
{
p1=getPoint(i,Clock_Size/2);
p2=getPoint(i,Clock_Size/2-10);
}
else
{
p1=getPoint(i,Clock_Size/2);
p2=getPoint(i,Clock_Size/2-5);
}
g.drawLine(p1.x, p1.y, p2.x, p2.y);
}
}
public Point getPoint(int arc,int length)
{
Point p=new Point();
double Arc=(Math.PI*arc)/180;
int h=(int)(length*Math.sin(Arc));
int l=(int)(length*Math.cos(Arc));
p.x=Clock_CenX+h;
p.y=Colok_CenY-l;
return p;
}
private void DrawSecPoint(int arc)
{
Graphics g=this.getGraphics();
g.setColor(bgcolor);
g.drawLine(Old_SecPoint1.x, Old_SecPoint1.y, Old_SecPoint2.x, Old_SecPoint2.y);
g.setColor(Color.blue);
Point p1=getPoint(arc,Clock_Size/2-15);
Point p2=getPoint(arc+180,10);
g.drawLine(p1.x, p1.y, p2.x, p2.y);
Old_SecPoint1=p1;
Old_SecPoint2=p2;
}

private void DrawMinPoint(int arc)
{
Graphics g=this.getGraphics();
g.setColor(bgcolor);
g.drawLine(Old_MinPoint1.x, Old_MinPoint1.y, Old_MinPoint2.x, Old_MinPoint2.y);
g.setColor(Color.yellow);
Point p1=getPoint(arc,Clock_Size/2-30);
Point p2=getPoint(arc+180,10);
g.drawLine(p1.x, p1.y, p2.x, p2.y);
Old_MinPoint1=p1;
Old_MinPoint2=p2;
// g.setColor(Color.red);
// g.fillOval(Clock_CenX-2, Colok_CenY-2, 2, 2);
// g.setColor(Color.black);
}

private void DrawHourPoint(int arc)
{
Graphics g=this.getGraphics();
g.setColor(bgcolor);
g.drawLine(Old_HourPoint1.x, Old_HourPoint1.y, Old_HourPoint2.x, Old_HourPoint2.y);
g.setColor(Color.orange);
Point p1=getPoint(arc,Clock_Size/2-40);
Point p2=getPoint(arc+180,10);
g.drawLine(p1.x, p1.y, p2.x, p2.y);
Old_HourPoint1=p1;
Old_HourPoint2=p2;
g.setColor(Color.white);
g.drawString("王鸿制作", Clock_X+50,Clock_Y+50);
}

class show extends Thread{
public void run()
{
try{
while(true)
{
sleep(1000);
Date now=new Date();
String str=String.valueOf(now.getHours());
txt_Hour.setText(str);
str=String.valueOf(now.getMinutes());
txt_Min.setText(str);
str=String.valueOf(now.getSeconds());
txt_Sec.setText(str);
int hourpositon=now.getHours()*30+(now.getMinutes()*30/60);
DrawHourPoint(hourpositon);//每秒钟时钟步进
DrawMinPoint(now.getMinutes()*6+(now.getSeconds()*6/60));//每秒钟,分指步进
DrawSecPoint(now.getSeconds()*6);

}
}
catch(InterruptedException e)
{}
}
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式