Solved

Keeping the background when painting a JPanel

Posted on 1999-01-13
2
234 Views
Last Modified: 2012-06-27
I've derived a class from JPanel, and I want to draw in it and keep its background black, not gray. Calling

setBackground(Color.black);

in the constructor works fine if I don't override paint(). But if I do override paint(), the background color disappears and I can't figure out how to just keep it around and draw over it. Short of simply drawing a big rectangle over the whole panel, what should I be doing to keep the background color?
0
Comment
Question by:HoJu
  • 2
2 Comments
 
LVL 16

Accepted Solution

by:
heyhey_ earned 20 total points
ID: 1230968
ere are three standard paint 'subroutines' in a Swing architecture.
     paintComponent(g);
     paintBorder(g);
     paintChildren(g);

     and here is the paintComponent method of the JPanel

     /**
      * Overriden from JComponent, paint the backgroud if the component is opaque.
      * The color used is the one returned by getBackground()
      * Override this method if you want to change how the JPanel paints its background.
      *
      * @param g the Graphics context in which the painting occurs
      */
     public void paintComponent(Graphics g) {
         if(isOpaque()) {
             g.setColor(getBackground());
             g.fillRect(0,0,getWidth(),getHeight());
         }
     }

     as you can see you'll have eighter
     - call super.paint(); // enables double buffering
     - call paintComponent();
     - fill the ractengle yourslef.

     hope this helps
       heyhey
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1230969
There is a very nice article at Swing connection (javasoft) about painting in AWT and Swing.

take a look at it.
the best answer to your question is:
you should never override paint() in Swing components - you should override paintComponent().


0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question