本文介绍了在Java中调整大小时,绘制的内容不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 请注意,我没有在Mac机器上的Windows机器上测试过。我不确定这是否也发生在Windows机器上... 当我调整我的Java应用程序时,内容是不可见的。我已经找到了在调整大小之后修正它的方法,但是当用户调整窗口大小时不能修改。 我没有使用Swing或其他东西,因为它让我的二进制文件变得如此缓慢(在我看来)。 结构如下所示: > am 能够跟踪'live'-resize事件: public void componentMoved(ComponentEvent e){ System.out.println(Live-resize); } 当我开始调整大小时, 调整大小时,会在我的日志中生成实时调整大小的每一个像素,然后调整窗口大小。 当我停止调整此方法的大小时没有被调用,componentResized方法。 当我将重绘方法(或官方重绘方法)添加到这样的'live'调整大小事件,我仍然得到输出,但是,它不是重新绘制或什么东西 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(); } 当我将应用程序最小化并重新启用应用程序时,我猜想需要相同的代码才能解决这个问题。 我没有使用 Graphics2D 或只是图形。 请问我可以如何重新绘制视图? 在此先感谢, Tim 解决方案好的,我终于修好了。 代替每次在 paint(Graphics g) - 方法中重绘它,您需要缓冲输出,并只重绘该图像(我有点希望Java已经这样做,就像Obj-C一样)。 public BufferedImage缓冲; public void redraw(){ buffer = new BufferedImage( 200,// height 300,// width BufferedImage.TYPE_4BYTE_ABGR); // ABGR = RGBA,每个像素4字节(r,g,b,a) Graphics g = buffer.getGraphics(); //在这里做绘图 if(this.getGraphics()){ //'this'已经显示,所以需要重绘 this.paint(this .getGraphics()); //小黑客} } public void update(Graphics g){ this.paint(g); } public void paint(Graphics g){ g.drawImage(buffer,0,0,this); } 现在,当您最小化窗口并再次最大化时,绘画依然存在。只是,窗户现在闪烁了0.1秒左右,但我并不在意这一点。 Please note I haven't tested this on a Windows-machine only on a Mac-machine. I'm not so sure whether this also occurs on a Windows-machine...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.I'm not using Swing or something because it makes my binary so slow (in my opinion).The structure is like this:Frame My main-windowContainer Content view of main-windowContainer-based subviews that including the paint(Graphics g)-methodI'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());}I am beware of the fact using the paint(getGraphics())-method isn't a really good way to do this, but since the repaint()-method doesn't do anything at all, it's the only working possibility.While resizing, all painted content becomes invisible. However, when I add a Button-instance to my Content-view and resize my Main-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");}When I start resizing this method is not being called.While resizing it generates "Live-resize" in my log every single pixel I resize the window.When I stop resizing this method is not being called, the componentResized-method does.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 somethingpublic void componentMoved(ComponentEvent e) { System.out.println("Live-resize"); this.contentView.paint(this.contentView.getGraphics());}Orpublic void componentMoved(ComponentEvent e) { System.out.println("Live-resize"); this.contentView.repaint();}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.I'm not using Graphics2D or something, just Graphics.Could you please explain me how I can repaint the views?Thanks in advance,Tim 解决方案 Okay, I finally fixed it.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);}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中调整大小时,绘制的内容不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-04 21:29