Solved

Keeping the background when painting a JPanel

Posted on 1999-01-13
2
225 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
where is session ID cache stored 1 46
Eclipse IDE - Cannot copy/paste from console output 8 126
mockito example issue 8 62
nextBoolean(double p) for Random class 3 35
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

910 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

21 Experts available now in Live!

Get 1:1 Help Now