Solved

Keeping the background when painting a JPanel

Posted on 1999-01-13
2
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

752 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