package com.clock;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;
import javax.imageio.*;
import com.sun.jna.examples.*;
import java.io.*;
import java.util.*;
import java.net.URL;
public class Test {
public static void main(String[] args) {
System.out.println(Test.class.getClassLoader().getResource("img"));
System.setProperty("sun.java2d.noddraw", "true");
JFrame frame = new ClockFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.toFront();
}
}
class ClockFrame extends JFrame {
private ClockPanel mainPanel;
private Image tardImg;
private Image hImg;
private Image mImg;
private Image sImg;
private Point offset;
private JPopupMenu menu;
public ClockFrame(Image tardImg, Image hImg, Image mImg, Image sImg) {
this.tardImg = tardImg;
this.hImg = hImg;
this.mImg = mImg;
this.sImg = sImg;
this.setUndecorated(true);
init();
WindowUtils.setWindowTransparent(this, true);
systemTrayInitial();
}
private void systemTrayInitial() {
if (!SystemTray.isSupported()) {
return;
}
try {
String title = "Q6桌面软件";
String company = "北京市XXX科技有限公司";
SystemTray sysTray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage(
Test.class.getClassLoader().getResource("img/trad_settings.png"));
PopupMenu popupMenu = new PopupMenu();
MenuItem closeItem = new MenuItem("关闭");
MenuItem hideItem = new MenuItem("隐藏");
MenuItem displayItem = new MenuItem("显示");
popupMenu.add(closeItem);
popupMenu.add(hideItem);
popupMenu.add(displayItem);
closeItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.exit(0);
}
});
hideItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if(isVisible()) {
setVisible(false);
}
}
});
displayItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if(!isVisible()) {
setVisible(true);
toFront();
}
}
});
TrayIcon trayicon = new TrayIcon(image, title + "\n" + company, popupMenu);
trayicon.setImageAutoSize(true);
//添加监听器
trayicon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if(!isVisible()) {
setVisible(true);
toFront();
}
}
});
sysTray.add(trayicon);
trayicon.displayMessage(title, company, TrayIcon.MessageType.INFO);
} catch (Exception e) {
e.printStackTrace();
}
}
public ClockFrame(URL trad, URL h, URL m, URL s) {
this(new ImageIcon(trad).getImage(),
new ImageIcon(h).getImage(),
new ImageIcon(m).getImage(),
new ImageIcon(s).getImage());
}
public ClockFrame () {
this(Test.class.getClassLoader().getResource("img/trad.png"),
Test.class.getClassLoader().getResource("img/trad_h.png"),
Test.class.getClassLoader().getResource("img/trad_m.png"),
Test.class.getClassLoader().getResource("img/trad_s.png"));
}
private void init() {
this.setSize(tardImg.getWidth(null), tardImg.getHeight(null));
this.getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
menu = new JPopupMenu();
JMenuItem closeItem = new JMenuItem("关闭");
final JMenuItem hideItem = new JMenuItem("隐藏");
JMenu transparence = new JMenu("透明度");
ButtonGroup group = new ButtonGroup();
this.makeButton("0", 1, transparence, group);
this.makeButton("20", 20, transparence, group);
this.makeButton("40", 40, transparence, group);
this.makeButton("60", 60, transparence, group);
this.makeButton("80", 80, transparence, group);
this.makeButton("100", 100, transparence, group);
hideItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
setVisible(false);
}
});
closeItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.exit(0);
}
});
menu.add(hideItem);
menu.add(closeItem);
menu.add(transparence);
this.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if(e.isPopupTrigger()) {
menu.show(ClockFrame.this, e.getX(), e.getY());
}
}
public void mousePressed(MouseEvent e) {
offset = e.getPoint();
}
});
this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
if (!SwingUtilities.isLeftMouseButton(e))
return;
Point where = e.getPoint();
where.translate(-offset.x, -offset.y);
Point loc = getLocationOnScreen();
loc.translate(where.x, where.y);
setLocation(loc.x, loc.y);
}
});
// Shape mask = new Area(new Ellipse2D.Float(0, 0, tardImg.getWidth(null), tardImg.getHeight(null)));
// WindowUtils.setWindowMask(this, mask);
initComponeent();
}
private void initComponeent() {
mainPanel = new ClockPanel(tardImg, hImg, mImg, sImg);
this.add(mainPanel);
}
private void makeButton(String name, final int value, JMenu transparence, ButtonGroup group) {
JRadioButtonMenuItem item = new JRadioButtonMenuItem(name);
group.add(item);
transparence.add(item);
item.setSelected(value == 100);
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
WindowUtils.setWindowAlpha(ClockFrame.this, value / 100F);
}
});
}
}
class ClockPanel extends JPanel {
private int hRote = 0;
private int mRote = 0;
private int sRote = 0;
private Image tardImg;
private Image hImg;
private Image mImg;
private Image sImg;
private Thread th;
public ClockPanel(Image tardImg, Image hImg, Image mImg, Image sImg) {
this.setBackground(new Color(0, 0, 0, 0));
this.tardImg = tardImg;
this.hImg = hImg;
this.mImg = mImg;
this.sImg = sImg;
this.start();
initTime();
}
private void initTime() {
Calendar c = Calendar.getInstance();
int h = c.get(Calendar.HOUR);
int m = c.get(Calendar.MINUTE);
int s = c.get(Calendar.SECOND);
sRote = s * 6;
mRote = m * 6 + sRote / 60;
hRote = h * 30 + mRote / 12;
}
private void start() {
if(th == null) {
th = new Thread() {
public void run() {
while(true) {
try {
Thread.sleep(1000);
} catch(Exception e) {}
setRotate();
}
}
};
}
th.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
int drawx = tardImg.getWidth(null);
int drawy = tardImg.getHeight(null);
int w = this.getWidth();
int h = this.getHeight();
int x = (w - drawx) / 2;
int y = (h - drawy) / 2;
g2.drawImage(tardImg, x, y, this);
drawTransImage(g, hImg, drawx, drawy, hRote);
drawTransImage(g, mImg, drawx, drawy, mRote);
drawTransImage(g, sImg, drawx, drawy, sRote);
}
public void drawTransImage(Graphics g, Image img, int drawx, int drawy, int rote) {
Graphics2D g2 = (Graphics2D)g.create();
int imgW = img.getWidth(null);
int imgH = img.getHeight(null);
int w = this.getWidth();
int h = this.getHeight();
int x = (w - imgW) / 2;
int y = (h - imgH) / 2;
RenderingHints qualityHints = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
qualityHints.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2.setRenderingHints(qualityHints);
g2.rotate(Math.toRadians(rote), drawx >> 1, drawy >> 1);
g2.drawImage(img, x, y, this);
}
private boolean flag = false;
public void setRotate() {
sRote += 6;
sRote %= 360;
if(sRote % 60 == 0) {
mRote += 1;
mRote %= 360;
flag = true;
}
if(mRote % 12 == 0 && flag) {
hRote += 1;
hRote %= 360;
flag = false;
}
this.repaint();
}
}
/*
WindowUtils.setWindowTransparent(this, true)方法会对所有引发AWTEvent的事件源转换为Component对象。
但TrayIcon并不是Compoenent。
*
**/
|
|