Advertisement
Guest User

Margus Martsepp (Stackoverflow:Wanting a type of grid for a pixel editor) 2

a guest
Sep 19th, 2010
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. import java.awt.AWTException;
  2. import java.awt.Dimension;
  3. import java.awt.EventQueue;
  4. import java.awt.Graphics;
  5. import java.awt.MouseInfo;
  6. import java.awt.Point;
  7. import java.awt.Rectangle;
  8. import java.awt.Robot;
  9. import java.awt.event.MouseEvent;
  10. import java.awt.event.MouseMotionListener;
  11. import java.awt.image.BufferedImage;
  12. import javax.swing.JFrame;
  13. import javax.swing.JPanel;
  14.  
  15. /** @see http://stackoverflow.com/questions/3742731 */
  16. public class Zoom extends JPanel implements MouseMotionListener {
  17.  
  18.     private static final int SIZE = 12;
  19.     private static final int S2 = SIZE / 2;
  20.     private static final int SCALE = 48;
  21.     private BufferedImage img;
  22.     private Robot robot;
  23.  
  24.     public Zoom() {
  25.         super(true);
  26.         this.setPreferredSize(new Dimension(SIZE * SCALE, SIZE * SCALE));
  27.         img = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_RGB);
  28.         try {
  29.             robot = new Robot();
  30.         } catch (AWTException e) {
  31.             e.printStackTrace(System.err);
  32.         }
  33.     }
  34.  
  35.     boolean ifdone = true;
  36.  
  37.     @Override
  38.     protected void paintComponent(Graphics g) {
  39.         if (ifdone) {
  40.             ifdone = false;
  41.             g.drawImage(img, 0, 0, getWidth(), getHeight(), null);
  42.             ifdone = true;
  43.         }
  44.     }
  45.  
  46.     @Override
  47.     public void mouseMoved(MouseEvent e) {
  48.         Point p = e.getPoint();
  49.         int x = p.x * SIZE / getWidth();
  50.         int y = p.y * SIZE / getHeight();
  51.         int c = img.getRGB(x, y);
  52.         this.setToolTipText(x + "," + y + ": " + String.format("%08X", c));
  53.     }
  54.  
  55.     @Override
  56.     public void mouseDragged(MouseEvent e) {
  57.         final Point p = MouseInfo.getPointerInfo().getLocation();
  58.         Rectangle rect = new Rectangle(p.x - S2, p.y - S2, SIZE, SIZE);
  59.         img = robot.createScreenCapture(rect);
  60.         repaint();
  61.     }
  62.  
  63.     private static void create() {
  64.         JFrame f = new JFrame("Click & drag to zoom.");
  65.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  66.         Zoom zoom = new Zoom();
  67.         f.add(zoom);
  68.         f.pack();
  69.         f.setVisible(true);
  70.         Ticker t = new Ticker(zoom);
  71.         zoom.addMouseMotionListener(zoom);
  72.         t.start();
  73.     }
  74.  
  75.     private static class Ticker extends Thread {
  76.         private final Robot robot;
  77.         public boolean update = true;
  78.         private final Zoom view;
  79.  
  80.         public Ticker(Zoom zoomPanel) {
  81.             view = zoomPanel;
  82.             try {
  83.                 robot = new Robot();
  84.             } catch (AWTException e) {
  85.                 throw new RuntimeException(e);
  86.             }
  87.         }
  88.  
  89.         public void done() {
  90.             update = false;
  91.         }
  92.  
  93.         public void run() {
  94.             while (update) {
  95.                 final Point p = MouseInfo.getPointerInfo().getLocation();
  96.                 Rectangle rect = new Rectangle(p.x - S2, p.y - S2, SIZE, SIZE);
  97.                 view.img = robot.createScreenCapture(rect);
  98.                 view.repaint();
  99.             }
  100.         }
  101.     }
  102.  
  103.     public static void main(String[] args) {
  104.         EventQueue.invokeLater(new Runnable() {
  105.             @Override
  106.             public void run() {
  107.                 create();
  108.             }
  109.         });
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement