j2sdk1.4.2_01\demo\applets\Clock在你安装的j2sdk下面就有。
解决方案 »
- 汽车的刹车抽象行为,是放在Abstract class Vehicle 里还是Interface Brake 里?
- java 版本的qq
- 这样写法多了个static是不是多余的?
- 继承AbstractTableModel 问题?
- Java精华积累:每个初学者都应该搞懂的问题!
- 非计算机专业学JAVA多久可以找到程序员的工作?
- 关于java多线程的一个问题
- 郁闷啊,昨晚停电,帖子没及时结啊,散分至歉(回头来结帖)
- Exception in thread "main" java.lang.NoSuchMethodError: main
- 那有 jdb下载?
- java 开发工具 NetBeans 、IntelliJ IDEA 、JBuilder 的比较
- 发现tomcat必须重起电脑才能应用改动的配置文件(4.1.24/4.1.27或以上版本)
import java.util.*;
import java.awt.*;
import java.applet.*;
import java.text.*;public class Clock extends Applet implements Runnable {
private volatile Thread timer;
private int lastxs, lastys, lastxm,
lastym, lastxh, lastyh;
private SimpleDateFormat formatter;
private String lastdate;
private Font clockFaceFont;
private Date currentDate;
private Color handColor;
private Color numberColor; public void init() {
int x,y;
lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0;
formatter = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy",
Locale.getDefault());
currentDate = new Date();
lastdate = formatter.format(currentDate);
clockFaceFont = new Font("Serif", Font.PLAIN, 14);
handColor = Color.blue;
numberColor = Color.darkGray; try {
setBackground(new Color(Integer.parseInt(getParameter("bgcolor"),
16)));
} catch (NullPointerException e) {
} catch (NumberFormatException e) {
}
try {
handColor = new Color(Integer.parseInt(getParameter("fgcolor1"),
16));
} catch (NullPointerException e) {
} catch (NumberFormatException e) {
}
try {
numberColor = new Color(Integer.parseInt(getParameter("fgcolor2"),
16));
} catch (NullPointerException e) {
} catch (NumberFormatException e) {
}
resize(300,300);
} public void paint(Graphics g) {
int xh, yh, xm, ym, xs, ys;
int s = 0, m = 10, h = 10;
int xcenter = 80, ycenter = 55;
String today; currentDate = new Date();
formatter.applyPattern("s");
try {
s = Integer.parseInt(formatter.format(currentDate));
} catch (NumberFormatException n) {
s = 0;
}
formatter.applyPattern("m");
try {
m = Integer.parseInt(formatter.format(currentDate));
} catch (NumberFormatException n) {
m = 10;
}
formatter.applyPattern("h");
try {
h = Integer.parseInt(formatter.format(currentDate));
} catch (NumberFormatException n) {
h = 10;
}
xs = (int) (Math.cos(s * Math.PI / 30 - Math.PI / 2) * 45 + xcenter);
ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 45 + ycenter);
xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 40 + xcenter);
ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 40 + ycenter);
xh = (int) (Math.cos((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30
+ xcenter);
yh = (int) (Math.sin((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30
+ ycenter);
g.setFont(clockFaceFont);
g.setColor(handColor);
g.drawArc(xcenter-50, ycenter-50, 100, 100, 0, 360);
g.setColor(numberColor);
g.drawString("9", xcenter-45, ycenter+3);
g.drawString("3", xcenter+40, ycenter+3);
g.drawString("12", xcenter-5, ycenter-37);
g.drawString("6", xcenter-3, ycenter+45); formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy");
today = formatter.format(currentDate); g.setColor(getBackground());
if (xs != lastxs || ys != lastys) {
g.drawLine(xcenter, ycenter, lastxs, lastys);
g.drawString(lastdate, 5, 125);
}
if (xm != lastxm || ym != lastym) {
g.drawLine(xcenter, ycenter-1, lastxm, lastym);
g.drawLine(xcenter-1, ycenter, lastxm, lastym);
}
if (xh != lastxh || yh != lastyh) {
g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
g.drawLine(xcenter-1, ycenter, lastxh, lastyh);
} g.setColor(numberColor);
g.drawString(today, 5, 125);
g.drawLine(xcenter, ycenter, xs, ys);
g.setColor(handColor);
g.drawLine(xcenter, ycenter-1, xm, ym);
g.drawLine(xcenter-1, ycenter, xm, ym);
g.drawLine(xcenter, ycenter-1, xh, yh);
g.drawLine(xcenter-1, ycenter, xh, yh);
lastxs = xs; lastys = ys;
lastxm = xm; lastym = ym;
lastxh = xh; lastyh = yh;
lastdate = today;
currentDate = null;
} public void start() {
timer = new Thread(this);
timer.start();
} public void stop() {
timer = null;
} public void run() {
Thread me = Thread.currentThread();
while (timer == me) {
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
}
repaint();
}
} public void update(Graphics g) {
paint(g);
} public String getAppletInfo() {
return "Title: A Clock \n"
+ "Author: Rachel Gollub, 1995 \n"
+ "An analog clock.";
}
public String[][] getParameterInfo() {
String[][] info = {
{"bgcolor", "hexadecimal RGB number",
"The background color. Default is the color of your browser."},
{"fgcolor1", "hexadecimal RGB number",
"The color of the hands and dial. Default is blue."},
{"fgcolor2", "hexadecimal RGB number",
"The color of the second hand and numbers. Default is dark gray."}
};
return info;
}
}
没有上面那个标准例子的好。主要是没有底下的数字时间显示,只有一个时钟而已!
import java.util.*;
import java.awt.*;
import java.applet.*;public class Clock extends Applet implements Runnable
{
Thread runner;
Image img;
Graphics gra;
int xs, ys, xm, ym, xh, yh; public void init()
{
img = createImage(this.getSize().width,this.getSize().height);
gra= img.getGraphics();
}
public void start()
{
if(runner == null)
{
runner = new Thread(this);
runner.start();
}
}
public void stop()
{
if(runner !=null)
{
runner.stop();
runner = null;
}
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
g.drawImage(img,0,0,this);
gra.setColor(Color.green);
gra.fillOval(100,100,60,60);
gra.setColor(Color.red);
gra.drawLine(130,130,130+xs,130-ys);
gra.setColor(Color.black);
gra.drawLine(130,130,130+xm,130-ym);
gra.setColor(Color.blue);
gra.drawLine(130,130,130+xh,130-yh);
}
public void run()
{
while(true)
{
Date d = new Date(); int s = d.getSeconds()+1;
double m = d.getMinutes()+1+ (double)s/60;
double h = d.getHours()+1+m/60;
xs =(int)(Math.sin(s*6*Math.PI/180)*29);
ys =(int)(Math.cos(s*6*Math.PI/180)*29); xm =(int)(Math.sin(m*6*Math.PI/180)*24);
ym =(int)(Math.cos(m*6*Math.PI/180)*24);
xh =(int)(Math.sin(h*30*Math.PI/180)*18);
yh =(int)(Math.cos(h*30*Math.PI/180)*18);
repaint();
try
{
Thread.sleep(1000);
}
catch(InterruptedException e) {}
}
}
}