package package002;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
public class Demo002旋转时钟 extends JFrame{
TranPanel panel;
public static void main(String[] args) {
Demo002旋转时钟 frame = new Demo002旋转时钟();
frame.setVisible(true);
}
public Demo002旋转时钟() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(300, 5, 1000, 1030);
panel=new TranPanel();
setContentPane(panel);
new Thread(panel).start();
}
}
class TranPanel extends JPanel implements Runnable{
int h=11;
int min=59;
int miao=56;
public void paint(Graphics g)
{
g.fillOval(500-12, 500-12, 14, 14);
dynamicClock(g,h,min,miao);
}
public void dynamicClock(Graphics g,int h,int min,int miao) {
drawtime(g,"秒",60,860,6,miao);//画笔 时分秒 循环次数 画的x坐标 旋转角度
drawtime(g,"分",60,740,6,min);
drawtime(g,"时",24,620,15,h);
}
public void drawtime(Graphics g,String time,int num,int drawx,int angle,int startnum)
{
Graphics2D g2 = (Graphics2D)g ;
for(int i=0+startnum;i<=num+startnum;i++) {
Image img = new ImageIcon("E:\\"+time+"\\"+i%num+time+".jpg").getImage();
g2.translate(500,485);
g2.rotate(Math.toRadians(angle));
g2.translate(-500,-485);
g2.translate(drawx,485);
g2.drawImage(img,0,0,this) ;
g2.translate(-drawx,-485);
}
}
@Override
public void run() {
while(true) {
try {
Thread.sleep(1000);
miao+=1;
if(miao>=60) {
miao=0;
min+=1;
}
if(min>=60) {
min=0;
h+=1;
}
if(h>=24) {
h=1;
}
System.out.println("当前时间:"+h+"时"+min+"分"+miao+"秒");
repaint();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}