请问如何用java制作下面这样的时钟?谢谢.
麻烦请给源程序,谢谢.
http://www.clocklink.com/clocks/0011-ltblue.swf?TimeZone=PST&TimeFormat=hhmmssTT&
http://www.clocklink.com/clocks/0011-ltblue.swf?TimeZone=PST&TimeFormat=hhmmssTT&
麻烦请给源程序,谢谢.
http://www.clocklink.com/clocks/0011-ltblue.swf?TimeZone=PST&TimeFormat=hhmmssTT&
http://www.clocklink.com/clocks/0011-ltblue.swf?TimeZone=PST&TimeFormat=hhmmssTT&
等高人
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Calendar;
import java.util.GregorianCalendar;import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* 时间类
* @author xiaohei
*
*/
public class Clock extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L; public Clock() {
ClockPaint cp = new ClockPaint(20, 20, 70); this.add(cp);
this.setSize(200, 200);
this.setResizable(false);
this.setLocation(260, 120);
this.setTitle("小时钟");
this.setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
} public static void main(String[] s) {
new Clock();
}
}class ClockPaint extends JPanel implements Runnable {
/**
*
*/
private static final long serialVersionUID = 1L;
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));
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();
}
}
}
绝不食言。
如果能用java比较完美的播放swf文件也成
我有flash源代码。
如果能用java比较完美的播放swf文件也成
我有flash源代码。
谢谢。我有flash源代码,如果需要,我也可以发给你。
* MyClock.java
* Writting-time: 2007/10/20 @author: Shen Yinian
* If anyone has any good ideas or suggestions to improve this program, please contact with me!
**/import java.awt.*;
import java.applet.Applet;
import java.util.Date;class Line {
private int x0,y0;
private int x1,y1;
public Line() {
setLine(0,0,0,0);
}
public Line(int a,int b,int c,int d) {
setLine(a,b,c,d);
}
public void setLine(int a,int b,int c,int d) {
x0 = a;
y0 = b;
x1 = c;
y1 = d;
}
public void DrawLine(Graphics g,Color c) {
g.setXORMode(Color.WHITE);
g.drawLine(x0,y0,x1,y1);
g.setColor(c);
g.setPaintMode();
g.drawLine(x0,y0,x1,y1);
}
public void Drawneedle(Graphics g, Color c, double angle, int dt) {
g.setXORMode(Color.WHITE);
Polygon p = new Polygon();
p.addPoint(x1, y1);
p.addPoint((int)(x0 + dt * Math.cos(angle)), (int)(y0 + dt * Math.sin(angle)));
p.addPoint((int)(x0 - 2 * dt * Math.sin(angle)), (int)(y0 +2 * dt * Math.cos(angle)));
p.addPoint((int)(x0 - dt * Math.cos(angle)), (int)(y0 - dt * Math.sin(angle)));
p.addPoint(x1, y1);
g.drawPolygon(p);
g.fillPolygon(p);
g.setColor(c);
g.setPaintMode();
g.drawPolygon(p);
g.fillPolygon(p);
}
}public class MyClock extends Applet implements Runnable { final double RAD = Math.PI / 180.0;
double angleh,anglem,angles;
int xo = 150, xh, xm, xs;
int yo = 150, yh, ym, ys;
int hlength = 50;
int mlength = 60;
int slength = 70;
int r = slength + 20;
int dt = 5; int xd,yd,xe,ye,xf,yf;
Thread t1; Line hl = new Line();
Line ml = new Line();
Line sl = new Line();
public void drawPane() {
Graphics g = this.getGraphics();
g.setColor(Color.ORANGE);
g.fillOval(xo - r, yo - r, r * 2, r * 2);
g.setColor(Color.BLACK);
g.drawOval(xo - r, yo - r, r * 2, r * 2);
for (int i = 0; i < 60; i ++) {
xf = (int)(xo + r * Math.sin((i + 1) * 6 * RAD));
yf = (int)(yo - r * Math.cos((i + 1) * 6 * RAD));
xd = (int)(xo + 9 * r / 10 * Math.sin((i + 1) * 6 * RAD));
yd = (int)(yo - 9 * r / 10 * Math.cos((i + 1) * 6 * RAD));
g.drawLine(xf,yf,xd,yd);
}
g.setColor(Color.RED);
for (int i = 0; i < 12; i++) {
xf = (int)(xo + r * Math.sin((i + 1) * 30 * RAD));
yf = (int)(yo - r * Math.cos((i + 1) * 30 * RAD));
xd = (int)(xo + 7 * r / 8 * Math.sin((i + 1) * 30 * RAD));
yd = (int)(yo - 7 * r / 8 * Math.cos((i + 1) * 30 * RAD));
xe = (int)(xo + 4 * r / 5 * Math.sin((i + 1) * 30 * RAD));
ye = (int)(yo - 4 * r / 5 * Math.cos((i + 1) * 30 * RAD));
g.drawLine(xf,yf,xd,yd);
g.drawString("" + (i + 1), xe - 4, ye + 5);
}
}
public void init() {
this.setSize(300,300);
}
public void start() {
if (t1 == null) {
t1 = new Thread(this);
t1.start();
}
}
public void run() {
while (t1 != null) {
repaint();
try {
t1.sleep(1000);
}
catch (Exception e) {}
}
}
public void paint(Graphics g) {
drawPane();
Date d = new Date();
int hr = d.getHours();
int min = d.getMinutes();
int sec = d.getSeconds();
angleh = ((hr % 12 * 30) + (min / 2)) * RAD;
anglem = ((min * 6) + (sec / 10)) * RAD;
angles = sec * 6 * RAD;
xs = (int)(xo + slength * Math.sin(sec * 6 * RAD));
ys = (int)(yo - slength * Math.cos(sec * 6 * RAD));
xm = (int)(xo + mlength * Math.sin(((min * 6) + (sec / 10)) * RAD));
ym = (int)(yo - mlength * Math.cos(((min * 6) + (sec / 10)) * RAD));
xh = (int)(xo + hlength * Math.sin(((hr % 12 * 30) + (min / 2)) * RAD));
yh = (int)(yo - hlength * Math.cos(((hr % 12 * 30) + (min / 2)) * RAD));
hl.setLine(xo,yo,xh,yh);
ml.setLine(xo,yo,xm,ym);
sl.setLine(xo,yo,xs,ys);
hl.DrawLine(g,Color.BLUE);
ml.DrawLine(g,Color.GREEN);
sl.DrawLine(g,Color.RED); hl.Drawneedle(g,Color.BLUE,angleh, dt + 2);
ml.Drawneedle(g,Color.GREEN,anglem, dt + 1);
sl.Drawneedle(g,Color.RED,angles, dt);
g.setColor(Color.black);
g.fillOval(xo - 4, yo - 4, 8, 8);
g.setColor(Color.BLUE);
g.drawString(d.toLocaleString(), xo - r + 30, yo - r - 20);
}
public void stop() {
t1.stop();
t1 = null;
}
}
而且时钟实现也不难.