Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Keeping the background when painting a JPanel

Posted on 1999-01-13
2
Medium Priority
?
260 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 80 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
Suggested Courses

926 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