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 6357861
In Process

The Archive Base Latest Questions

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

I have a custom JPanel and sometimes throughout my program, I need to call

  • 0

I have a custom JPanel and sometimes throughout my program, I need to call a method which paints the screen black, that’s it.

public void clearScreen() {
    Graphics g = getGraphics();
    g.setColor(Color.black);
    g.fillRect(0,0,getWidth(),getHeight());
}

When I launch the program, I call this method.

However, I find that sometimes it works, and sometimes it doesn’t. It’s very odd. I also found out that when it doesn’t work, the graphics object is NOT null, and the width and height are also correctly defined (from getWidth() and getHeight()).

Why would this sometimes work and sometimes not work?

What is the correct way to make a custom drawing on my JPanel at some point in the program? Is it correct to use getGraphics() as I am doing? My JPanel (at some point) has JComponents, but later on I remove those JComponents and do some custom graphics drawing. Why would this sometimes only work?

  • 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:15:16+00:00Added an answer on May 24, 2026 at 11:15 pm

    Don’t get your Graphics object by calling getGraphics on a component such as a JPanel since the Graphics object obtained will not persist on the next repaint (which is likely the source of your problems).

    Instead, consider doing all of your drawing in a BufferedImage, and then you can use getGraphics() to your heart’s content. If you do this, don’t forget to dispose of the Graphics object when you’re done painting with it.

    e.g.,

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import javax.swing.JPanel;
    
    @SuppressWarnings("serial")
    public class MyPaint extends JPanel {
       public static final int IMG_WIDTH = 400;
       public static final int IMG_HEIGHT = IMG_WIDTH;
    
       private BufferedImage image = new BufferedImage(IMG_WIDTH, IMG_HEIGHT,
                BufferedImage.TYPE_INT_ARGB);
    
       public MyPaint() {
          MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
          addMouseListener(myMouseAdapter);
          addMouseMotionListener(myMouseAdapter);
       }
    
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          if (image != null) {
             g.drawImage(image, 0, 0, null);
          }
       }
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(IMG_WIDTH, IMG_HEIGHT);
       }
    
       public void clearScreen() {
          Graphics g = image.getGraphics();
          g.setColor(Color.black);
          g.fillRect(0, 0, image.getWidth(), image.getHeight());
          g.dispose();
          repaint();
       }
    
       private class MyMouseAdapter extends MouseAdapter {
          // code to draw on the buffered image. 
          // Don't forget to call repaint() on the "this" JPanel
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few models that need to have custom find conditions placed on
I have a popup menu that displays dynamically created custom JPanel objects in a
So I have a JFrame which I've added a custom JPanel component. The JPanel
I have created the custom control which is just a panel that I will
I have custom groupViews that need to change state when they are expanded and
I have a custom control that implements IPostBackEventHandler. Some client-side events invoke __doPostBack(controlID, eventArgs).
I have a custom installer action that updates the PATH environment, and creates an
We have a custom-built Flash-based video player that I maintain, and it needs to
I have developed a basic custom JTableModel as follows public class CustomTableModel extends DefaultTableModel
I have a custom component subclassed from JPanel, with a keyboard handler. My main

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.