Advertisement
Guest User

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

a guest
Sep 18th, 2010
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.52 KB | None | 0 0
  1. import java.awt.AWTException;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.EventQueue;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.MouseInfo;
  8. import java.awt.Point;
  9. import java.awt.Robot;
  10. import java.awt.event.MouseEvent;
  11. import java.awt.event.MouseMotionListener;
  12. import java.awt.image.BufferedImage;
  13. import java.util.Date;
  14.  
  15. import javax.swing.JFrame;
  16. import javax.swing.JPanel;
  17.  
  18. public class Zoom extends JPanel implements MouseMotionListener {
  19.  
  20.     private static final int SIZE = 8;
  21.     private static final int S2 = SIZE / 2;
  22.     private static final int SCALE = 48;
  23.     private BufferedImage img;
  24.     private Robot robot;
  25.     private Ticker t;
  26.  
  27.     private class Ticker extends Thread {
  28.         private long timeRef = new Date().getTime();
  29.         public boolean update = true;
  30.         public JPanel jp = null;
  31.         private int iter = 0;
  32.  
  33.         public Ticker(JPanel g) {
  34.             super();
  35.             jp = g;
  36.         }
  37.  
  38.         public void done() {
  39.             update = false;
  40.         }
  41.  
  42.         public void run() {
  43.             try {
  44.                 while (update == true) {
  45.                     BufferedImage img2 = new BufferedImage(SIZE, SIZE,
  46.                             BufferedImage.TYPE_INT_RGB);
  47.                     updateImg(img2);
  48.                     synchronized (img) {
  49.                         img = img2;
  50.                     }
  51.                     paintComponent(jp.getGraphics());
  52.                     StringBuilder sb = new StringBuilder();
  53.                     sb.append(++iter)
  54.                             .append(" frames in ")
  55.                             .append((double) (new Date().getTime() - this.timeRef) / 1000)
  56.                             .append("s.");
  57.                     System.out.println(sb.toString());
  58.                 }
  59.             } catch (Exception e) {
  60.                 e.printStackTrace();
  61.                 update = false;
  62.             }
  63.         }
  64.     }
  65.  
  66.     private void updateImg(BufferedImage image) {
  67.         Graphics2D gc = null;
  68.         try {
  69.             Point p = MouseInfo.getPointerInfo().getLocation();
  70.             gc = image.createGraphics();
  71.             for (int x = 0; x < 8; x++) {
  72.                 for (int y = 0; y < 8; y++) {
  73.                     gc.setColor(robot.getPixelColor(p.x - 4 + x, p.y - 4 + y));
  74.                     gc.drawLine(x, y, x, y);
  75.                 }
  76.             }
  77.         } catch (Exception e) {
  78.             e.printStackTrace();
  79.         } finally {
  80.             gc.dispose();
  81.         }
  82.     }
  83.  
  84.     public Zoom() {
  85.         super(true);
  86.  
  87.         this.setPreferredSize(new Dimension(SIZE * SCALE, SIZE * SCALE));
  88.  
  89.         img = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_RGB);
  90.  
  91.         t = new Ticker(this);
  92.         try {
  93.             robot = new Robot();
  94.         } catch (AWTException e) {
  95.             e.printStackTrace(System.err);
  96.         }
  97.  
  98.     }
  99.  
  100.     @Override
  101.     protected void paintComponent(Graphics g) {
  102.         try {
  103.             if (img != null)
  104.                 synchronized (img) {
  105.                     g.drawImage(img, 0, 0, getWidth(), getHeight(), null);
  106.                 }
  107.         } catch (Exception e) {
  108.             e.printStackTrace();
  109.         }
  110.  
  111.     }
  112.  
  113.     @Override
  114.     public void mouseMoved(MouseEvent e) {
  115.         Point p = e.getPoint();
  116.         int x = p.x * SIZE / getWidth();
  117.         int y = p.y * SIZE / getHeight();
  118.         int c = img.getRGB(x, y);
  119.         this.setToolTipText(x + "," + y + ": " + String.format("%08X", c));
  120.     }
  121.  
  122.     @Override
  123.     public void mouseDragged(MouseEvent e) {
  124.         for (int r = 0; r < SIZE; r++) {
  125.             for (int c = 0; c < SIZE; c++) {
  126.                 Color color = robot.getPixelColor(e.getXOnScreen() + c - S2,
  127.                         e.getYOnScreen() + r - S2);
  128.                 img.setRGB(c, r, color.getRGB());
  129.             }
  130.         }
  131.         repaint();
  132.     }
  133.  
  134.     private static void create() {
  135.         JFrame f = new JFrame("Click & drag to zoom.");
  136.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  137.         Zoom zoom = new Zoom();
  138.         f.add(zoom);
  139.         f.pack();
  140.         f.setVisible(true);
  141.         zoom.addMouseMotionListener(zoom);
  142.         zoom.t.run();
  143.     }
  144.  
  145.     public static void main(String[] args) {
  146.         EventQueue.invokeLater(new Runnable() {
  147.  
  148.             @Override
  149.             public void run() {
  150.                 create();
  151.             }
  152.         });
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement