Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7873815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:37:25+00:00 2026-06-03T02:37:25+00:00

I am a newcomer to Java, Swing, and GUI programming, so I am probably

  • 0

I am a newcomer to Java, Swing, and GUI programming, so I am probably missing a number of central points about building a GUI with Swing and the thread model behind. The exercise I am trying consist in a little application for creating, moving and resizing figures on a canvas. Moreover, I am trying to keep the View as behaviourless as possible, with a Presenter object being responsible for injecting the desired behaviour. In other words, I do not want the View to know about figures or how they must be drawn, it simply offers a setUpdater() method for the Presenter to provide an object that knows what must be drawn in order to represent the state of a Model.

But I have found a problem: under some circumstances, figures are lost. For instance, if I iconify and then deiconify the application window. I thought the paintComponent() of my canvas component was not called, but a breakpoint showed me the problem was different: it was called and the figures were painted, but then dissapeared.

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

In the machine where I am testing the simplified example, the figure that is drawn when paintComponent is called (always the same circle) dissapears not only after deiconification, but every time it is painted.

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

TYIA.

PS1: The simplified code follows:

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: I am reading http://www.javaworld.com/javaworld/jw-08-2007/jw-08-swingthreading.html in order to understand the thread model involved in using Swing, but I still have not done anything special for controlling threads in my code.

PS3: I have not found the answer to my problem googling, the most similar issue is maybe the one unanswered in http://www.eclipse.org/forums/index.php/t/139776/.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:37:28+00:00Added an answer on June 3, 2026 at 2:37 am

    Your problem is that you’re getting your Graphics object by calling getGraphics() on JPanel, and any Graphics object thus obtained will not be long-lasting, so that anything drawn with it will likewise disappear on the next repaint.

    The solution: don’t do this. Either

    • draw with the Graphics object given to the paintComponent method by the JVM or
    • call getGraphics() or createGraphics() on a BufferedImage to get its Graphics or Graphics2D object, draw with this, dispose of it, and then draw the BufferedImage in the JComponent or JPanel’s paintComponent(...) method again using the Graphics object passed in by the JVM.

    For your situation, I think your best off using a BufferedImage, or point 2 above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm the newcomer into java ee,I have studied about core java,servlet,jsp. Could anyone give
I am a newcomer to Python and is in need of programming a simple
I am building a client server application using Java Sockets (in Windows XP). For
I'm newcomer in django, and here is question: I have model class: def Client(models.User)
I'm a newcomer of OOP, so I has one silly question about when a
I am a newcomer to RoR and have a question about routing. I have
Please consider that im a newcomer to c#. After scanning about 700 posts i
For a newcomer to .NET Web Development and programming in general, who chooses C#
I'm a newcomer to the idea of aspect-oriented programming but I would like to
Java EE 6 newcomer question ahead, so beware... I keep reading introductory CDI material

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.