[英]Flickering While Resizing Transparent JWindow
I'm trying to make a transparent window with Java Swing, and I have created the window.我正在尝试用 Java Swing 制作透明的 window,并且我已经创建了 Z05B8C35CBD96FBFFFDE4C074。 Problem is when I resize the window, it flickers.
问题是当我调整 window 的大小时,它会闪烁。 I tried changing the background to an opaque color instead.
我尝试将背景更改为不透明的颜色。 That fixed the problem, But I want the window to be transparent.
这解决了问题,但我希望 window 是透明的。 I have also tried
我也试过
Toolkit.getDefaultToolkit().setDynamicLayout(true);
, ,
Toolkit.getDefaultToolkit().getDesktopProperty("awt.dynamicLayoutSupported");
, ,
System.setProperty("sun.awt.noerasebackground", "true");
, ,
But with no avail.但无济于事。 I've tried
JWindow.setBounds
instead of JWindow.setSize
, but that also had no effect.我试过
JWindow.setBounds
而不是JWindow.setSize
,但这也没有效果。
Here is the code I use to produce the window这是我用来生产 window 的代码
import java.awt.Color;
import java.awt.Graphics;
import java.awt.MouseInfo;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
import javax.swing.JWindow;
public class GlassWindow extends JPanel {
// RIGHT CLICK TO CLOSE
private static final long serialVersionUID = 1L;
private JWindow jwindow = new JWindow();
private boolean programCalledRender, shouldClose;
private int edges = 8;
private Color background = new Color(255, 100, 0, 100);
public GlassWindow() {
int width = 1000, height = 750;
this.setOpaque(false);
this.setSize(width, height);
jwindow.setSize(width, height);
jwindow.setBackground(new Color(0, 0, 0, 0));
jwindow.setContentPane(this);
jwindow.setLocationRelativeTo(null);
jwindow.setVisible(true);
jwindow.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON3) System.exit(0);
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
});
new Thread(() -> run()).start();
}
public void render() {
programCalledRender = true;
this.repaint();
}
private void run() {
int setTPS = 20, setFPS = 60;
long lastTime = System.nanoTime();
double delta = 0, frameDelta = 0;
while(true) {
if(shouldClose) break;
long now = System.nanoTime();
delta += (now-lastTime)/(1000000000/(double)setTPS);
frameDelta += (now-lastTime)/(1000000000/(double)setFPS);
lastTime = now;
while(delta > 0) {
tick();
delta--;
}
while(frameDelta > 0) {
render();
frameDelta--;
}
}
}
private void tick() {
}
@Override
public void paintComponent(Graphics g) {
if(!programCalledRender) return;
super.paintComponent(g);
int newWidth = MouseInfo.getPointerInfo().getLocation().x+20-jwindow.getX();
if(newWidth != jwindow.getWidth()) {
jwindow.setSize(newWidth, jwindow.getHeight());
}
if(background != null) {
g.setColor(background);
g.fillRect(edges, edges, jwindow.getWidth()-edges*2, jwindow.getHeight()-edges*2);
}
g.setColor(new Color(0, 0, 0, 100));
for(int i = 0; i <= edges; i++) {
g.drawRect(i, i, jwindow.getWidth()-i*2, jwindow.getHeight()-i*2);
}
}
public static void main(String[] args) {
new GlassWindow();
}
}
How can I prevent flickering when the JWindow is being resized?调整 JWindow 大小时如何防止闪烁?
Any help is appreciated.任何帮助表示赞赏。
As suggested by @camickr I should not resize the window in the render
method.正如@camickr 所建议的,我不应该在
render
方法中调整 window 的大小。
So to stop the flickering I remove所以为了停止闪烁,我删除了
int newWidth = MouseInfo.getPointerInfo().getLocation().x+20-jwindow.getX();
if(newWidth != jwindow.getWidth()) {
jwindow.setSize(newWidth, jwindow.getHeight());
}
from the paintComponent(Graphics g)
method.来自
paintComponent(Graphics g)
方法。 And add并添加
jwindow.addMouseMotionListener(new MouseMotionListener() {
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {
int newWidth = MouseInfo.getPointerInfo().getLocation().x+20-jwindow.getX();
if(newWidth != jwindow.getWidth()) {
jwindow.setSize(newWidth, jwindow.getHeight());
}
}
});
to the constructor GlassWindow()
.到构造函数
GlassWindow()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.