Not able to set the background color of JComponent

In my JApplet, I am able to change the background color of the panel but not the currentSurface which is an instance of PaintSurface extends JComponent. Could someone please help me out? Many thanks

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.awt.geom.*;

publicclass Tanksextends JApplet

{

publicstaticfinalint WIDTH = 400;

publicstaticfinalint HEIGHT = 400;

private JButton button1;

private PaintSurface currentSurface =new PaintSurface();

public Tanks()

{

this.add(currentSurface,BorderLayout.CENTER);

ButtonListener b1 =new ButtonListener();

JPanel panel =new JPanel();

panel.setBackground(Color.white);

[b]currentSurface.setBackground(Color.white);[/b]

button1 =new JButton("Start");

button1.addActionListener(b1);

panel.add(button1);

this.add(panel,BorderLayout.NORTH);

}

publicvoid init()

{

this.setSize(WIDTH, HEIGHT);

this.setVisible(true);

}

privateclass ButtonListenerimplements ActionListener

{

publicvoid actionPerformed(ActionEvent e)

{

if(e.getActionCommand() =="Start")

{

currentSurface.figure="Start";

repaint();

}

}

}

}

class AnimationThreadextends Thread

{

JApplet c;

public AnimationThread(JApplet c)

{

this.c = c;

}

publicvoid run()

{

}

}

class PaintSurfaceextends JComponent

{

String figure;

public PaintSurface()

{

}

publicvoid paint(Graphics g)

{

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(

RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

if(figure=="Start")

{

Shape s =new Ellipse2D.Float(20,50,250,150);

g2.setPaint(Color.BLACK);

g2.draw(s);

}

}

}

[4356 byte] By [Ken@Javaa] at [2007-11-26 22:43:10]
# 1
Either extend JPanel instead of JComponent, or in your PaintSurface constructor call "setOpaque(true);"
itchyscratchya at 2007-7-10 11:59:16 > top of Java-index,Desktop,Core GUI APIs...
# 2
> Either extend JPanel instead of JComponent, or in> your PaintSurface constructor call "setOpaque(true);"I tried both of your methods, and it still doesn't work.
Ken@Javaa at 2007-7-10 11:59:16 > top of Java-index,Desktop,Core GUI APIs...
# 3
Ah, yes, something else as well: don't override paint(). Override paintComponent(), and call super.paintComponent() as the first line in your implementation.
itchyscratchya at 2007-7-10 11:59:16 > top of Java-index,Desktop,Core GUI APIs...
# 4

> Ah, yes, something else as well: don't override

> paint(). Override paintComponent(), and call

> super.paintComponent() as the first line in your

> implementation.

I got an error: paintComponent(java.awt.Graphics) in javax.swing.JComponent cannot be applied to () super.paintComponent();

Ken@Javaa at 2007-7-10 11:59:16 > top of Java-index,Desktop,Core GUI APIs...
# 5
Yes, you need to pass the Graphics object.
itchyscratchya at 2007-7-10 11:59:16 > top of Java-index,Desktop,Core GUI APIs...
# 6
It works. But could you please tell me why setBackground() doesn't work when I extends JComponent?
Ken@Javaa at 2007-7-10 11:59:16 > top of Java-index,Desktop,Core GUI APIs...
# 7
Because JComponent is not opaque by default.
itchyscratchya at 2007-7-10 11:59:16 > top of Java-index,Desktop,Core GUI APIs...
# 8
Thank you very much itchyscratchy.
Ken@Javaa at 2007-7-10 11:59:16 > top of Java-index,Desktop,Core GUI APIs...
# 9

> It works. But could you please tell me why setBackground() doesn't work when I extends JComponent?

JComponent doesn't do any custom painting so the background is not painted.

JPanel extends JComponent and adds custom painting to paint the background (and of couse sets the panel to opaque so the painting works).

camickra at 2007-7-10 11:59:16 > top of Java-index,Desktop,Core GUI APIs...
# 10
Is there anything else to distinguish JComponent from JPanel, as a base classfrom which to derive custom components? From which should one derive?Does it make any difference?
DrLaszloJamfa at 2007-7-10 11:59:16 > top of Java-index,Desktop,Core GUI APIs...
# 11

> Is there anything else to distinguish JComponent from JPanel,

Just narrowed down the reason why the background isn't painted. Background painting is supported in the ComponentUI class. However, JComponent doesn't have a UI.

When you extend from JPanel, you automatically inherit a simple UI for the component.

To add the default ComponentUI to your custom component when you extend JComponent you can do the following:

import java.awt.*;

import javax.swing.*;

import javax.swing.plaf.*;

public class MyComponent extends JComponent

{

public MyComponent()

{

super();

setUI( new ComponentUI() {});

}

public static void main(String args[])

throws Exception

{

MyComponent component = new MyComponent();

component.setOpaque(true);

component.setBackground(Color.RED);

component.setPreferredSize( new Dimension(250, 250) );

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add( component );

frame.pack();

frame.setLocationRelativeTo( null );

frame.setVisible( true );

}

}

Now the background will be painted.

> From which should one derive? Does it make any difference?

I know nothing about how the LAF support on Swing works so I will just suggest you take a look at the installUI() method of the ComponentUI class. This method should completely configure the component for the look and feel.

So if you want to inherit the LAF of JPanel then I guess you start with JPanel. Looking at the code for the BasicPanelUI the following properties appear to be set:

LookAndFeel.installColorsAndFont(p, "Panel.background", "Panel.foreground", "Panel.font");

LookAndFeel.installBorder(p,"Panel.border");

If you want to make sure your component has its own unique LAF then I guess you would extend JComponent.

camickra at 2007-7-10 11:59:16 > top of Java-index,Desktop,Core GUI APIs...