| | |
Not able to set the background color of JComponent
![]() |
•
•
Join Date: Mar 2007
Posts: 2
Reputation:
Solved Threads: 0
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.*;
public class Tanks extends JApplet
{
public static final int WIDTH = 400;
public static final int 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);
currentSurface.setBackground(Color.white);
button1 = new JButton("Start");
button1.addActionListener(b1);
panel.add(button1);
this.add(panel,BorderLayout.NORTH);
}
public void init()
{
this.setSize(WIDTH, HEIGHT);
this.setVisible(true);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand() == "Start")
{
currentSurface.figure="Start";
repaint();
}
}
}
}
class AnimationThread extends Thread
{
JApplet c;
public AnimationThread(JApplet c)
{
this.c = c;
}
public void run()
{
}
}
class PaintSurface extends JComponent
{
String figure;
public PaintSurface()
{
}
public void 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);
}
}
} You should be overriding paintComponent, and not paint. You also need to call setOpaque on the component.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
![]() |
Similar Threads
- need help changing background color of an iframe (JavaScript / DHTML / AJAX)
- iframe background color - pls help (HTML and CSS)
- HTML 3.2 table background color (HTML and CSS)
- How Vb.NET Textbox background color change when TabStop come? (VB.NET)
- VS 2005 ListViews not holding background color (C#)
- help with setting background color!!! (Java)
- How do i set a page background color? (HTML and CSS)
Other Threads in the Java Forum
- Previous Thread: String.replace() Method
- Next Thread: JTextField to only accept numbers
| Thread Tools | Search this Thread |
6 actuate android api applet application applications array arrays automation balls bank binary bluetooth bold business c++ chat class clear client code codesnippet collections component coordinates database defaultmethod development dice doctype dragging ebook eclipse educational error file formatingtextintooltipjava fractal froglogic game givemetehcodez graphics gui hql html ide ideas image infinite ingres integer intersect invokingapacheantprogrammatically j2me java javaexcel javaprojects jni jpanel jtextarea julia linux list map method methods mobile mysql netbeans openjavafx parameter php problem program programming project recursion repositories scanner scrollbar sell server set sms sort sorting sql sqlserver state storm string sun superclass swing swt threads tree websites windows






