Solved

Keeping the background when painting a JPanel

Posted on 1999-01-13
2
217 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
Comment Utility
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_
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now