问题描述
我有以下代码片段,应该在第一台显示器的透明背景上增加一个计数器.当图像显示为 0 时,它渲染得很好,但在那之后(只要点击 1),窗口就会以不透明的背景重新绘制.
I have the following snippet of code which should increment a counter on a transparent background on the first monitor. When the image shows 0 it renders fine, but after that (as soon as 1 is hit) the window redraws with an opaque background.
我知道实践中的愚蠢示例,只是将实际用例分解为一段更简单的代码.
Silly example in practice I know, just broken a real use case down to a simpler piece of code.
看来关键可能在TestCanvas的paintComponent方法中:
It seems the key might be in the paintComponent method of TestCanvas:
g.setColor(new Color(0, 0, 0, 0));
g.clearRect(0, 0, getWidth(), getHeight());
据我所知,这两条线应该将绘图颜色设置为完全透明,然后用该颜色清除给定区域 - 但这似乎在第一次重绘之后就没有了.
From what I can work out, those two lines should set the drawing colour to completely transparent, then clear the given area with that colour - but this doesn't seem to be holding for beyond the first repaint.
使用 fillRect 而不是 clearRect 不起作用,因为它只是在现有图像的顶部绘制透明矩形,因此它永远不会被清除.1 叠加在 0 上,然后 2 叠加在 1 上,依此类推
Using fillRect instead of clearRect doesn't work because it just paints the transparent rectangle on top of the existing image, so it never gets cleared. 1 is overlayed on 0, then 2 overlayed on 1, etc.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
public class LyricWindow extends JWindow {
private final TestCanvas canvas;
public LyricWindow(Rectangle area, boolean stageView) {
setBackground(new Color(0, 0, 0, 0));
setArea(area);
canvas = new TestCanvas();
canvas.setPreferredSize(new Dimension((int) (area.getMaxX() - area.getMinX()), (int) (area.getMaxY() - area.getMinY())));
add(canvas);
new Thread() {
public void run() {
for(int i = 0; true; i++) {
final int ii = i;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
canvas.setText(Integer.toString(ii));
}
});
try {
Thread.currentThread().sleep(200);
}
catch(InterruptedException ex) {}
System.out.println(ii);
}
}
}.start();
}
public final void setArea(final Rectangle area) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
if(canvas != null) {
canvas.setPreferredSize(new Dimension((int) (area.getMaxX() - area.getMinX()), (int) (area.getMaxY() - area.getMinY())));
}
setSize((int) (area.getMaxX() - area.getMinX()), (int) (area.getMaxY() - area.getMinY()));
setLocation((int) area.getMinX(), (int) area.getMinY());
}
});
}
public static void main(String[] args) {
LyricWindow w = new LyricWindow(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getConfigurations()[0].getBounds(), false);
w.setVisible(true);
}
}
class TestCanvas extends JPanel {
private String text;
@Override
public void paintComponent(Graphics g) {
g.setColor(new Color(0, 0, 0, 0));
g.clearRect(0, 0, getWidth(), getHeight());
g.setColor(Color.RED);
g.drawString(text, 100, 100);
}
public void setText(String s) {
text = s;
repaint();
}
}
推荐答案
原来我必须在绘画前设置正确的复合值.添加
Turned out I had to set the correct composite value before painting. Adding
((Graphics2D)g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC));
到 paintComponent()
方法的开头,然后使用 fillRect()
,成功了!
to the start of the paintComponent()
method, then using fillRect()
, did the trick!
这篇关于Graphics2D 不清除透明图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!