制作一个钟表

用EasyX制作的一个简易钟表,需设置字符集属性为多字节字符集。效果如下所示:
在这里插入图片描述
GIF图会有些闪动,在实际中这种闪动几乎不可见。

#define  _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
#define width   900					//屏幕的宽
#define high    600					//屏幕的高
#define P 3.1415926					//圆周率

typedef struct CLOCKDATA
{
	int center_x , center_y ;		//表盘中心,即针的其起点

	int secondLong ;				//秒针的长度
	int secondEnd_x,secondEnd_y;	//秒针终点的位置
	float secondAngle;				//秒针单位转动角度

	int minuteLong;					//分针的长度
	int minuteEnd_x, minuteEnd_y;	//分针终点的位置
	float minuteAngle;				//分针单位转动角度

	int hourLong;					//分针的长度
	int hourEnd_x, hourEnd_y;		//时针终点的位置
	float hourAngle;				//时针单位转动角度

	int R_dial;						//表盘半径
	int scale;						//刻度到表盘中心的距离

	SYSTEMTIME time;				//系统时间
}Clock;

void showDial(Clock *clock);		//打印表盘
void Init(Clock *clock);			//初始化
void UpdatePtrPos(Clock *clock);	//更新指针的位置
void showHour(Clock *clock);		//显示时针
void showMinute(Clock *clock);		//显示分针
void showSecond(Clock *clock);		//显示秒针
void  showTime(Clock *clock);		//显示时间

void showDial(Clock *clock)
{
	setlinecolor(WHITE);									//设置划线颜色
	circle(clock->center_x, clock->center_y, clock->R_dial);//画表盘的圆

	//显示0、3、6、9刻度,用黄边红圆表示
	setfillcolor(RED);
	setcolor(YELLOW);
	int cir1_x = clock->center_x;
	int cir1_y = clock->center_y - clock->secondLong - 25;
	fillcircle(cir1_x, cir1_y, 5);							//画填充圆

	setfillcolor(RED);
	setcolor(YELLOW);
	int cir2_x = clock->center_x + clock->secondLong + 25;
	int cir2_y = clock->center_y;
	fillcircle(cir2_x, cir2_y, 5);

	setfillcolor(RED);
	setcolor(YELLOW);
	int cir3_x = clock->center_x;
	int cir3_y = clock->center_y + clock->secondLong + 25;
	fillcircle(cir3_x, cir3_y, 5);

	setfillcolor(RED);
	setcolor(YELLOW);
	int cir4_x = clock->center_x- clock->secondLong - 25;
	int cir4_y = clock->center_y ;
	fillcircle(cir4_x, cir4_y, 5);
}

void Init(Clock *clock)
{
	initgraph(width, high);			//生成显示钟表的窗口

	clock->center_x = width / 2;	//表盘中心坐标
	clock->center_y = high / 2;

	clock->secondLong = width / 5;	//秒针长度
	clock->minuteLong = width / 6;	//分针长度
	clock->hourLong = width / 7;	//时针长度

	clock->R_dial = width / 4;		//表盘半径

	clock->scale = width / 4 + 30;

	showDial(clock);			
}

void  UpdatePtrPos(Clock *clock)
{
	GetLocalTime(&(clock->time));	//获取时间

	//秒针转动
	clock->secondAngle = (float)(clock->time.wSecond * 2 * P / 60);  //根据时间秒数计算出秒针角度
	clock->secondEnd_x = (int)(clock->center_x + clock->secondLong*sin(clock->secondAngle));//计算秒针终点坐标
	clock->secondEnd_y = (int)(clock->center_y - clock->secondLong*cos(clock->secondAngle));

	//分针转动
	clock->minuteAngle = (float)(clock->time.wMinute * 2 * P / 60);//根据时间分钟数计算出秒针角度
	clock->minuteEnd_x = (int)(clock->center_x + clock->minuteLong*sin(clock->minuteAngle));
	clock->minuteEnd_y = (int)(clock->center_y - clock->minuteLong*cos(clock->minuteAngle));
																						  
	//时针转动
	clock->hourAngle = (float)(clock->time.wHour * 2 * P / 12);//根据时间小时数计算出秒针角度
	clock->hourEnd_x = (int)(clock->center_x + clock->hourLong*sin(clock->hourAngle));
	clock->hourEnd_y = (int)(clock->center_y - clock->hourLong*cos(clock->hourAngle));
}

void showHour(Clock *clock)  //用红色显示时针
{
	setlinestyle(PS_SOLID,2);
	setcolor(RED);
	line(clock->center_x, clock->center_y, clock->hourEnd_x, clock->hourEnd_y);
}

void showMinute(Clock *clock)//用蓝色显示时针
{
	setlinestyle(PS_SOLID, 2);
	setcolor(BLUE);
	line(clock->center_x, clock->center_y, clock->minuteEnd_x, clock->minuteEnd_y);
}

void showSecond(Clock *clock)//用白色显示时针
{
	setlinestyle(PS_SOLID, 2);
	setcolor(WHITE);
	line(clock->center_x, clock->center_y, clock->secondEnd_x, clock->secondEnd_y);

	FlushBatchDraw();
	Sleep(10);

	setcolor(BLACK);
	line(clock->center_x, clock->center_y, clock->secondEnd_x, clock->secondEnd_y);

	if (clock->time.wSecond == 0)//每到一分钟就清空一次表盘
	{
		clearcircle(clock->center_x, clock->center_y, clock->secondLong); 
	}
}

void  showTime(Clock *clock)
{
	char  year[10] = " ", month[10] = " ",day[10] = " ", week[10] = " ";
	char  hour[10] = " ", minute[10] = " ", second[10] = " ";
	sprintf(year, "%d /", clock->time.wYear);
	sprintf(month, "%d /", clock->time.wMonth);
	sprintf(day, "%d ", clock->time.wDay);
	sprintf(week, "星期 %d", clock->time.wDayOfWeek);

	sprintf(hour, "%d 时", clock->time.wHour);
	if (clock->time.wMinute<10)
		sprintf(minute, "0%d 分", clock->time.wMinute);
	else
		sprintf(minute, "%d 分", clock->time.wMinute);
	if (clock->time.wSecond<10)
		sprintf(second, "0%d 秒", clock->time.wSecond);
	else
		sprintf(second, "%d 秒", clock->time.wSecond);

	settextstyle(15, 15, "楷体");
	setcolor(WHITE);
	outtextxy(clock->center_x - 125, clock->center_y + clock->R_dial + 15, hour);
	outtextxy(clock->center_x -25, clock->center_y + clock->R_dial + 15, minute);
	outtextxy(clock->center_x +75, clock->center_y + clock->R_dial + 15, second);

	outtextxy(clock->center_x - 150, clock->center_y + clock->R_dial + 50,year);
	outtextxy(clock->center_x - 50, clock->center_y + clock->R_dial + 50,month);
	outtextxy(clock->center_x +50, clock->center_y + clock->R_dial + 50,day);
	outtextxy(clock->center_x + 150, clock->center_y + clock->R_dial + 50, week);
	if (clock->time.wMilliseconds == 0)//每到一秒就清空一次显示时间的区域
	{
		clearrectangle(clock->center_x - 160, clock->center_y + clock->R_dial + 5, clock->center_x + 250, clock->center_y + clock->R_dial + 70);
	}
	
}

int  main()
{
	Clock clock;
	Init(&clock);
	while (1)
	{
		UpdatePtrPos(&clock); //更新钟表时分秒针位置
		showSecond(&clock);
		showMinute(&clock);
		showHour(&clock);
		showTime(&clock);
	}
	EndBatchDraw();
	system("pause");
	return  0;
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
用MFC VC++实现的时钟源代码 // MFCFrame1View.cpp : implementation of the CMFCFrame1View class // #include "stdafx.h" #include "MFCFrame1.h" #include "MFCFrame1Doc.h" #include "MFCFrame1View.h" #include "PointDialog.h" #include "math.h" GLUquadricObj *objCylinder = gluNewQuadric(); #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CMFCFrame1View IMPLEMENT_DYNCREATE(CMFCFrame1View, CView) BEGIN_MESSAGE_MAP(CMFCFrame1View, CView) //{{AFX_MSG_MAP(CMFCFrame1View) ON_WM_CREATE() ON_WM_DESTROY() ON_WM_SIZE() ON_COMMAND(IDM_ZIXUAN, OnZixuan) ON_WM_TIMER() ON_COMMAND(IDM_ChangDirect, OnChangDirect) //}}AFX_MSG_MAP // Standard printing commands ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMFCFrame1View construction/destruction CMFCFrame1View::CMFCFrame1View() { // TODO: add construction code here this->m_GLPixelIndex = 0; this->m_hGLContext = NULL; Angle1=0.0; Angle2=30.0; Timer=0; x=0.0; z=0.0; juli=40.0; } CMFCFrame1View::~CMFCFrame1View() { } BOOL CMFCFrame1View::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs cs.style |= (WS_CLIPCHILDREN | WS_CLIPSIBLINGS); return CView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // CMFCFrame1View drawing ///////////////////////////////////////////////////////////////////////////// // CMFCFrame1View printing BOOL CMFCFrame1View::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CMFCFrame1View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CMFCFrame1View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } ///////////////////////////////////////////////////////////////////////////// // CMFCFrame1View diagnostics #ifdef _DEBUG void CMFCFrame1View::AssertValid() const { CView::AssertValid(); } void CMFCFrame1View::Dump(CDumpContext& dc) const { CView::Dump(dc); } CMFCFrame1Doc* CMFCFrame1View::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMFCFrame1Doc))); return (CMFCFrame1Doc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMFCFrame1View message handlers BOOL CMFCFrame1View::SetWindowPixelFormat(HDC hDC) { PIXELFORMATDESCRIPTOR pixelDesc= { sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL| PFD_DOUBLEBUFFER|PFD_SUPPORT_GDI, PFD_TYPE_RGBA, 24, 0,0,0,0,0,0, 0, 0, 0, 0,0,0,0, 32, 0, 0, PFD_MAIN_PLANE, 0, 0,0,0 }; this->m_GLPixelIndex = ChoosePixelFormat(hDC,&pixelDesc); if(this->m_GLPixelIndex==0) { this->m_GLPixelIndex = 1; if(DescribePixelFormat(hDC,this->m_GLPixelIndex,sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc)==0) { return FALSE; } } if(SetPixelFormat(hDC,this->m_GLPixelIndex,&pixelDesc)==FALSE) { return FALSE; } return TRUE; } int CMFCFrame1View::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1; // TODO: Add your specialized creation code here HWND hWnd = this->GetSafeHwnd(); HDC hDC = ::GetDC(hWnd); if(this->SetWindowPixelFormat(hDC)==FALSE) { return 0; } if(this->CreateViewGLContext(hDC)==FALSE) { return 0; } return 0; } BOOL CMFCFrame1View::CreateViewGLContext(HDC hDC) { this->m_hGLContext = wglCreateContext(hDC); if(this->m_hGLContext==NULL) {//创建失败 return FALSE; } if(wglMakeCurrent(hDC,this->m_hGLContext)==FALSE) {//选为当前RC失败 return FALSE; } return TRUE; } void CMFCFrame1View::OnDestroy() { CView::OnDestroy(); // TODO: Add your message handler code here if(wglGetCurrentContext()!=NULL) { wglMakeCurrent(NULL,NULL); } if(this->m_hGLContext!=NULL) { wglDeleteContext(this->m_hGLContext); this->m_hGLContext = NULL; } } void CMFCFrame1View::OnSize(UINT nType, int cx, int cy) { CView::OnSize(nType, cx, cy); // TODO: Add your message handler code here GLsizei width,height; GLdouble aspect; width = cx; height = cy; if(cy==0) { aspect = (GLdouble)width; } else { aspect = (GLdouble)width/(GLdouble)height; } glViewport(0,0,width,height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(40.0,aspect,5.0,1000.0); } void CMFCFrame1View::OnDraw(CDC* pDC) { CMFCFrame1Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here CPaintDC dc(this); glClearColor(1.0,1.0,1.0,1.0); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0,0.0,10.0,0.0,0.0,0.0,0.0,1.0,0.0); glRotatef(-90.0,1.0,0.0,0.0);/*返回原坐标*/ glTranslatef(-3.0,0.0,0.0); SwapBuffers(dc.m_ps.hdc); glDrawBuffer (GL_BACK); glFlush(); } void CMFCFrame1View::OnZixuan() { // TODO: Add your command handler code here Timer=1; SetTimer(1,100,NULL); } void CMFCFrame1View::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default CPaintDC dc(this); if (Timer==1) { Angle1=Angle1-1; Angle2=Angle2-1; glClearColor(1.0,1.0,1.0,1.0); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW);/**/ glLoadIdentity(); gluLookAt(0.0,20.0,0.000000001,0.0,0.0,0.0,0.0,1.0,0.0); glPushMatrix(); glColor3f(0.7,0.7,0.7); glTranslatef(0.0,0.0,1.1); glRotatef(-90.0,1.0,0.0,0.0); glutSolidCone(5.0,0.0,60.0,60.0); /*底盘*/ glPopMatrix(); glPushMatrix(); glColor3f(0.0,0.0,0.0); glTranslatef(3.9,0.99,1.0); /*刻度*/ glRotatef(90.0,0.0,1.0,0.0); gluCylinder(objCylinder, 0.05, 0.05, 0.8, 9999, 9); glPopMatrix(); glPushMatrix(); glColor3f(0.0,0.0,0.0); glTranslatef(-4.7,0.99,1.0); /*刻度*/ glRotatef(90.0,0.0,1.0,0.0); gluCylinder(objCylinder,0.05, 0.05, 0.8, 9999, 9); glPopMatrix(); glPushMatrix(); glColor3f(0.0,0.0,0.0); glTranslatef(0.0,0.99,-2.9); /*刻度*/ glRotatef(180.0,0.0,1.0,0.0); gluCylinder(objCylinder,0.05, 0.05, 0.8, 9999, 9); glPopMatrix(); glPushMatrix(); glColor3f(0.0,0.0,0.0); glTranslatef(0.0,0.99,5.8); /*刻度*/ glRotatef(180.0,0.0,1.0,0.0); gluCylinder(objCylinder,0.05, 0.05,0.8, 9999, 9); glPopMatrix(); glPushMatrix(); glColor3f(0.0,1.0,0.0); glRotatef(45.0,0.0,1.0,0.0); glTranslatef(-0.67,0.99,0.7); /*时针*/ glRotatef(Angle1/129600,0.0,1.0,0.0); gluCylinder(objCylinder, 0.07, 0.02, 2.5, 9999, 9); glPopMatrix(); glPushMatrix(); glColor3f(1.0,0.0,0.0); glTranslatef(0.0,0.99,1.0); /*分针*/ glRotatef(Angle2/360,0.0,1.0,0.0); gluCylinder(objCylinder, 0.05, 0.02, 3.5, 9999, 9); glPopMatrix(); glPushMatrix(); glColor3f(0.0,0.0,0.5); glTranslatef(0.0,0.99,1.0); /*秒针*/ glRotatef(Angle1,0.0,1.0,0.0); gluCylinder(objCylinder, 0.07, 0.02, 4.5, 9999, 9); glPopMatrix(); SwapBuffers(dc.m_ps.hdc); glDrawBuffer (GL_BACK); glFlush(); } else if(Timer==2) { glClearColor(1.0,1.0,1.0,1.0); glClear(GL_COLOR_BUFFER_BIT); if (juli>12.0) { glMatrixMode(GL_MODELVIEW);/*建立了从世界坐标系到观察坐标系的转换矩阵*/ glLoadIdentity(); gluLookAt(0.0,8.0,juli,0.0,0.0,0.0,0.0,1.0,0.0); juli=juli-0.1; glPushMatrix(); glColor3f(0.0,0.0,0.0); glRotatef(-90.0,1.0,0.0,0.0); glutWireCone(40.0,0.0,30.0,30.0); /*画高度为0的圆锥*/ glPopMatrix(); glPushMatrix(); glColor3f(1.0,0.0,1.0); glLineWidth(4.0); glTranslatef(4.0,1.0,0.0); glutWireOctahedron(); /*画八面体*/ glLineWidth(1.0); glPopMatrix(); glPushMatrix(); glColor3f(1.0,0.0,1.0); glTranslatef(0.0,1.1,0.0); glRotatef(Angle2,0.0,1.0,0.0); gluCylinder(objCylinder, 1.0, 1.0, 10.0, 9999, 9); /*画壶*/ glPopMatrix(); } else if(juli<=12.0) { Angle2=Angle2+0.01; if (Angle2==360.0) Angle2=0.0; glMatrixMode(GL_MODELVIEW);/*建立了从世界坐标系到观察坐标系的转换矩阵*/ glLoadIdentity(); x=12.0*sin(Angle2); z=12.0*cos(Angle2); gluLookAt(x,5.0,z,0.0,0.0,0.0,0.0,1.0,0.0); glPushMatrix(); glColor3f(0.0,0.0,0.0); glRotatef(-90.0,1.0,0.0,0.0); glutWireCone(40.0,0.0,30.0,30.0); /*画高度为0的圆锥*/ glPopMatrix(); glPushMatrix(); glColor3f(1.0,0.0,1.0); glLineWidth(4.0); glTranslatef(4.0,1.0,0.0); glutWireOctahedron(); /*画八面体*/ glLineWidth(1.0); glPopMatrix(); glPushMatrix(); glColor3f(1.0,0.0,1.0); glTranslatef(0.0,1.1,0.0); gluCylinder(objCylinder, 1.0, 1.0, 10.0, 9999, 9); /*画壶*/ glPopMatrix(); } SwapBuffers(dc.m_ps.hdc); glDrawBuffer (GL_BACK); glFlush(); } CView::OnTimer(nIDEvent); } void CMFCFrame1View::OnChangDirect() { // TODO: Add your command handler code here Timer=2; SetTimer(1,100 ,NULL); }
制作一个钟表可以分为以下几个步骤: 1. 创建一个窗口并设置窗口大小。 ```java JFrame frame = new JFrame("Clock"); frame.setSize(400, 400); ``` 2. 创建一个面板并将其添加到窗口中。 ```java JPanel panel = new JPanel(); frame.add(panel); ``` 3. 创建一个时钟面板并将其添加到面板中。 ```java ClockPanel clockPanel = new ClockPanel(); panel.add(clockPanel); ``` 4. 创建一个时钟面板类并实现绘制钟表的方法。 ```java public class ClockPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); // 获取当前时间 Calendar calendar = Calendar.getInstance(); int hour = calendar.get(Calendar.HOUR); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); // 计算时针、分针、秒针的角度 int hourAngle = (int) ((hour + minute / 60.0) * 30); int minuteAngle = (int) (minute * 6); int secondAngle = (int) (second * 6); // 绘制时针 g.setColor(Color.BLACK); g.drawLine(200, 200, (int) (200 + 60 * Math.sin(Math.toRadians(hourAngle))), (int) (200 - 60 * Math.cos(Math.toRadians(hourAngle)))); // 绘制分针 g.setColor(Color.BLUE); g.drawLine(200, 200, (int) (200 + 80 * Math.sin(Math.toRadians(minuteAngle))), (int) (200 - 80 * Math.cos(Math.toRadians(minuteAngle)))); // 绘制秒针 g.setColor(Color.RED); g.drawLine(200, 200, (int) (200 + 100 * Math.sin(Math.toRadians(secondAngle))), (int) (200 - 100 * Math.cos(Math.toRadians(secondAngle)))); } } ``` 5. 启动窗口。 ```java frame.setVisible(true); ``` 完整代码如下: ```java import java.awt.Color; import java.awt.Graphics; import java.util.Calendar; import javax.swing.JFrame; import javax.swing.JPanel; public class Clock { public static void main(String[] args) { JFrame frame = new JFrame("Clock"); frame.setSize(400, 400); JPanel panel = new JPanel(); frame.add(panel); ClockPanel clockPanel = new ClockPanel(); panel.add(clockPanel); frame.setVisible(true); } } class ClockPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); // 获取当前时间 Calendar calendar = Calendar.getInstance(); int hour = calendar.get(Calendar.HOUR); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); // 计算时针、分针、秒针的角度 int hourAngle = (int) ((hour + minute / 60.0) * 30); int minuteAngle = (int) (minute * 6); int secondAngle = (int) (second * 6); // 绘制时针 g.setColor(Color.BLACK); g.drawLine(200, 200, (int) (200 + 60 * Math.sin(Math.toRadians(hourAngle))), (int) (200 - 60 * Math.cos(Math.toRadians(hourAngle)))); // 绘制分针 g.setColor(Color.BLUE); g.drawLine(200, 200, (int) (200 + 80 * Math.sin(Math.toRadians(minuteAngle))), (int) (200 - 80 * Math.cos(Math.toRadians(minuteAngle)))); // 绘制秒针 g.setColor(Color.RED); g.drawLine(200, 200, (int) (200 + 100 * Math.sin(Math.toRadians(secondAngle))), (int) (200 - 100 * Math.cos(Math.toRadians(secondAngle)))); } } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值