導航:首頁 > 編程知識 > 編程出來的時鍾怎麼寫

編程出來的時鍾怎麼寫

發布時間:2023-12-19 10:28:48

Ⅰ 用java寫數字時鍾

這個數字時鍾的核心部分應該是一條1秒鍾刷新一次的線程
每一秒就用 new Date()獲得當前時間 並且按照你的個人想法設置相應的 SimpleDateFormat對其進行format
如果需要封裝成可視化的就把這個new Date()放進一個Frame裡面

Ⅱ 怎麼用JAVA編程實現實時動態運行的模擬時鍾

import java.awt.*;
import java.applet.Applet;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ClockApplet extends Applet implements Runnable //Applet支持線程
{
private Thread athread; //線程
private SimpleDateFormat sdateformat; //日期格式

public void init()
{

this.setBackground(Color.white);//背景顏色設為白色
this.athread = null;
}
public void paint(Graphics g)
{
this.sdateformat = new SimpleDateFormat("hh時mm分ss秒");
g.drawString(this.sdateformat.format(new Date()),25,131);

Calendar rightnow = Calendar.getInstance();
int second = rightnow.get(Calendar.SECOND);
int minute = rightnow.get(Calendar.MINUTE);
int hour = rightnow.get(Calendar.HOUR);

//半徑
int R_H = 20,R_M = 4,R_S = 4;
//時針的坐標
//x ====(9-3)[0-6] (3-9)[6-0]
//y ====(12-6)[0-6] (6-12)[6-0]
int H_x ;
int H_y;
//x
if(hour == 0)
{
hour = 12;
}
if( hour >= 3 && hour <= 9 )
{
H_x = R_H*Math.abs(hour - 9);
}
else
{
if(hour > 9)
{
H_x = R_H*Math.abs(hour - 9);
}
else
{
H_x = R_H*Math.abs(hour+3);
}
}
//y
if( hour >= 6 && hour <= 12 )
{
H_y = R_H*Math.abs(hour - 12);
}
else
{
H_y = R_H*hour;
}

//分針的坐標
int M_x;
int M_y;

if(minute == 0)
{
minute = 60;
}
if( minute >= 15 && minute <= 45 )
{
M_x = R_M*Math.abs(minute - 45);
}
else
{
if(minute > 45)
{
M_x = R_M*Math.abs(minute - 45);
}
else
{
M_x = R_M*Math.abs(minute+15);
}
}
//y
if( minute >= 30 && minute < 60 )
{
M_y = R_M*Math.abs(minute - 60);
}
else
{
M_y = R_M*minute;
}

//秒針的坐標
int S_x;
int S_y;

if(second == 0)
{
second = 60;
}
if( second >= 15 && second <= 45 )
{
S_x = R_S*Math.abs(second - 45);
}
else
{
if(second > 45)
{
S_x = R_S*Math.abs(second - 45);
}
else
{
S_x = R_S*Math.abs(second+15);
}
}
//y
if( second >= 30 && second <= 60 )
{
S_y = R_S*Math.abs(second - 60);
}
else
{
S_y = R_S*second;
}

// g.drawString(String.valueOf(second),25,50);
// g.drawString(String.valueOf(minute),25,60);
// g.drawString(String.valueOf(hour),25,70);
// g.drawString(String.valueOf(H_x),25,80);
// g.drawString(String.valueOf(H_y),25,90);
g.drawOval(0,0,120,120);//距離相差10像素
g.setColor(Color.darkGray);
g.drawString("9",5,65);
g.drawString("3",110,65);
g.drawString("12",55,15);
g.drawString("6",55,115);

g.drawString("1",80,20);
g.drawString("2",100,40);
g.drawString("4",100,90);
g.drawString("5",80,110);

g.drawString("7",30,110);
g.drawString("8",10,90);
g.drawString("10",10,40);
g.drawString("11",30,20);

g.setColor(Color.red);
g.drawLine(60,60,H_x,H_y);//前一個點表示起點,另一個表示終點
g.setColor(Color.blue);
g.drawLine(60,60,M_x,M_y);
g.setColor(Color.yellow);
g.drawLine(60,60,S_x,S_y);

}
public void start()
{
if(athread == null)
{
athread = new Thread(this);
athread.start();
}
}
public void stop()
{
if(athread != null)
{
athread.interrupt();
athread = null;
}
}
public void run()
{
while(athread != null)
{
repaint();
try
{
athread.sleep(1000);
}
catch(InterruptedException e)
{

}
}
}

}

Ⅲ c語言怎麼樣編寫一個時鍾程序

c語言行敬並時鍾程序代碼如下:

#include<windows.h>

#include<math.h>

#define ID_TIMER 1//計時器ID

#define TWOPI (2*3.14159)

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR szCmdLine,int iCmdShow)

{

static TCHAR szAppName[]=TEXT("Clock"檔跡稿孫);

HWND hwnd;

MSG msg;

WNDCLASS wndclass;

wndclass.cbClsExtra=0;

wndclass.cbWndExtra=0;

wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);

wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);

wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);

wndclass.hInstance=hInstance;

wndclass.lpfnWndProc=WndProc;

wndclass.lpszClassName=szAppName;

wndclass.lpszMenuName=NULL;

wndclass.style=CS_HREDRAW|CS_VREDRAW;

if(!RegisterClass(&wndclass))

{

MessageBox(NULL,TEXT("This program requires Windows

T"),szAppName,MB_ICONERROR);

return 0;

}

hwnd=CreateWindow(szAppName,TEXT("Analog Clock"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);

ShowWindow(hwnd,iCmdShow);

UpdateWindow(hwnd);

while(GetMessage(&msg,NULL,0,0))

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}

return msg.wParam;

}

void Setsotropic(HDC hdc,int cxClient,int cyClient)

{

SetMapMode(hdc,MM_ISOTROPIC);

SetWindowExtEx(hdc,1000,1000,NULL);

SetViewportExtEx(hdc,cxClient/2,-cyClient/2,NULL);

SetViewportOrgEx(hdc,cxClient/2,cyClient/2,NULL);

}

void RotatePoint(POINT pt[],int iNum,int iAngle)

{

int i;

POINT ptTemp;

for(i=0;i<iNum;i++)

{

ptTemp.x=(int)(pt[i].x*cos(TWOPI*iAngle/360)+pt[i].y*sin(TWOPI*iAngle/360));

ptTemp.y=(int)(pt[i].y*cos(TWOPI*iAngle/360)+pt[i].x*sin(TWOPI*iAngle/360));

pt[i]=ptTemp;

}

}

void DrawClock(HDC hdc)

{

int iAngle;

POINT pt[3];

for(iAngle=0;iAngle<360;iAngle+=6)

{

pt[0].x=0;

pt[0].y=900;

RotatePoint(pt,1,iAngle);

pt[2].x=pt[2].y=iAngle%5?33:100;

pt[0].x-=pt[2].x/2;

pt[0].y-=pt[2].y/2;

pt[1].x=pt[0].x+pt[2].x;

pt[1].y=pt[0].y+pt[2].y;

SelectObject(hdc,GetStockObject(BLACK_BRUSH));

Ellipse(hdc,pt[0].x,pt[0].y,pt[1].x,pt[1].y );

}

}

void DrawHands(HDC hdc,SYSTEMTIME *pst,BOOL fChange)

{

static POINT pt[3][5]={0,-150,100,0,0,600,-100,0,0,-150, 0,-200,50,0,0,800,-50,0,0,-200, 0,0,0,0,0,0,0,0,0,800 };

int i,iAngle[3];

POINT ptTemp[3][5];

iAngle[0]=(pst->wHour*30)%360+pst->wMinute/2;

iAngle[1]=pst->wMinute*6;

iAngle[2]=pst->wSecond*6;

memcpy(ptTemp,pt,sizeof(pt));

for(i=fChange?0:2;i<3;i++)

{

RotatePoint(ptTemp[i],5,iAngle[i]);

Polyline(hdc,ptTemp[i],5);

}

}

LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)

{

static int cxClient,cyClient;

static SYSTEMTIME stPrevious;

BOOL fChange;

HDC hdc;

PAINTSTRUCT ps;

SYSTEMTIME st;

switch(message)

{

case WM_CREATE:

SetTimer(hwnd,ID_TIMER,1000,NULL);

GetLocalTime(&st);

stPrevious=st;

return 0;

case WM_SIZE:

cxClient=LOWORD(lParam);

cyClient=HIWORD(lParam);

return 0;

case WM_TIMER:

GetLocalTime(&st);

fChange=st.wHour!=stPrevious.wHour||st.wMinute!=stPrevious.wMinute;

hdc=GetDC(hwnd);

Setsotropic(hdc,cxClient,cyClient);

SelectObject(hdc,GetStockObject(WHITE_PEN));

DrawHands(hdc,&stPrevious,fChange);

SelectObject(hdc,GetStockObject(BLACK_PEN));

DrawHands(hdc,&st,TRUE);

stPrevious=st;

return 0;

case WM_PAINT:

hdc=BeginPaint(hwnd,&ps);

Setsotropic(hdc,cxClient,cyClient);

DrawClock(hdc);

DrawHands(hdc,&stPrevious,TRUE);

EndPaint(hwnd,&ps);

return 0;

case WM_DESTROY:

KillTimer(hwnd,ID_TIMER);

PostQuitMessage(0);

return 0;

}

return DefWindowProc(hwnd,message,wParam,lParam);

}

閱讀全文

與編程出來的時鍾怎麼寫相關的資料

熱點內容
網路輿情應對的基本理念是什麼 瀏覽:433
word2007層次結構 瀏覽:456
去掉文件名的數字 瀏覽:713
word公司 瀏覽:710
淘寶店數據包怎麼上傳 瀏覽:341
pbt文件 瀏覽:204
HX基礎編程怎麼改變字體 瀏覽:876
怎麼開網路教學 瀏覽:915
630升級工程武器 瀏覽:936
用換機助手接收的軟體文件在哪找 瀏覽:282
閱達app一教一輔五年級有哪些 瀏覽:7
win10系統用f2調節音量 瀏覽:19
壓縮文件密碼器 瀏覽:840
線下活動數據分析有哪些 瀏覽:314
助聽器插片式編程線如何連接 瀏覽:293
怎麼刪除系統休眠文件 瀏覽:914
搜索文件內容中包含的文字並替換 瀏覽:542
微信相冊程序圖標 瀏覽:714
win8怎麼顯示文件格式 瀏覽:547
文件伺服器中毒 瀏覽:721

友情鏈接