I'm making a program that has an image that you scroll around on, and I can't figure out how to update the image if a button is pressed (For example: Adds a Green Ellipse to the image.) It already draws the image into the JScrollPane and you can scroll around, but when you click a button it doesn't refresh the image. (more details in code) Here is the code:
public class PegMaster extends JPanel implements ActionListener {
    //Note: not complete code
    public PegBox[] pegbox = new PegBox[9];
    public static Dimension size = new Dimension(520, 500);
    public BufferedImage canvas;
    public Graphics2D g2d;
    public JScrollPane scroller;
    JPanel panel;
    private Canvas window;
    JScrollPane pictureScrollPane;
    public PegMaster() {
        JButton button = new JButton("test");
        button.addActionListener(this);
        add(button);
        canvas = new BufferedImage((int)size.getWidth()-30, 75 * GUESSES, BufferedImage.TYPE_INT_RGB);
        g2d = canvas.createGraphics();
        for(int i = 0;i<=pegbox.length-1;i++) {
           pegbox[i] = new PegBox(i, g2d);
        }
        window = new Canvas(new ImageIcon(toImage(canvas)), 1);
        //Class Canvas is a Scrollable JLabel to draw to (the image)
        pictureScrollPane = new JScrollPane(window);
        pictureScrollPane.setPreferredSize(new Dimension((int)size.getWidth()-10, (int)size.getHeight()-20));
        pictureScrollPane.setViewportBorder(BorderFactory.createLineBorder(Color.black));
        add(pictureScrollPane);
        //adds the scrollpane, but can't update the image in it
    }
    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createGUI();
                //just adds the scrollpane
            }
        });
    }
    public void paint(Graphics g) {
        super.paint(g);
        for(int i = 0;i<=pegbox.length-1;i++) {
            //pegbox[i] = new PegBox(i);
            pegbox[i].draw(g2d);
        }
        try {
            Thread.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }   
        //tried re-making the scrollpane, didn't work.
        //window = new Canvas(new ImageIcon(toImage(canvas)), 1);
        //pictureScrollPane = new JScrollPane(window);
        //pictureScrollPane.setPreferredSize(new Dimension((int)size.getWidth()-10 (int)size.getHeight()-20));
        //pictureScrollPane.setViewportBorder(BorderFactory.createLineBorder(Color.black));
        //tried imageupdate: pictureScrollPane.imageUpdate(canvas, 0, 0, 0 (int)size.getWidth()-10, (int)size.getHeight()-20);
        //remove(pictureScrollPane);
        //tried this: pictureScrollPane.revalidate();
        repaint();
    }
}