我想在 JFrame 的背景上做一个模糊处理,它是透明的以显示它下面发生的事情,但我不知道如何模糊背景并避免闪烁。我想要实现的是有一个稍微模糊的透明背景,但仍然显示它下面的窗户的“实时视图”,而不是有一个不会改变的模糊静态图片。请记住,窗口可能会占用全屏大小。
我希望我描述的正确,因为我仍然是 Java 图形的初学者。
代码中的透明框:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class BlurredBackgroundWindow {
public static void main(String[] args) {
new BlurredBackgroundWindow().drawGUI();
}
public void drawGUI() {
myJFrame frm = new myJFrame();
frm.setTitle("BlurredBackgroundWindow");
frm.setSize(480, 360);
frm.setUndecorated(true);
frm.setBackground(new Color(0,0,0,1));
frm.setLocationRelativeTo(null);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setVisible(true);
}
class myJFrame extends JFrame {
public void paint(Graphics g) {
super.paint(g);
// Doing the rest of painting here
}
}
}
注意 - 框架是完全透明的,并且看到一些效果将颜色更改为例如。
frm.setBackground(new Color(0,100,0,100));
谢谢你的帮助
编辑1:这是我想避免的闪烁效果,但我真的不知道如何......这个仅适用于全屏窗口。
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
public class BlurredBackgroundWindow {
public static int FPS = 2;
private BufferedImage temp = null;
private BufferedImage out = null;
private BufferedImage image = null;
myJFrame frm = new myJFrame();
public static void main(String[] args) {
new BlurredBackgroundWindow().drawGUI();
}
public void drawGUI() {
frm.setTitle("BlurredBackgroundWindow");
frm.setExtendedState(JFrame.MAXIMIZED_BOTH);
frm.setUndecorated(true);
frm.setBackground(new Color(0,0,0,1));
frm.setLocationRelativeTo(null);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setVisible(true);
Timer bcg = new Timer();
bcg.schedule(new TimerTask() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot;
public void run() {
System.out.println("Repaint");
frm.repaint();
try {
robot = new Robot();
image = robot.createScreenCapture(screenRectangle);
} catch (AWTException e) {}
frm.repaint();
// Save the blurred image for debugging
/*try {
ImageIO.write(image, "png", new File("C:\\test.png"));
} catch (IOException e) {
e.printStackTrace();
}*/
}
}, 0, (int) (1000f/FPS));
}
class myJFrame extends JFrame {
public void paint(Graphics g) {
super.paint(g);
if(image != null) {
float[] matrix = {
0.111f, 0.111f, 0.111f,
0.111f, 0.111f, 0.111f,
0.111f, 0.111f, 0.111f,
};
BufferedImageOp op = new ConvolveOp( new Kernel(3, 3, matrix) );
temp = op.filter(image, out);
out = temp;
g.drawImage(out,0,0,null);
temp=null;out=null;image=null;
}
}
}
}