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]

# 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.