The drawing drawn in paintComponent () is immediately lost

I am new to Java, Swing and GUI programming, so I probably miss a few central points about creating a GUI with Swing and a flow model. The exercise I'm trying consists in a small application for creating, moving, and resizing shapes on canvas. Moreover, I try to make the presentation as useless as possible, since the Presenter was responsible for injecting the desired behavior. In other words, I don’t want View to know about the numbers and how to draw them, it just offers the setUpdater () method for Presenter to provide an object that knows what needs to be done to represent the Model state.

But I found a problem: in some circumstances, the numbers are lost. For example, if I label and then de-initialize the application window. I thought that the paintComponent () of my canvas component was not being called, but the breakpoint showed me that the problem was different: it was called and the shapes were drawn, but then disappeared.

I tried to simplify my code to show the problem without buttons, sliders or even a model. However, I keep separate classes for presentation and presenter, as this separation is important for my purposes.

In the machine where I am testing a simplified example, the figure that is drawn when calling paintComponent (always the same circle) disappears not only after deionization, but also whenever it is painted.

Please help me understand what is happening ... and how to solve it.

TYIA.

PS1: Simplified Code:

import java.awt.*;
import javax.swing.*;

interface ViewUpdater {
    void updateCanvas(Graphics2D g2d);
}

class View {
    private JFrame windowFrame;
    private JPanel canvasPanel;
    private ViewUpdater updater;
    public static final Color CANVAS_COLOR = Color.white;
    public static final int CANVAS_SIDE = 500;

    public View() {
        windowFrame = new JFrame();
        windowFrame.
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        canvasPanel = new JCanvas();
        canvasPanel.setBackground(CANVAS_COLOR);
        canvasPanel.
            setPreferredSize(new Dimension(CANVAS_SIDE,
                                           CANVAS_SIDE));
        windowFrame.getContentPane().add(canvasPanel);
        windowFrame.pack();
        windowFrame.setResizable(false);
    }

    public void setVisible() {
        windowFrame.setVisible(true);
    }

    public void setUpdater(ViewUpdater updater) {
        this.updater = updater;
    }

    public void updateView() {
        System.out.println("BEGIN updateView");
        Graphics2D g2d =(Graphics2D) canvasPanel.getGraphics();
        g2d.setColor(CANVAS_COLOR);
        g2d.fillRect(0, 0, CANVAS_SIDE, CANVAS_SIDE);
        if (updater != null) {
            System.out.println("GOING TO updateCanvas");
            updater.updateCanvas(g2d);
        }
        System.out.println("END updateView");
   }

    private class JCanvas extends JPanel {

        private static final long serialVersionUID =
                                    7953366724224116650L;

        @Override
        protected void paintComponent(Graphics g) {
            System.out.println("BEGIN paintComponent");
            super.paintComponent(g);
            updateView();
            System.out.println("END paintComponent");
        }
    }
}

class Presenter {
    private View view;
    private static final Color FIGURE_COLOR = Color.black;

    public Presenter(View view) {
        this.view = view;
        this.view.setUpdater(new ProjectViewUpdater());
        this.view.setVisible();
    }

    private class ProjectViewUpdater
        implements ViewUpdater {
        @Override
        public void updateCanvas(Graphics2D g2d) {
            g2d.setColor(FIGURE_COLOR);
            g2d.drawOval(100, 100, 300, 300);
            // The circle immediately disappears!
        }
    }
}

public class Main {
    public static void main(String[] args) {
        new Presenter(new View());
    }
}

PS2: http://www.javaworld.com/javaworld/jw-08-2007/jw-08-swingthreading.html, , Swing, .

PS3: Google, , , http://www.eclipse.org/forums/index.php/t/139776/.

+3
1

, Graphics, getGraphics() JPanel, Graphics , , , .

: .

  • Graphics, paintComponent JVM
  • getGraphics() createGraphics() BufferedImage, Graphics Graphics2D, , , BufferedImage JComponent JPanel paintComponent(...), Graphics JVM.

, BufferedImage, 2 .

+4

All Articles