?? clock.java
字號:
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.text.*;
import java.net.*;
import java.awt.image.*;
class RotateG
{
private Graphics g;
private int ox = 0;
private int oy = 0;
private double radians = 0.0;
private double cos = 1.0;
private double sin = 0.0;
public RotateG(Graphics g)
{
this.g = g.create();
}
public void setOrigin(int x, int y)
{
ox = x;
oy = y;
}
public void setAngle(double a)
{
radians = (a * Math.PI) / 180;
cos = Math.cos(radians);
sin = Math.sin(radians);
}
public void setColor(Color c)
{
g.setColor(c);
}
public int rotate_x(int x, int y)
{
return ((int) (ox + x * cos - y * sin));
}
public int rotate_y(int x, int y)
{
return ((int) (oy + y * cos + x * sin));
}
public void drawLine(int x1, int y1, int x2, int y2)
{
g.drawLine(rotate_x(x1, y1),
rotate_y(x1, y1),
rotate_x(x2, y2),
rotate_y(x2, y2));
}
public void drawOval(int x, int y, int width, int height)
{
g.drawOval(x,y,width,height);
}
public void fillOval(int x, int y, int width, int height)
{
g.fillOval(x,y,width,height);
}
public void drawPolygon(int x[], int y[], int n)
{
int new_x[] = new int[n];
int new_y[] = new int[n];
for (int i = 0; i < n; i++)
{
new_x[i] = rotate_x(x[i], y[i]);
new_y[i] = rotate_y(x[i], y[i]);
}
g.drawPolygon(new_x, new_y, n);
}
public void fillPolygon(int x[], int y[], int n)
{
int new_x[] = new int[n];
int new_y[] = new int[n];
for (int i = 0; i < n; i++)
{
new_x[i] = rotate_x(x[i], y[i]);
new_y[i] = rotate_y(x[i], y[i]);
}
g.fillPolygon(new_x, new_y, n);
}
public void drawImage(Image img,int x, int y,ImageObserver observer)
{
g.drawImage(img,x,y,observer);
}
}
public class Clock extends Applet implements Runnable
{
private Date date;
private int iHour,iMinute,iSecond;
private Thread thread = null;
private Image offI,backG;
private Graphics offG;
private int ixHour[]={4,4,0,-4,-4}
,iyHour[]={0,-30,-35,-30,0}
,ixMinute[]={3,3,0,-3,-3}
,iyMinute[]={0,-45,-50,-45,0};
public void init()
{
this.setLayout(null);
date = new Date();
SimpleDateFormat df = new SimpleDateFormat("'現(xiàn)在時刻' hh:mm:ss");
String lasttime = df.format(date);
offI = createImage(300,300);
offG = offI.getGraphics();
backG = getImage(getCodeBase(),"Clock.jpg");
}
public void start()
{
if(thread == null)
{
thread = new Thread(this);
thread.start();
}
}
public void run()
{
RotateG g = new RotateG(offG);
while(true)
{
try
{
thread.sleep(1000);
date = new Date();
SimpleDateFormat df = new SimpleDateFormat("'現(xiàn)在時刻' hh:mm:ss");
String lasttime = df.format(date);
System.out.println(lasttime);
iSecond = date.getSeconds();
iMinute = date.getMinutes();
iHour = date.getHours();
offG.setColor(Color.white);
offG.fillRect(0,0,300,300);
g.setOrigin(0,0);
g.drawImage(backG,0,0,this);
g.setOrigin(107,107);
System.out.println(iHour);
g.setAngle(iHour*30+iMinute*0.5);
g.setColor(Color.green);
g.fillPolygon(ixHour,iyHour,5);
g.setAngle(iMinute*6);
g.setColor(Color.red);
g.fillPolygon(ixMinute,iyMinute,5);
g.setAngle(iSecond*6);
g.setColor(Color.blue);
g.drawLine(0,0,0,-65);
g.setColor(Color.black);
g.fillOval(102,102,10,10);
repaint();
}
catch(InterruptedException e){}
}
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
g.drawImage(offI,0,0,this);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -