package sawat.loupe;
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.*;
import javax.swing.*;
import javax.swing.border.Border;
public class Loupe
extends JFrame {
private static final int SLIDER_ACCURACY = 5;
private static final int INITIAL_SCALE = 3;
private static final int MAX_SCALE = 6;
private static final int MIN_SCALE = 0;
Loupe f = new Loupe();
f.setAlwaysOnTop(true);
f.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private boolean captureEnable = true;
private final Robot robot;
private final JLabel captureLabel;
private GridMode gridMode = GridMode.GUIDE_LINE;
super("Loupe");
this.
robot =
new Robot();
robot.setAutoDelay(0);
this.
captureLabel =
new JLabel();
captureLabel.
setPreferredSize(new Dimension(200,
200));
text.setText("Press the space key");
MIN_SCALE * SLIDER_ACCURACY,
MAX_SCALE * SLIDER_ACCURACY,
INITIAL_SCALE * SLIDER_ACCURACY);
initSliderLabels();
initEventListener();
initTimer();
}
private void initSliderLabels() {
Hashtable<Integer, JLabel> labels = new Hashtable<Integer, JLabel>();
for (int i = MIN_SCALE, n = MAX_SCALE; i <= n; i++) {
labels.put(i * SLIDER_ACCURACY,
new JLabel("×" +
(1 <<
(i
))));
// new JLabel(String.format("× %.1f", Math.pow(2, i)))); // if(MIN_SCALE < 0)
}
slider.setLabelTable(labels);
slider.setPaintLabels(true);
}
if (i < 0x10)
buffer.append('0');
buffer.
append(Integer.
toHexString(i
));
}
protected void capture() {
Point location = MouseInfo.
getPointerInfo().
getLocation();
.pow(2, (double) slider.getValue() / SLIDER_ACCURACY);
int width = (int) (captureLabel.getWidth() / scale);
int height = (int) (captureLabel.getHeight() / scale);
if (width <= 0 || height <= 0) {
return;
}
location.x - width / 2, location.y - width / 2, width, height));
captureLabel.setIcon(new ExpandImageIcon(image, scale, gridMode));
}
buffer.append('#');
appendHex(buffer, color.getRed());
appendHex(buffer, color.getGreen());
appendHex(buffer, color.getBlue());
return buffer.toString();
}
protected void copyToClipbord() {
text.setText("Copty to Clipboard!");
public void run() {
.getSystemClipboard();
Border border = captureLabel.
getBorder();
Insets insets = border.
getBorderInsets(captureLabel
);
int w = captureLabel.getWidth() - insets.left - insets.right;
int h = captureLabel.getHeight() - insets.top - insets.bottom;
try {
graphics.translate(-insets.left, -insets.top);
captureLabel.paint(graphics);
} finally {
graphics.dispose();
}
ImageAndTextSelection selection = new ImageAndTextSelection(bi,
pickUp());
clipboard.setContents(selection, selection);
}
});
}
private void initEventListener() {
public void eventDispatched
(AWTEvent event
) {
if (event.
getID() ==
KeyEvent.
KEY_PRESSED) {
switch (ke.getKeyChar()) {
case 'l': toggleCaptureEnable(); break;
case 'c': copyToClipbord(); break;
case 'g': changeGridMode(); break;
case 'f': capture(); break;
default: pickUp();
}
}
else if(event.getID() == MouseWheelEvent.MOUSE_WHEEL) {
MouseWheelEvent mwe = (MouseWheelEvent) event;
slider.setValue(slider.getValue() +
mwe.getWheelRotation());
}
}
}
private void initTimer() {
@Override
public void run() {
if (captureEnable)
capture();
}
}, 500, 100);
}
Point location = MouseInfo.
getPointerInfo().
getLocation();
Color color = robot.
getPixelColor(location.
x, location.
y);
String hexColor = toHexString
(color
);
text.setText(hexColor);
text.setBorder(border);
return hexColor;
}
protected void toggleCaptureEnable() {
captureEnable = !captureEnable;
text.setText((this.captureEnable ? "The screen capture was begun."
: "The screen capture was locked."));
}
protected void changeGridMode() {
GridMode[] modes = GridMode.values();
gridMode = modes[(gridMode.ordinal()+1)%modes.length];
text.setText("GridMode is chaged.");
}
enum GridMode { GUIDE_LINE, GRID, NONE };
static class ExpandImageIcon
extends ImageIcon {
private static final Color guidLineColor =
new Color(0,
0,
255,
128);
private static final Color gridColor =
new Color(128,
128,
128,
128);
private final double scale;
private final GridMode gridMode;
public ExpandImageIcon
(Image image,
double scale, GridMode gridMode
) {
super(image);
this.scale = scale;
this.gridMode = gridMode;
}
@Override
public int getIconHeight() {
return (int) (super.getIconHeight() * scale);
}
@Override
public int getIconWidth() {
return (int) (super.getIconWidth() * scale);
}
private void paintGuideLine
(Graphics g,
int x,
int y,
Image image
) {
g.setColor(guidLineColor);
int w = image.getWidth(getImageObserver());
int h = image.getHeight(getImageObserver());
if (w < 0 || h < 0) {
return;
}
int xc = x + (int) (scale * (w / 2));
int yc = y + (int) (scale * (h / 2));
g.drawLine(xc, y, xc, y + getIconHeight());
g.drawLine(x, yc, x + getIconWidth(), yc);
int xcPlus1 = xc + (int) scale;
int ycPlus1 = yc + (int) scale;
g.drawLine(xcPlus1, y, xcPlus1, y + getIconHeight());
g.drawLine(x, ycPlus1, x + getIconWidth(), ycPlus1);
}
@Override
Image image = getImage
();
g.drawImage(image, x, y, getIconWidth(), getIconHeight(), c);
switch (gridMode) {
case GUIDE_LINE : paintGuideLine(g, x, y, image); break;
case GRID : paintGridLine(g, x, y); break;
case NONE: break;
default : throw new AssertionError();
}
}
private void paintGridLine
(Graphics g,
int x,
int y
) {
g.setColor(gridColor);
for (int xi = 0; xi * scale < x + getIconWidth(); xi++) {
g.drawLine(x + (int)(xi*scale), y,
x + (int)(xi*scale), y + getIconHeight());
}
for (int yi = 0; yi * scale < y + getIconHeight(); yi++) {
g.drawLine(x, y + (int)(yi*scale),
x + getIconWidth(), y + (int)(yi*scale));
}
}
}
public ImageAndTextSelection
(Image image,
String text
) {
this.image = image;
this.hexColor = text;
}
return image;
} else if (DataFlavor.
stringFlavor.
equals(flavor
)) {
return hexColor;
}
}
}
public boolean isDataFlavorSupported
(DataFlavor flavor
) {
}
image = null;
}
}
}