Java, how to draw constantly changing graphics

后端 未结 4 2183
甜味超标
甜味超标 2020-11-22 15:44

Have not done this before, so obviously I suck at it. Here 64 pixels around current mouse position get drawn little bigger on a form. Problem is, that it\'s \'kind of\' to s

4条回答
  •  情书的邮戳
    2020-11-22 16:18

    If you don't mind using Swing, this example shows how to quickly zoom in on a BufferedImage obtained from an Icon. In your case, you'd want an 8x8 BufferedImage that gets filled in mouseMoved() with the pixels seen by the robot.

    Addendum: Here's a snapshot of the top, left corner of your example.

    Addendum:

    Zooming itself is not important...

    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 example.

    Addendum: As getting individual pixels is slow and the createScreenCapture() 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.

    Zoom.png

    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 & 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();
                }
            });
        }
    }
    

提交回复
热议问题