只要打好在屏幕上的透明窗口,绘制到它上面。透明的Windows甚至支持点击,因此效果就像直接在屏幕上绘画一样。
使用Java 7:
Window w=new Window(null)
{
@Override
public void paint(Graphics g)
{
final Font font = getFont().deriveFont(48f);
g.setFont(font);
g.setColor(Color.RED);
final String message = "Hello";
FontMetrics metrics = g.getFontMetrics();
g.drawString(message,
(getWidth()-metrics.stringWidth(message))/2,
(getHeight()-metrics.getHeight())/2);
}
@Override
public void update(Graphics g)
{
paint(g);
}
};
w.setAlwaysOnTop(true);
w.setBounds(w.getGraphicsConfiguration().getBounds());
w.setBackground(new Color(0, true));
w.setVisible(true);
如果每个像素的透明度不支持或不提供系统上的点击行为,你可以通过设置窗口尝试每个像素的透明度Shape
代替:
Window w=new Window(null)
{
Shape shape;
@Override
public void paint(Graphics g)
{
Graphics2D g2d = ((Graphics2D)g);
if(shape==null)
{
Font f=getFont().deriveFont(48f);
FontMetrics metrics = g.getFontMetrics(f);
final String message = "Hello";
shape=f.createGlyphVector(g2d.getFontRenderContext(), message)
.getOutline(
(getWidth()-metrics.stringWidth(message))/2,
(getHeight()-metrics.getHeight())/2);
// Java6: com.sun.awt.AWTUtilities.setWindowShape(this, shape);
setShape(shape);
}
g.setColor(Color.RED);
g2d.fill(shape.getBounds());
}
@Override
public void update(Graphics g)
{
paint(g);
}
};
w.setAlwaysOnTop(true);
w.setBounds(w.getGraphicsConfiguration().getBounds());
w.setVisible(true);
行不通的。也许与平台相关的JNI。但没有纯粹的Java方法;这是肯定的。 –