只要打好在屏幕上的透明窗口,繪製到它上面。透明的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方法;這是肯定的。 –