用Java在屏幕上绘图 [英] Drawing over screen in Java
问题描述
我想用Java创建一个助手应用程序,其行为类似于:每当通过全局快捷方式调用时,它都可以在屏幕上绘制一些文本(不是在其自己的应用程序窗口中,而是在屏幕顶部)。 / p>
类似的帖子是此处,但我想用Java来实现。
当我搜索 java屏幕上绘制之类的东西时,我只能得到很多有关Java2D的教程。
p>我要检查:1)是否可以使用Java绘制其他应用程序? 2)如果不可能,在Mac / Ubuntu中还有其他选择吗?
非常感谢。
(旁注:我知道Java不支持全局快捷方式。我正在尝试其他方法来解决该问题,此处不相关)
只需在屏幕上放置一个透明窗口并在其上绘制即可。透明Windows甚至支持点击功能,因此效果就像直接在屏幕上绘制一样。
使用Java 7:
w = new Window(null)的窗口
{
@Override
public void paint(Graphics g)
{
final字体= getFont()。deriveFont(48f);
g.setFont(font);
g.setColor(Color.RED);
final String message = Hello;
FontMetrics指标= 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
公共无效涂料(Graphics g)
{
Graphics2D g2d =((Graphics2D)g);
if(shape == null)
{
字体f = getFont()。deriveFont(48f);
FontMetrics指标= 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);
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).
A similar post is here, but I want to achieve this in Java.
When I search something like "java draw over screen", I can only get lots of tutorials about 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?
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)
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.
Using 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:
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);
这篇关于用Java在屏幕上绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!