1

I cant find any answer to this. I am trying to use active painting in loop in Java. I create a new BufferedImage and in my paint method get its graphics, draw to its graphics normal Image and also various shapes like fillRect() etc. Then I draw the BufferedImage to JPanel (with variable name canvas) graphics.

Graphics gr = buffer.createGraphics();
gr.drawImage(img, 0, 0, 500, 500, null);

for (int i = 0; i<200; i++){        
gr.drawOval(i*10,i*20,50,50);
etc.
}

gr.dispose();
canvas.getGraphics().drawImage(buffer, 0, 0, 500, 500, null);

Why do I see in the JPanel all the shapes drawn but without Image , which comes only with delay I thought that first everything is drawn to BufferedImage and that is subsequently drawn on another graphics at once (isnt that whats buffers about?). Can someone please explain this to me? What thread is supposed to draw stuff on graphics object? In which is this Image drawing running (when using active rendering, not calling paintComponent())

4

1 回答 1

0

什么线程应该在图形对象上绘制东西?

你想要的任何线程。不久前,我编写了一个 GIS 应用程序,我必须在其中执行此类操作(绘制图像和形状并显示它们)。这是我如何进行的:

  1. 有一个背景线程将图像和所有形状绘制到BufferedImage
  2. JPanel.paintComponent()后台线程获取结果并将其绘制到 JPanel 的图形中

运行这段代码

canvas.getGraphics().drawImage(buffer, 0, 0, 500, 500, null);

外面paintComponent真的是一个坏主意,可能会解释你遇到的问题。这是因为您从中获得的图形canvas.getGraphics()并非用于渲染,而仅用于打印等操作

您绝对应该使用可以解决很多问题BufferedImage的方法JPanel.paintComponent()

于 2012-06-28T20:20:43.790 回答