package 实战;
import javazoom.jl.decoder.JavaLayerException;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class StillClock extends JFrame {
Draw draw = new Draw();
BufferedImage image;
//提前加载
File file = new File("src/资源文件夹/001.jpg");//背景图片
{
try {
image = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
}
public StillClock(){
setTitle("会转的表!");
setSize(600,600);
add(draw);
Thread thread = new Thread(draw);
thread.start();
setLocationRelativeTo(null);//使居中
setVisible(true);//使可见
setDefaultCloseOperation(3);//打开后关闭
}
public static void main(String[] args) throws MalformedURLException, FileNotFoundException, JavaLayerException {
StillClock stillClock = new StillClock();
stillClock.music();
}
void music() throws FileNotFoundException, JavaLayerException { //注意,java只能播放无损音质,如.wav这种格式
try {
File f = new File("src/资源文件夹/1791.wav");
AudioClip aau = Applet.newAudioClip(f.toURI().toURL());
aau.loop(); //单曲循环
} catch (Exception e)
{
e.printStackTrace();
}
}
class Draw extends JPanel implements Runnable{
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
Calendar calendar = new GregorianCalendar();
//得到一些基本的常量
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
int week = calendar.get(Calendar.DAY_OF_WEEK);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
String[] weeks = {"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
double sTheta = second * 2 * Math.PI / 60;
double mTheta = (minute*(2*Math.PI)+sTheta)/60;
double hTheta = (hour*(2*Math.PI)+mTheta)/12;
//设置表盘时间
String data = year+"-"+month+"-"+day+" "+weeks[week-1];
String time = hour+":"+minute+":"+second;
g2.setBackground(Color.MAGENTA);
g2.clearRect(0,0,getWidth(),getHeight());//每一次repaint后清空表盘
g2.translate(getWidth()/2,getHeight()/2);//定义圆点
//添加背景图片
g2.drawImage(image, -250, -250, 500, 500, null);
g2.setStroke(new BasicStroke(5.0f));//设置线条粗细
g2.setColor(Color.RED);
g2.drawOval(-200,-200,400,400);//画出表盘的外围
//画出时间的数字形式
g2.setFont(new Font("宋体",Font.BOLD,20));
g2.setColor(Color.YELLOW);
g2.drawString(data,-50,50);
g2.drawString(time,-35,80);
//画出表盘的大刻度
for (int i=0;i<12;i++){
double theta = 2*Math.PI/12*i;
g2.rotate(theta);
g2.setStroke(new BasicStroke(3.0f));
g2.drawLine(0,180,0,200);
g2.drawString(String.valueOf(i),5,-175);
g2.rotate(2*Math.PI-theta);
}
//画出小刻度
for(int i=0;i<60;i++){
double theta = 2*Math.PI/60*i;
g2.rotate(theta);
g2.setStroke(new BasicStroke(1.5f));
g2.drawLine(0,190,0,200);
g2.rotate(2*Math.PI-theta);
}
//画针
Line2D.Float line = new Line2D.Float(0,0,0,-160);
Line2D.Float line1 = new Line2D.Float(0,0,0,-130);
Line2D.Float line2 = new Line2D.Float(0,0,0,-100);
g2.rotate(sTheta);
g2.draw(line);
g2.rotate(2*Math.PI-sTheta);
g2.rotate(mTheta);
g2.draw(line1);
g2.rotate(2*Math.PI-mTheta);
g2.rotate(hTheta);
g2.draw(line2);
g2.rotate(2*Math.PI-hTheta);
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
repaint();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
【画一个表盘】关于如何写一个会动的时间表
最新推荐文章于 2024-11-06 09:39:20 发布