📄 timerdialog.java
字号:
/*
* Copyright (c) 2002-2008 TeamDev Ltd. All rights reserved.
*
* Use is subject to license terms.
*
* The complete licence text can be found at
* http://www.teamdev.com/winpack/license.jsf
*/
package teamdev.jxcapture.samples.demo.timer;
import teamdev.jxcapture.samples.demo.JxCaptureDemo;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.Timer;
import java.util.TimerTask;
/**
* Represents Timer dialog that notifies how many seconds left
* before capture.
*
* @author Ikryanov Vladimir
*/
public class TimerDialog {
private final Rectangle rectangle = new Rectangle(74, 46, 69, 23);
private final Dimension dialogSize = new Dimension(150, 75);
private ImageIcon mouseEnterImage;
private ImageIcon mouseLeaveImage;
private ImageIcon mousePressImage;
private ImageIcon currentImage;
private JWindow dialog;
private TimerWindow dialogWindow;
private TimerListener timerListener;
private Timer dialogTimer = new Timer();
private int delayTime;
private boolean mousePressed;
public TimerDialog(int delayTime) {
mouseEnterImage = new ImageIcon(JxCaptureDemo.class.getResource("resources/images/mouseEnter.png"));
mouseLeaveImage = new ImageIcon(JxCaptureDemo.class.getResource("resources/images/mouseLeave.png"));
mousePressImage = new ImageIcon(JxCaptureDemo.class.getResource("resources/images/mousePress.png"));
currentImage = mouseLeaveImage;
this.delayTime = delayTime;
dialog = new JWindow();
dialog.setContentPane(createContentPane());
dialog.setSize(0, 0);
dialog.setVisible(true);
dialogWindow = TimerWindow.createInstance(dialog);
dialog.setVisible(false);
dialog.setSize(dialogSize);
dialog.setLocation(getLocation());
}
private JComponent createContentPane() {
final JPanel contentPane = new TimerPane();
contentPane.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
updateDialog(e.getPoint());
}
public void mouseDragged(MouseEvent e) {
updateDialog(e.getPoint());
}
private void updateDialog(Point point) {
if (rectangle.contains(point)) {
currentImage = mousePressed ? mousePressImage : mouseEnterImage;
contentPane.repaint();
} else {
currentImage = mouseLeaveImage;
contentPane.repaint();
}
}
});
contentPane.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (rectangle.contains(e.getPoint())) {
mousePressed = true;
currentImage = mousePressImage;
contentPane.repaint();
}
}
public void mouseReleased(MouseEvent e) {
if (rectangle.contains(e.getPoint()) && mousePressed) {
currentImage = mouseEnterImage;
contentPane.repaint();
complete(true);
}
mousePressed = false;
}
});
return contentPane;
}
private Point getLocation() {
Rectangle screenRect = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
return new Point(screenRect.width - dialogSize.width, screenRect.height - dialogSize.height);
}
private void complete(boolean isCanceled) {
dialogTimer.cancel();
dialog.setVisible(false);
dialog.dispose();
if (timerListener != null) {
timerListener.complete(isCanceled);
}
}
private class TimerPane extends JPanel {
protected void paintComponent(Graphics g) {
if (currentImage != null) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.drawImage(currentImage.getImage(), 0, 0, null);
g2.setFont(new Font("Verdana", 0, 45));
g2.setColor(Color.WHITE);
String str = String.valueOf(delayTime);
final int blockWidth = 80;
FontMetrics fontMetrics = g2.getFontMetrics();
int width = fontMetrics.stringWidth(str);
int x = (blockWidth / 2) - (width / 2);
g2.drawString(str, x, 55);
}
}
}
public interface TimerListener {
public void complete(boolean isCanceled);
}
public void show(final TimerListener listener) throws IllegalStateException {
if (dialog.isVisible()) {
throw new IllegalStateException("Timer is already shown.");
}
timerListener = listener;
dialog.setVisible(true);
dialogWindow.showWithFadeEffect();
dialogTimer.schedule(new TimerTask() {
public void run() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
delayTime--;
dialog.repaint();
if (delayTime == 0) {
complete(false);
}
}
});
}
}, 0, 1000);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -