Ok… I couldn’t find what I did before, so I just whipped this up… I notice that it sucks on the Mac because of the shadows around the window. I haven’t tested it on Linux or Windows but it’s enough to give you the idea anyway. It look like JNI is really the way to do this right though.
Run this and drag the red circle around the screen…
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.SwingUtilities;
/*
* Created on Mar 4, 2004
*
* @author scottpalmer
*
*/
public class ShapeWindow extends Frame implements MouseMotionListener
{
public ShapeWindow() throws AWTException {
setUndecorated(true);
setBounds(rect);
r = new Robot();
behindImg = r.createScreenCapture(rect);
compositeImg = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(64,64);
addMouseMotionListener(this);
}
public static void main(String[] args) throws AWTException {
ShapeWindow sw = new ShapeWindow();
sw.setVisible(true);
}
public void paint(Graphics g) {
Graphics gc = compositeImg.getGraphics();
gc.drawImage(behindImg,0,0,64,64,null);
gc.setColor(Color.RED);
gc.drawArc(0,0,64,64,0,360);
gc.dispose();
g.drawImage(compositeImg,0,0,getWidth(),getHeight(),null);
}
private void computeNewImage( Point p ) {
int dx = -p.x;
int dy = -p.y;
SwingUtilities.convertPointToScreen(p,this);
rect.x = p.x;
rect.y = p.y;
Image img = r.createScreenCapture(rect);
// draw old background into this image.
img.getGraphics().drawImage(behindImg,dx,dy,64,64,null);
behindImg = img;
}
// Mouse listener
public void mouseDragged(MouseEvent e) {
Point p = e.getPoint();
computeNewImage( p );
repaint();
setLocation( p );
}
public void mouseMoved(MouseEvent e) {
}
Rectangle rect = new Rectangle(16,16,64,64);
Robot r;
Image behindImg;
Image compositeImg;
}