1

outputImage

Everything works fine but when i scroll while the graphics is being generated, the scrolled portion graphics disappears

MainApp.java

public abstract class MainApp implements ActionListener{

//Just Listing the method that triggers Canvas(extends JPanel and starts painting)
public void startGeneration(){
    String rule = (String) cb.getSelectedItem();
        Jexception.setVisible(false);
        Jexception1.setVisible(false);
        if(genNum > 0){
            cgs = new CAGenerationSet(genNum, rule);
            cAGenList = new ArrayList<>();
            cAGenList = cgs.run(cgs);
            if(sp!= null) {
                frame.remove(sp);
                if(canvas!=null) sp.remove(canvas);
            }
            //initializing canvas and adding it to the JScrollPane
            canvas = new Canvas(cAGenList);
            sp = new JScrollPane();
            splitPane.setDividerLocation(80);
            splitPane.setRightComponent(sp);
            sp.setVisible(true);

            EventQueue.invokeLater(new Runnable() {
                  public void run() {
                    sp.setViewportView(canvas);
                    sp.revalidate();
                  }
                });
            }
            else {
                Jexception.setVisible(false);
                Jexception1.setVisible(true);
            }
  }
}

Canvas extends JPanel. Here I am overriding the paintComponent() and calling the draw method that triggers the SwingWorker to draw the graphics.

Canvas.java

public class Canvas extends JPanel {
ArrayList<CAGeneration> cAGenList;
private int y,x;
private Thread t;
public static SwingWorker<Void, Void> worker;
private int count = 0;
private boolean check = false;

public CACanvas(ArrayList<CAGeneration> cAGenList) {
    this.cAGenList = cAGenList;
}

@Override
public Dimension getPreferredSize() {
    return new Dimension(800, 800);
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    revalidate();
    if(!check){
        drawCA(g, this.cAGenList);
    }

}

private void drawCA(Graphics g, ArrayList<CAGeneration> cAGenList ) { 
        worker = new SwingWorker<Void, Void>() {
            @Override
            protected Void doInBackground() throws Exception {
                Graphics2D g2d = (Graphics2D) g;
                check = true;
                y= 10;
                count = 0;
                synchronized (cAGenList) {
                for(CAGeneration cg: cAGenList){

                    count++;
                    y = y+10;
                    x = 0;
                    if(!isCancelled()){
                        System.out.println(Thread.currentThread().getName());
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                        }
                        for(int i=0; i<cg.getcACell().length; i++){
                            x = x+10;
                            if (cg.getcACell()[i] == 0) {
                                paintRect(g2d, x, y, 9, Color.WHITE);
                            }
                            if (cg.getcACell()[i] == 1) {
                                paintRect(g2d, x, y, 9, Color.GRAY);
                            }
                            if (cg.getcACell()[i] == 2) {
                                paintRect(g2d, x, y, 9, Color.BLACK);   
                            }

                        }
                    }
                }
            }
                check = false;
                return null;
            }
        };

        ExecutorService threadPool = Executors.newSingleThreadExecutor();
        threadPool.submit(worker);
}



private void paintRect(Graphics2D g2D, int x, int y, int size, Color color){
    if(g2D!=null) g2D = (Graphics2D) getGraphics();
    g2D.setColor(color);
    g2D.fillRect(x,  y, size, size);
}
}
Pooya
  • 6,083
  • 3
  • 23
  • 43
rapss
  • 11
  • 1
  • You're approach is wrong, instead of trying to generate the graphics inside a `SwingWorker` directly to the `Graphics` context each time `paint` is called, you should generate the content, maybe to a `BufferedImage` and then, when it's read, paint it. Painting but it's nature is destructive, you are expected to repaint the entire state of the component every time `paint` is called – MadProgrammer Apr 12 '16 at 06:12
  • @MadProgrammer Thanks ! for the suggestion. I will try to use BufferedImage and reply back. – rapss Apr 12 '16 at 06:33
  • For [example](http://stackoverflow.com/a/25043676/230513). – trashgod Apr 12 '16 at 11:20

0 Answers0