📄 newclock.java
字号:
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class newClock extends JFrame {
double run=0;
public newClock()
{
setTitle("Owner Clock");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
clockPanel ab = new clockPanel();
this.add(ab);
setSize(500,500);
}
public static void main(String[] args)
{
newClock e = new newClock();
e.setVisible(true);
}
class clockPanel extends JPanel{
public void paint(Graphics g)
{
//插入图片
ImageIcon im=new ImageIcon(getClass().getResource("123.jpg"));
g.drawImage(im.getImage(),0,0,this);
//时钟的钟面
int r=150;
Insets insets = getInsets();
int L = insets.left , T = insets.top;
g.setColor(Color.red);
int x0 = L+100+r , y0 = T+50+r;
g.drawOval(L+100, T+50, 2*r, 2*r);
g.drawOval(x0-r+15, y0-r+15,2*(r-15),2*(r-15));
//时钟上的刻度
g.setColor(Color.green);
for(int i=1,ang=60;i<=12;i++,ang-=30)
{
int x,y;
x=(int)(r*Math.cos(Math.PI*ang/180)+x0);
y=(int)(y0-r*Math.sin(Math.PI*ang/180));
if(0<=ang&&ang<90) g.drawString(""+i, x-10 ,y+10);
if(90<=ang&&ang<180) g.drawString(""+i, x ,y+10);
if(180<=ang&&ang<270) g.drawString(""+i, x ,y);
if(270<=ang&&ang<360) g.drawString(""+i, x-10 ,y);
if(ang==0) ang=360;
}
// 获取Windows时间
int hour,min,sec,year,month,data,week;
String st;
String weekly="";
Calendar now = Calendar.getInstance();
hour = now.get(Calendar.HOUR_OF_DAY);
min = now.get(Calendar.MINUTE);
sec = now.get(Calendar.SECOND);
year = now.get(Calendar.YEAR);
month = now.get(Calendar.MONTH)+1;
data = now.get(Calendar.DAY_OF_MONTH);
week = now.get(Calendar.WEEK_OF_MONTH);
switch(week){
case 1:weekly = "星期日";break;
case 2:weekly = "星期一";break;
case 3:weekly = "星期二";break;
case 4:weekly = "星期三";break;
case 5:weekly = "星期四";break;
case 6:weekly = "星期五";break;
case 7:weekly = "星期六";break;
}
// 走马灯显示
g.setColor(Color.black);
if(hour<10) st="0"+hour; else st=""+hour;
if(min<10) st=st+" :0"+min; else st=st+" :"+min;
if(sec<10) st=st+" :0"+sec; else st=st+" :"+sec;
st=weekly+" 日期:"+year+"年"+month+"月"+data+"日 当前时间:"+st;
Font f=new Font(st,Font.PLAIN+Font.BOLD,15);
g.setFont(f);
run+=0.1;
if(run>500) run=-150;
g.drawString(st,(int)run,T+400);
// 秒针
double s=2*Math.PI*(sec-15)/60;
int sx,sy;
g.setColor(Color.red);
sx=(int)(x0+Math.cos(s)*(r-10));
sy=(int)(y0+Math.sin(s)*(r-10));
g.drawLine(x0, y0,sx ,sy );
// 分针
double m=2*Math.PI*(min+sec/60-15)/60;
g.setColor(Color.gray);
int mr1=120,mr2=90,mr3=10;
int x_m[]={x0+(int)(mr3* Math.cos(m+Math.PI/2)),
x0+(int)(mr3* Math.cos(m-Math.PI/2)),
x0 + (int)(mr2* Math.cos(m-Math.PI/20)),
x0 + (int)(mr1* Math.cos(m)),
x0 + (int)(mr2* Math.cos(m+Math.PI/20))};
int y_m[]={y0+(int)(mr3* Math.sin(m+Math.PI/2)),
y0+(int)(mr3* Math.sin(m-Math.PI/2)),
y0 + (int)(mr2* Math.sin(m-Math.PI/20)),
y0 + (int)(mr1* Math.sin(m)),
y0 + (int)(mr2* Math.sin(m+Math.PI/20))};
g.fillPolygon(x_m, y_m, 5);
// 时针
double h=2*Math.PI*(hour+min/60+sec/360-15)/12;
g.setColor(Color.cyan);
int hr1=90,hr2=60,hr3=10;
int x_h[]={x0+(int)(hr3* Math.cos(h+Math.PI/2)),
x0 + (int)(hr3* Math.cos(h-Math.PI/2)),
x0 + (int)(hr2* Math.cos(h-Math.PI/12)),
x0 + (int)(hr1* Math.cos(h)),
x0 + (int)(hr2* Math.cos(h+Math.PI/12))};
int y_h[]={y0+(int)(hr3* Math.sin(h+Math.PI/2)),
y0 + (int)(hr3* Math.sin(h-Math.PI/2)),
y0 + (int)(hr2* Math.sin(h-Math.PI/12)),
y0 + (int)(hr1* Math.sin(h)),
y0 + (int)(hr2* Math.sin(h+Math.PI/12))};
g.fillPolygon(x_h, y_h, 5);
repaint();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -