在 Java 中调整大小时绘制的内容不可见 [英] Painted content invisible while resizing in Java
问题描述
请注意,我没有在 Windows 机器上仅在 Mac 机器上测试过这个.我不太确定这是否也发生在 Windows 机器上...
当我调整 Java 应用程序的大小时,内容是不可见的.我已经找到了一种方法来修复它在调整窗口大小之后,但不是同时用户调整窗口大小.
When I resize my Java-application the content is invisible. I already found a way to fix it after resizing it, but not while the user is resizing the window.
我没有使用 Swing 之类的东西,因为它使我的二进制文件变得很慢(在我看来).
I'm not using Swing or something because it makes my binary so slow (in my opinion).
结构是这样的:
Frame
我的主窗口Containe
rmain-window
的内容视图
Container
-based subviews,包括paint(Graphics g)
-method
Container
-based subviews that including thepaint(Graphics g)
-method
我已将所有侦听器添加到
My main-window
,现在我可以重绘 Content-view 调整窗口大小后.I've added all listeners to
My main-window
and now I'm able to redraw the Content-view after resizing the window.public void componentResized(ComponentEvent e) { this.contentView.paint(this.contentView.getGraphics()); }
我意识到使用
paint(getGraphics())
方法并不是一个很好的方法,但是由于repaint()
-method 什么都不做,它是唯一可行的方法.I am beware of the fact using the
paint(getGraphics())
-method isn't a really good way to do this, but since therepaint()
-method doesn't do anything at all, it's the only working possibility.在调整大小时,所有绘制的内容都变得不可见.但是,当我将
Button
-instance 添加到我的Content-view
并调整我的Main-window
,按钮不会隐藏.While resizing, all painted content becomes invisible. However, when I add a
Button
-instance to myContent-view
and resize myMain-window
, the button doesn't get invisible.我我能够跟踪实时"调整大小事件:
I am able to trace the 'live'-resize event:
public void componentMoved(ComponentEvent e) { System.out.println("Live-resize"); }
- 当我开始调整大小时,不会调用此方法.
- 在调整窗口大小时,我会在我的日志中生成实时调整大小",我调整窗口大小的每个像素.
- 当我停止调整大小时,此方法未被调用,componentResized-method 会调用.
当我将我的重绘方法(或官方重绘方法)添加到像这样的 'live'-resize 事件时,我仍然得到输出,但是,它不是重绘或其他东西
When I add my repaint-method (or the official repaint-method) to the 'live'-resize event like this, I still get the output, however, it's not repainting or something
public void componentMoved(ComponentEvent e) { System.out.println("Live-resize"); this.contentView.paint(this.contentView.getGraphics()); }
或
public void componentMoved(ComponentEvent e) { System.out.println("Live-resize"); this.contentView.repaint(); }
当我将应用程序最小化到 Dock 并再次最大化应用程序时,会发生同样的事情,我猜需要相同的代码来解决这个问题.
When I minimize my application to the dock and maximize the application again, the same thing happens, I guess that the same code is needed to fix this.
我没有使用
Graphics2D
或其他东西,只是使用Graphics
.I'm not using
Graphics2D
or something, justGraphics
.你能解释一下我如何重新绘制视图吗?
Could you please explain me how I can repaint the views?
提前致谢,提姆
推荐答案
好的,我终于修好了.
不是每次在
paint(Graphics g)
方法中重绘它,您需要缓冲输出并仅重绘该图像(我有点希望 Java 已经这样做了,就像Obj-C).Instead of redrawing it every time in the
paint(Graphics g)
-method, you need to buffer the output and only redraw that image (I kinda hoped Java would be already doing that, just like Obj-C).public BufferedImage buffer; public void redraw() { buffer = new BufferedImage( 200, // height 300, // width BufferedImage.TYPE_4BYTE_ABGR); // ABGR = RGBA, 4-byte (r, g, b, a) per pixel Graphics g = buffer.getGraphics(); // do your drawing here if (this.getGraphics()) { // 'this' is already shown, so it needs a redraw this.paint(this.getGraphics()); // little hack } } public void update(Graphics g) { this.paint(g); } public void paint(Graphics g) { g.drawImage(buffer, 0, 0, this); }
现在,当您最小化窗口并再次最大化时,画作仍然存在.只是,窗口现在闪烁了 0.1 秒左右,但我真的不在乎.
Now, when you minimize the window and maximize it again, the paintings remain. Only, the window's flickering now for .1-second or so, but I don't really care about that.
这篇关于在 Java 中调整大小时绘制的内容不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!