Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you don't mind using Swing, this <a href="https://stackoverflow.com/questions/2901472">example</a> shows how to quickly zoom in on a <code>BufferedImage</code> obtained from an <code>Icon</code>. In your case, you'd want an 8x8 <code>BufferedImage</code> that gets filled in <code>mouseMoved()</code> with the pixels seen by the robot.</p> <p>Addendum: Here's a snapshot of the top, left corner of your example.</p> <p>Addendum:</p> <blockquote> <p>Zooming itself is not important...</p> </blockquote> <p>The slow part is getting pixels from the desktop; scaling is minor. If you just want to see a variety of animation techniques, have a look at this <a href="http://sites.google.com/site/drjohnbmatthews/kineticmodel" rel="nofollow noreferrer">example</a>.</p> <p>Addendum: As getting individual pixels is slow and the <code>createScreenCapture()</code> method suggested by @Steve McLeod is fast, here's the idea I was driving at. You can see it also updates much more smoothly. Note that releasing the mouse button allows one to see the captured colors.</p> <p><img src="https://i.stack.imgur.com/yH2NS.png" alt="Zoom.png"></p> <pre><code>import java.awt.AWTException; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Point; import java.awt.Rectangle; import java.awt.Robot; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import java.awt.image.BufferedImage; import javax.swing.JFrame; import javax.swing.JPanel; /** @see https://stackoverflow.com/questions/3742731 */ public class Zoom extends JPanel implements MouseMotionListener { private static final int SIZE = 16; private static final int S2 = SIZE / 2; private static final int SCALE = 48; private BufferedImage img; private Robot robot; public Zoom() { super(true); this.setPreferredSize(new Dimension(SIZE * SCALE, SIZE * SCALE)); img = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_RGB); try { robot = new Robot(); } catch (AWTException e) { e.printStackTrace(System.err); } } @Override protected void paintComponent(Graphics g) { g.drawImage(img, 0, 0, getWidth(), getHeight(), null); } @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) { int x = e.getXOnScreen(); int y = e.getYOnScreen(); Rectangle rect = new Rectangle(x - S2, y - S2, SIZE, SIZE); img = robot.createScreenCapture(rect); repaint(); } private static void create() { JFrame f = new JFrame("Click &amp; drag to zoom."); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Zoom zoom = new Zoom(); f.add(zoom); f.pack(); f.setVisible(true); zoom.addMouseMotionListener(zoom); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { create(); } }); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload