Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.AWTException;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.EventQueue;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.MouseInfo;
- import java.awt.Point;
- import java.awt.Robot;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseMotionListener;
- import java.awt.image.BufferedImage;
- import java.util.Date;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class Zoom extends JPanel implements MouseMotionListener {
- private static final int SIZE = 8;
- private static final int S2 = SIZE / 2;
- private static final int SCALE = 48;
- private BufferedImage img;
- private Robot robot;
- private Ticker t;
- private class Ticker extends Thread {
- private long timeRef = new Date().getTime();
- public boolean update = true;
- public JPanel jp = null;
- private int iter = 0;
- public Ticker(JPanel g) {
- super();
- jp = g;
- }
- public void done() {
- update = false;
- }
- public void run() {
- try {
- while (update == true) {
- BufferedImage img2 = new BufferedImage(SIZE, SIZE,
- BufferedImage.TYPE_INT_RGB);
- updateImg(img2);
- synchronized (img) {
- img = img2;
- }
- paintComponent(jp.getGraphics());
- StringBuilder sb = new StringBuilder();
- sb.append(++iter)
- .append(" frames in ")
- .append((double) (new Date().getTime() - this.timeRef) / 1000)
- .append("s.");
- System.out.println(sb.toString());
- }
- } catch (Exception e) {
- e.printStackTrace();
- update = false;
- }
- }
- }
- private void updateImg(BufferedImage image) {
- Graphics2D gc = null;
- try {
- Point p = MouseInfo.getPointerInfo().getLocation();
- gc = image.createGraphics();
- for (int x = 0; x < 8; x++) {
- for (int y = 0; y < 8; y++) {
- gc.setColor(robot.getPixelColor(p.x - 4 + x, p.y - 4 + y));
- gc.drawLine(x, y, x, y);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- gc.dispose();
- }
- }
- public Zoom() {
- super(true);
- this.setPreferredSize(new Dimension(SIZE * SCALE, SIZE * SCALE));
- img = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_RGB);
- t = new Ticker(this);
- try {
- robot = new Robot();
- } catch (AWTException e) {
- e.printStackTrace(System.err);
- }
- }
- @Override
- protected void paintComponent(Graphics g) {
- try {
- if (img != null)
- synchronized (img) {
- g.drawImage(img, 0, 0, getWidth(), getHeight(), null);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @Override
- public void mouseMoved(MouseEvent e) {
- Point p = e.getPoint();
- int x = p.x * SIZE / getWidth();
- int y = p.y * SIZE / getHeight();
- int c = img.getRGB(x, y);
- this.setToolTipText(x + "," + y + ": " + String.format("%08X", c));
- }
- @Override
- public void mouseDragged(MouseEvent e) {
- for (int r = 0; r < SIZE; r++) {
- for (int c = 0; c < SIZE; c++) {
- Color color = robot.getPixelColor(e.getXOnScreen() + c - S2,
- e.getYOnScreen() + r - S2);
- img.setRGB(c, r, color.getRGB());
- }
- }
- repaint();
- }
- private static void create() {
- JFrame f = new JFrame("Click & drag to zoom.");
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- Zoom zoom = new Zoom();
- f.add(zoom);
- f.pack();
- f.setVisible(true);
- zoom.addMouseMotionListener(zoom);
- zoom.t.run();
- }
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- @Override
- public void run() {
- create();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement