// For [url]http://www.java-forum.org/awt-swing-swt/89478-designfrage-bei-rechenintensiver-operation.html#post565754[/url]
import javax.swing.*;
import java.util.*;
import java.util.concurrent.*;
import java.awt.*;
import java.awt.image.*;
import java.beans.*;
public class BildZeichnerFenster extends JFrame
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
BildZeichnerFenster bildZeichnerFenster =
new BildZeichnerFenster();
bildZeichnerFenster.setVisible(true);
bildZeichnerFenster.startCreation();
}
});
}
private ImagePanel imagePanel;
public BildZeichnerFenster()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800,600);
imagePanel = new ImagePanel();
getContentPane().add(imagePanel);
}
public void startCreation()
{
imagePanel.setImage(null);
ImageWorker imageWorker = new ImageWorker();
ProgressDialog progressDialog = new ProgressDialog(this, imageWorker);
imageWorker.execute();
progressDialog.setVisible(true);
Image image = null;
try
{
image = imageWorker.get();
}
catch (ExecutionException e)
{
e.printStackTrace();
}
catch (InterruptedException e)
{
Thread.currentThread().interrupt();
}
imagePanel.setImage(image);
}
}
class ImageWorker extends SwingWorker<Image, Void>
{
@Override
public Image doInBackground()
{
int w = 500;
int h = 500;
int lines = 1000000;
BufferedImage image=new BufferedImage(w,h, BufferedImage.TYPE_INT_RGB);
Graphics graphics = image.getGraphics();
Random random = new Random();
for (int i=0; i<lines; i++)
{
int x0 = random.nextInt(w);
int y0 = random.nextInt(h);
int x1 = random.nextInt(w);
int y1 = random.nextInt(h);
int r = random.nextInt(256);
int g = random.nextInt(256);
int b = random.nextInt(256);
graphics.setColor(new Color(r,g,b));
graphics.drawLine(x0,y0,x1,y1);
int progress = (int)(((float)i/lines) * 100);
setProgress(Math.min(progress, 100));
}
return image;
}
}
class ProgressDialog extends JDialog implements PropertyChangeListener
{
private SwingWorker<?,?> swingWorker;
private JProgressBar progressBar;
public ProgressDialog(JFrame owner, SwingWorker<?,?> swingWorker)
{
super(owner, "Computing...", true);
this.swingWorker = swingWorker;
progressBar = new JProgressBar();
progressBar.setStringPainted(true);
getContentPane().add(progressBar);
swingWorker.addPropertyChangeListener(this);
pack();
setLocationRelativeTo(owner);
}
@Override
public void propertyChange(PropertyChangeEvent event)
{
if ("state".equals(event.getPropertyName()) &&
SwingWorker.StateValue.DONE == event.getNewValue())
{
setVisible(false);
swingWorker.removePropertyChangeListener(this);
dispose();
}
else if ("progress".equals(event.getPropertyName()))
{
int progress = (Integer) event.getNewValue();
progressBar.setValue(progress);
}
}
}
class ImagePanel extends JPanel
{
private Image image;
public void setImage(Image image)
{
this.image = image;
repaint();
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (image == null)
{
g.drawString("Image not computed yet", 16, 16);
}
else
{
g.drawImage(image, 0, 0, this);
}
}
}