用Java在屏幕上绘图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21604762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Drawing over screen in Java
提问by songyy
I want to create an helper application in Java.. which behaves like: whenever called through a global shortcut, it can draw some text on the screen (not onto its own application window, but on top of the screen).
我想在 Java 中创建一个辅助应用程序 .. 其行为类似于:每当通过全局快捷方式调用时,它都可以在屏幕上绘制一些文本(不是在它自己的应用程序窗口上,而是在屏幕顶部)。
A similar post is here, but I want to achieve this in Java.
类似的帖子在这里,但我想用 Java 实现这一点。
When I search something like "java draw over screen", I can only get lots of tutorials about Java2D.
当我搜索诸如“java draw over screen”之类的内容时,我只能得到很多关于 Java2D 的教程。
I want to check: 1) is it possible to draw over other applications in Java? 2) If not possible, is there any alternatives in Mac / Ubuntu?
我想检查:1)是否可以在 Java 中绘制其他应用程序?2) 如果不可能,Mac / Ubuntu 中是否有其他选择?
Thanks a lot.
非常感谢。
(Side note: I know java don't have the global shortcut support. I'm trying other methods to solve that problem, non-related here)
(旁注:我知道 java 没有全局快捷方式支持。我正在尝试其他方法来解决这个问题,这里不相关)
回答by Holger
Simply lay a transparent Window over the screen and draw onto it. Transparent Windows even support click-through so the effect is like if you were drawing over the screen directly.
只需在屏幕上放置一个透明的 Window 并在其上绘图。透明窗口甚至支持点击,因此效果就像您直接在屏幕上绘制一样。
Using Java?7:
使用 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);
If per-pixel translucency is not supported or does not provide the click-through behavior on your system, you can try per-pixel transparency by setting a Window Shape
instead:
如果您的系统不支持每像素半透明或不提供点击行为,您可以通过设置 WindowShape
来尝试每像素透明度:
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);