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

  • Home
  • SEARCH
  • 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 6361261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:45:13+00:00 2026-05-24T23:45:13+00:00

In my gui that I have created in Java, every time I minimize the

  • 0

In my gui that I have created in Java, every time I minimize the window, repaint is called and the drawing that was originally there vanishes once the window is maximized (back to normal)

So I created an action Listener to detect when the window had been minimized and then maximized for it to draw each point that was originally on the panel back to the screen.
Unfortunately, I am not getting the results that I expected.
For some reason, when the window is maximized each point is drawn back onto the panel but the background of the entire window is black. Also, this can be very annoying for the user to have to wait for each point to be drawn back onto the screen each time they want to minimize the window.

Here is what I have so far:

    //Redraw plot if window is minimized
    window.addWindowStateListener(new WindowAdapter()
    {
        Graphics g = dPanel.getGraphics();

        public void windowStateChanged(WindowEvent ev) 
        {
            boolean minimized = false;

            //If user minimizes window and then maximizes window
            if(window.getExtendedState() == Frame.ICONIFIED )
            {
                minimized = true;
                System.out.println("Window minimized");
            }

            if(ev.getNewState() == Frame.NORMAL || minimized == true)
            {

                System.out.println("Window back to normal state");

                //Draw each Point back onto the screen
                for(Point i: PointArray)
                {
                    drawPoint(g, i, startColor); 
                    System.out.println("Panel Repaint");
                }
            }
        }
    });
}

Is there some way that my code can be edited to achieve the desired results or is there a better way to do this. Basically, I just want that when the user minimizes the GUI, the painting that was there before minimization is still there once the user maximizes the window. Also, moving the window around can also cause parts of the panel to be repainted or the entire panel to be repainted.

    //Draws point onto panel
public void drawPoint(Graphics g, Point PointArray, Color color)
{
    Graphics2D g2d = (Graphics2D)g;
    g2d.setStroke(new BasicStroke(2f));
    g.setColor(color); //g2d.setColor(Color.black); 
    g2d.drawOval((int)PointArray.a, (int)PointArray.b, 2, 2);
}  
  • 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-05-24T23:45:14+00:00Added an answer on May 24, 2026 at 11:45 pm

    The more I read on your situation and need, the more I think you should actually use getGraphics, but do not call this on a JComponent such as a JPanel. Rather you should do your drawing in a BufferedImage, and you should get the Graphics object of the BufferedImage by calling getGraphics on the BufferedImage. You can then draw this BufferedImage in a JComponent’s paintComponent method with the Graphics#drawImage(…) method, or even better for its simplicity (and if you don’t want to use the image as a background for a JPanel or gui), draw the Image in an ImageIcon that is displayed in a JLabel. One caveat though is if you get your Graphics object in ths way, don’t forget to dispose of it when you’re done.

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

Sidebar

Related Questions

I have GUI created using netbeans GUI builder. I want to add there an
I have created a GUI in Java using swings with the help of Netbeans
I have created one GUI using Swing of Java. I have to now set
I have a program with a GUI that needs to open a separate window
I'm a Java beginner. I already created a simple GUI application that display will
I have a program with Driver GUI java file that creates a JFrame and
I'm currently developing a GUI for a Java-application that I've created. I would like
I have a fat GUI that it getting fairly complex, and I would like
I have a GUI that is going to do a lot of disparate tasks.
I have a GUI that is issuing commands to a web server based on

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.