4

I am working on a "paint-like" application (a little drawing software) to familiarize with Java 2D components. Here is my problem: I have a JFrame whose ContentPane is an instance of a class inheriting from JPanel. I want to set the background color to white but it remains on its default color... The class name corresponding to ContentPane is Container. Here is a simplified code:

public class Container extends JPanel {

    public Container() {
        super();
        this.setBackground(Color.WHITE);
    }
}

The JFrame constructor contains the line:

this.setContentPane(mainContainer);

Have I missed something?

Thx.

Goatcat
  • 1,133
  • 2
  • 14
  • 31
MarAja
  • 1,547
  • 4
  • 20
  • 36
  • When I add the line: jFrame.setBackground(Color.WHITE); It works... Nevertheless, I would like to understand why it doesn't work with my contentPane. Any idea? Thx. – MarAja Jul 31 '13 at 11:42

4 Answers4

9

This could fix it...

public class Container extends JPanel
{
    public Container() 
    {
        super();
        this.setOpaque(true);
        this.setBackground(Color.WHITE);
    }
}
  • I also have the same problem! I specifically want to be able to click my buttons and alternate the colors but that doesn't work at all – Sidharth Ghoshal Jan 15 '14 at 18:20
4

For some components, the background is switched off by default. The background color is only applied to opaque widgets. Call the following method for all components in your widget hierarchy that should paint their background:

component.setOpaque(true);
Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
0

I also had that problem, and only that worked out is exactly what OP suggested.

// Only this works for me
this.setBackground(Color.blue);

Whole code of example class is here (just for purpose to show where I tried to put/set setBackground();

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class CircleDraw extends JFrame {
    Float diameter = 150f;

    public CircleDraw() {
        super("Circle Draw");
        this.setSize(300, 300);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.add(new CirclePanel(diameter));
        this.setVisible(true);

        // Only this works for me
        this.setBackground(Color.blue);
    }

    public static void main(String[] args) {
        new CircleDraw();
    }
}

class CirclePanel extends JPanel {

    Float diameter;

    public CirclePanel(Float diameter) {
        super();
        // this.setOpaque(true);
        // this.setBackground(Color.WHITE);
        this.diameter = diameter;
    }

    @Override
    public void paintComponent(Graphics g) {

        int panelWidth = this.getSize().width;
        int panelHeight = this.getSize().height;

        setPreferredSize(new Dimension(300, 300));
        Graphics2D comp2D = (Graphics2D) g;
        comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        comp2D.setStroke(new BasicStroke(1f));
        // comp2D.setBackground(Color.white);
        comp2D.setPaint(Color.white);

        Ellipse2D.Float e1 = new Ellipse2D.Float((panelWidth / 2) - (diameter / 2), (panelHeight / 2) - (diameter / 2), diameter, diameter);
        comp2D.draw(e1);
    }
}
Nenad Bulatović
  • 7,238
  • 14
  • 83
  • 113
  • Add the call to `super.paintComponent(g);` in the override method to paint the JPanel properly :D – HelloImKevo Oct 14 '14 at 00:16
  • It will not work as you think it would. Where would you exactly put that statement? – Nenad Bulatović Oct 14 '14 at 08:24
  • If you change the code to: `public CircleDraw() { // ... } public static void main(String[] args) { new CircleDraw(); } public CirclePanel(Float diameter) { super(); this.setOpaque(true); this.setBackground(Color.BLUE); this.diameter = diameter; } @Override public void paintComponent(Graphics g) { super.paintComponent(g); // ... }` That should work as intended; a blue panel and white circle (if I'm understanding the intent correctly) – HelloImKevo Oct 15 '14 at 17:40
0

you need to include the super function in your override function. IE

 protected void paintComponent(Graphics g){
    super.paintComponent(g); // this sets the background normaly
    g.setColor(Color.BLACK);
    g.drawLine(0,0,getWidth(),getHeight());
}
curtis
  • 1