FileDocCategorySizeDatePackage
DetachedMagnifyingGlass.javaAPI DocExample2300Mon Jan 09 11:02:00 GMT 2006None

DetachedMagnifyingGlass

public class DetachedMagnifyingGlass extends JComponent implements MouseMotionListener

Fields Summary
double
zoom
JComponent
comp
Point
point
Dimension
mySize
Robot
robot
Constructors Summary
public DetachedMagnifyingGlass(JComponent comp, Dimension size, double zoom)

        this.comp = comp;
        // flag to say don't draw until we get a MouseMotionEvent
        point = new Point (-1, -1); 
        comp.addMouseMotionListener(this);
        this.mySize = size;
        this.zoom = zoom;
        // if we can't get a robot, then we just never
        // paint anything
        try {
            robot = new Robot();
        } catch (AWTException awte) {
            System.err.println ("Can't get a Robot");
            awte.printStackTrace();
        } 
    
Methods Summary
private java.awt.RectanglecomputeGrabRect()

        // width, height are size of this comp / zoom
        int grabWidth = (int) ((double) mySize.width / zoom);
        int grabHeight = (int) ((double) mySize.height / zoom);
        // upper left corner is current point
        return new Rectangle (point.x, point.y, grabWidth, grabHeight);
    
public java.awt.DimensiongetMaximumSize()

 return mySize; 
public java.awt.DimensiongetMinimumSize()

 return mySize; 
public java.awt.DimensiongetPreferredSize()

 return mySize; 
public voidmouseDragged(java.awt.event.MouseEvent e)

        mouseMoved (e);
    
public voidmouseMoved(java.awt.event.MouseEvent e)

        Point offsetPoint = comp.getLocationOnScreen();
        e.translatePoint (offsetPoint.x, offsetPoint.y);
        point = e.getPoint();
        repaint();
    
public voidpaint(java.awt.Graphics g)

        if ((robot == null) || (point.x == -1))
        { 
            g.setColor (Color.blue);
            g.fillRect (0, 0, mySize.width, mySize.height);
            return;
        }
        Rectangle grabRect = computeGrabRect();
        BufferedImage grabImg = robot.createScreenCapture (grabRect);
        Image scaleImg = 
            grabImg.getScaledInstance (mySize.width, mySize.height, Image.SCALE_FAST);
        g.drawImage (scaleImg, 0, 0, null);