java时钟动画代码 java编写时钟程序

怎么用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);
Calendarrightnow = 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 = 3hour = 9 )
{
H_x = R_H*Math.abs(hour - 9);
}
else
{
if(hour9)
{
H_x = R_H*Math.abs(hour - 9);
}
else
{
H_x = R_H*Math.abs(hour 3);
}
}
//y
if( hour = 6hour = 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 = 15minute = 45 )
{
M_x = R_M*Math.abs(minute - 45);
}
else
{
if(minute45)
{
M_x = R_M*Math.abs(minute - 45);
}
else
{
M_x = R_M*Math.abs(minute 15);
}
}
//y
if( minute = 30minute60 )
{
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 = 15second = 45 )
{
S_x = R_S*Math.abs(second - 45);
}
else
{
if(second45)
{
S_x = R_S*Math.abs(second - 45);
}
else
{
S_x = R_S*Math.abs(second 15);
}
}
//y
if( second = 30second = 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)
{
}
}
}
}
java动画 图形失真的问题(例程:时钟程序) 求解决 谢谢了!点阵的 失真是很正常的了 。。。。。。。。换个视网膜的屏试试,可能会好很多 。
JAVA用SWT写时钟代码#include time.h
#include stdio.h
#include stdlib.h
#define BUF_SIZE 80
int main()
{
time_t t;
struct tm *tm;
char s[BUF_SIZE];
if(time(t) == ((time_t)-1)) return 1;
tm = localtime(t);
if(!tm) return 1;
strftime(s, sizeof(s), ":%H:%M:%S", tm);
printf("%s\n", s);
system("pause");
return 0;
}
求java七段数字时钟的源代码其实这个很有趣 。自己写很有意思
先画每个数字.....
每个数字图样分7段java时钟动画代码,给每一段编号 ,
51
6
42
3
然后设计10个长度为7java时钟动画代码的数组,作为每个数字java时钟动画代码的字体
boolean[][] font={
{true, true,true,true,true,true,false},//数字0的字样控制开关
{false, true,true,false,false,false,false},//数字1的字样控制开关
//等等等等
{true, true,true,true,false,true,true},//数字9的字样控制开关
}
或者,熟练的就用一个byte数组就够了
byte[] font={
0x3F, ////数字0的字样控制开关
0x06,//数字1的字样控制开关
//等等
0x6F//数字9的字样控制开关
}
然后画几个黑段拼出一个数字,对应数组显示,用6个数字实例拼出最终效果,
这是模拟真机的写法.
如果要简单,直接拿0-9共10张图片对应数值就可以了 。
JAVA画时钟代码import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import sun.util.calendar.Gregorian;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class ClockPointer extends JFrame{
int x, y, x0, y0, r, h, olds_x, olds_y, oldm_x, oldm_y, oldh_x, oldh_y,
ss,mm, hh, old_m, old_h, ang;
final double RAD = Math.PI/180;
public ClockPointer(){
super("Java时钟");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Image image = getToolkit().getImage("clock.gif");
setIconImage(image);
setSize(400,400);
setBackground(Color.white);
//setLocation(300,150);
this.setLocationRelativeTo(null);
setResizable(true);
int delay = 1000;
//创建一个监听事件
setVisible(true);
ActionListener drawClock = new ActionListener(){
public void actionPerformed(ActionEvent evt){
repaint();
}
};
//创建一个时间计数器java时钟动画代码,每一秒触发一次
new Timer(delay, drawClock).start();
}
java.text.SimpleDateFormat fmTime = new java.text.SimpleDateFormat("HH:mm:ss");
//绘制图形
public void paint(Graphics g){
super.paint(g);
g.setFont(null);
Graphics2D g2D = (Graphics2D)g;
Insets insets = getInsets();
int L = insets.left/2, T = insets.top/2;
h = getSize().height;
g.setColor(Color.white);
//画圆
g2D.setStroke(new BasicStroke(2.0f));
g.setColor(Color.gray);
g.drawOval(L 40, T 40, h-80, h-80);
r = h/2 - 40;
x0 = 40r - 5L;
y0 = 40r - 5 - T;
ang = 60;
//绘制时钟上java时钟动画代码的12个字
for(int i = 1;i = 12;i){
x = (int)((r 10)*Math.cos(RAD*ang) x0);
y = (int)((r 10)*Math.sin(RAD*ang) y0);
g.setColor(Color.black);
g.drawString("" i, x, h-y);
ang -=30;
}
//获得现在java时钟动画代码的时间
Calendar now = new GregorianCalendar();
int nowh = now.get(Calendar.HOUR_OF_DAY);
int nowm = now.get(Calendar.MINUTE);
int nows = now.get(Calendar.SECOND);
String st=fmTime.format(now.getTime());
//在窗体上显示时间
g.setColor(Color.pink);
g.fillRect(L, T, 50, 28);
g.setColor(Color.blue);
g.drawString(st,L 2,T 26);
//计算时间与度数java时钟动画代码的关系
ss = 90 - nows*6;
mm = 90 - nowm*6;
hh = 90 - nowh*30 - nowm/2;
x0 = r 40 L;
y0 = r 40 T;
g2D.setStroke(new BasicStroke(1.2f));
//擦除秒针
//if(olds_x0){
//g.setColor(getBackground());
//// g.setColor(Color.gray);
//g.drawLine(x0, y0, olds_x, h-olds_y); // (?)
//}
//绘制秒针
x = (int)(r*0.9*Math.cos(RAD*ss)) x0;
y = (int)(r*0.9*Math.sin(RAD*ss)) y0-2*T;
g.setColor(Color.yellow);
g.drawLine(x0, y0, x, h-y);
olds_x = x;
olds_y = y;
g2D.setStroke(new BasicStroke(2.2f));
//擦除分针
//if(old_m!=mm){
//g.setColor(getBackground());
//g.drawLine(x0,y0,oldm_x,h-oldm_y);
//}
//绘制分针
x = (int)(r*0.7*Math.cos(RAD*mm)) x0;
y = (int)(r*0.7*Math.sin(RAD*mm)) y0-2*T;
g.setColor(Color.green);
g.drawLine(x0,y0,x,h-y);
oldm_x = x;
oldm_y = y;
old_m = mm;
g2D.setStroke(new BasicStroke(3.2f));
//擦除时针
//if(old_h!=hh){
//g.setColor(getBackground());
//g.drawLine(x0,y0,oldh_x,h-oldh_y);
//}
//绘制时针
x = (int)(r*0.5*Math.cos(RAD*hh)) x0;
y = (int)(r*0.5*Math.sin(RAD*hh)) y0-2*T;
g.setColor(Color.red);
g.drawLine(x0,y0,x,h-y);
oldh_x = x;
oldh_y = y;
old_h = hh;
}
public static void main(String[] args){
new ClockPointer();
}
}
//整理一下
【java时钟动画代码 java编写时钟程序】关于java时钟动画代码和java编写时钟程序的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读