如何调整 Swing JWindow 的大小而不会闪烁?
我正在尝试基于 制作一个自定义 UI,以便选择要共享的屏幕区域。我已经扩展并添加了代码,使其可调整大小,并使用“剪切”窗口的中心。JWindow
JWindow
AWTUtilities.setWindowShape()
运行代码时,当窗口在负x和y方向(即向上和向左)调整大小时,我遇到了闪烁。似乎正在发生的情况是,在更新组件之前,窗口已调整大小并绘制。下面是代码的简化版本。运行时,顶部面板可用于向上和向左调整窗口大小。窗口的背景设置为绿色,以明确我不想显示的像素的位置。
编辑:改进了代码以使用a正确塑造窗口,并在底部添加了一个虚拟组件以进一步说明闪烁(也更新了屏幕截图)。ComponentListener
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Area;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;
import com.sun.awt.AWTUtilities;
public class FlickerWindow extends JWindow implements MouseListener, MouseMotionListener{
JPanel controlPanel;
JPanel outlinePanel;
int mouseX, mouseY;
Rectangle windowRect;
Rectangle cutoutRect;
Area windowArea;
public static void main(String[] args) {
FlickerWindow fw = new FlickerWindow();
}
public FlickerWindow() {
super();
setLayout(new BorderLayout());
setBounds(500, 500, 200, 200);
setBackground(Color.GREEN);
controlPanel = new JPanel();
controlPanel.setBackground(Color.GRAY);
controlPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
controlPanel.addMouseListener(this);
controlPanel.addMouseMotionListener(this);
outlinePanel = new JPanel();
outlinePanel.setBackground(Color.BLUE);
outlinePanel.setBorder(new CompoundBorder(new EmptyBorder(2,2,2,2), new LineBorder(Color.RED, 1)));
add(outlinePanel, BorderLayout.CENTER);
add(controlPanel, BorderLayout.NORTH);
add(new JButton("Dummy button"), BorderLayout.SOUTH);
setVisible(true);
setShape();
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
setShape();
}});
}
public void paint(Graphics g) {
// un-comment or breakpoint here to see window updates more clearly
//try {Thread.sleep(10);} catch (Exception e) {}
super.paint(g);
}
public void setShape() {
Rectangle bounds = getBounds();
Rectangle outlineBounds = outlinePanel.getBounds();
Area newShape = new Area (new Rectangle(0, 0, bounds.width, bounds.height));
newShape.subtract(new Area(new Rectangle(3, outlineBounds.y + 3, outlineBounds.width - 6, outlineBounds.height - 6)));
setSize(bounds.width, bounds.height);
AWTUtilities.setWindowShape(this, newShape);
}
public void mouseDragged(MouseEvent e) {
int dx = e.getXOnScreen() - mouseX;
int dy = e.getYOnScreen() - mouseY;
Rectangle newBounds = getBounds();
newBounds.translate(dx, dy);
newBounds.width -= dx;
newBounds.height -= dy;
mouseX = e.getXOnScreen();
mouseY = e.getYOnScreen();
setBounds(newBounds);
}
public void mousePressed(MouseEvent e) {
mouseX = e.getXOnScreen();
mouseY = e.getYOnScreen();
}
public void mouseMoved(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
重写的方法可以用作断点,也可以在此处取消注释,以便在更新发生时提供更清晰的更新视图。paint()
Thread.sleep()
我的问题似乎源于导致窗口在布局之前被涂到屏幕上的方法。setBounds()
调整大小之前的窗口,如下所示:
调整较大大小(向上和向左)期间的窗口,如被覆盖方法的断点处所示):paint()
调整较小(向下和向右)大小期间的窗口,如被覆盖方法的断点处所示):paint()
当然,这些屏幕截图是在激进的鼠标拖动移动期间拍摄的,但即使对于更适度的鼠标拖动,闪烁也会变得非常烦人。
将大小调整为较大屏幕截图上的绿色区域显示了在完成任何绘制/布局之前绘制的新背景,这似乎发生在基础或本机窗口管理器中。“调整大小为较小”屏幕截图上的蓝色区域显示 的背景被推入视图,但现在已过期。这发生在Linux(Ubuntu)和Windows XP下。ComponentPeer
JPanel
有没有人找到一种方法,在对屏幕进行任何更改之前,导致或调整为后台缓冲区的大小,从而避免这种闪烁效果?也许可以设置一个系统属性来避免这种情况,但我找不到一个。Window
JWindow
java.awt....
编辑#2:注释掉对 的调用(并可选择取消注释 中的行),然后积极地拖动顶部面板,以便清楚地看到闪烁的性质。AWTUtilities.setWindowShape()
Thread.sleep(10)
paint()
编辑#3:有没有人能够在Windows 7或Mac OSX上的Sun Java下测试这种行为?