我在GUI中创建了一个着名的Mandelbrot分形,并加快创建我的图像我决定使用Swing Workers。在doInBackground()方法中,我正在计算每个像素的颜色,并将所有颜色放入数组中。在done()方法中,我访问数组并使用适当的颜色为每个像素着色。这种方式在EDT中着色时在不同的线程中进行繁重的计算。我只有一个问题 - 即使我知道它们存储在BufferedImage中(我可以将文件保存到硬盘,直接访问BufferedImage,或者 - 在缩放时 - 我正在创建一个深层副本的BufferedImage - 在这种情况下,我可以看到正确的图像)。我对Java很新,但我希望我的应用程序尽可能好。我的代码如下,在这里:http://pastebin.com/M2iw9rEYBufferedImage在SwingWorkers制作后不显示
public abstract class UniversalJPanel extends JPanel{
protected BufferedImage image;
protected Graphics2D g2d;
protected int iterations = 100; // max number of iterations
protected double realMin = -2.0; // default min real
protected double realMax = 2.0;// default max real
protected double imaginaryMin = -1.6; // default min imaginary
protected double imaginaryMax = 1.6;// default max imaginary
protected int panelHeight;
protected int panelWidth;
protected Point pressed, released; // points pressed and released - used to calculate drawn rectangle
protected boolean dragged; // if is dragged - draw rectangle
protected int recWidth, recHeight,xStart, yStart; // variables to calculate rectangle
protected FractalWorker[] arrayOfWorkers; // array of Swing workers
public abstract int calculateIterations(Complex c);
public abstract double getDistance(Complex a, Complex b);
public void paintComponent(Graphics g){
super.paintComponent(g);
panelHeight = getHeight();
panelWidth = getWidth();
image =new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); // creating new Bufered image
g2d= (Graphics2D) image.getGraphics();
arrayOfWorkers= new FractalWorker[getHeight()]; // create new worker for each row and execute them
for(int q = 0; q < getHeight(); q++){
arrayOfWorkers[q] = new FractalWorker(q);
arrayOfWorkers[q].execute();
}
g.drawImage(image, 0, 0, null); // draw an image
}
// *** getters, setters, different code//
private class FractalWorker extends SwingWorker<Object, Object>{
private int y; // row on which worker should work now
private Color[] arrayOfColors; // array of colors produced by workers
public FractalWorker(int z){
y = z;
}
protected Object doInBackground() throws Exception {
arrayOfColors = new Color[getWidth()];
for(int q=0; q<getWidth(); q++){ // calculate and insert into array proper color for given pixel
int iter = calculateIterations(setComplexNumber(new Point(q,y)));
if(iter == iterations){
arrayOfColors[q] = Color.black;
}else{
arrayOfColors[q] = Color.getHSBColor((float)((iter/ 20.0)), 1.0f, 1.0f);
}
}
return null;
}
protected void done(){ // take color from the array and draw pixel
for(int i = 0; i<arrayOfColors.length; i++){
g2d.setColor(arrayOfColors[i]);
g2d.drawLine(i, y, i, y);
}
}
}
如何检查SwingWorkers是否已完成执行done()方法中的内容?我不确定在开始我的程序时创建BufferedImage是什么意思 - 我想通过调用paintComponent()方法来实现。 “(...),然后在组件大小改变时重新创建它。” - 我没有改变我的组件的大小。感谢您的帮助。 – Trishia 2012-04-17 18:27:56
你的程序不应该直接调用paintComponent方法 - Swing会这样做。您的程序必须能够快速响应任何paintComponent调用。这就是为什么在paintComponent启动之前你必须准备好并绘制图像。如果图像内容发生变化,则需要通过调用panel.repaint()重新绘制组件,这将导致Swing调用paintComponent。 要确定您的SwingWorkers是否已完成,您可以在done()的末尾设置布尔值,然后保留所有工作人员的列表,并在检查布尔值时对其进行迭代。 – 2012-04-18 08:33:18