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();
}
}
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();
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);
}
});
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;
}
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);
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;