Not able to set the background color of JComponent

Reply

Join Date: Mar 2007
Posts: 2
Reputation: jk1998 is an unknown quantity at this point 
Solved Threads: 0
jk1998 jk1998 is offline Offline
Newbie Poster

Not able to set the background color of JComponent

 
0
  #1
Mar 23rd, 2007
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);
  }
 
 }
}
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,353
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 251
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Not able to set the background color of JComponent

 
0
  #2
Mar 23rd, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 2
Reputation: jk1998 is an unknown quantity at this point 
Solved Threads: 0
jk1998 jk1998 is offline Offline
Newbie Poster

Re: Not able to set the background color of JComponent

 
0
  #3
Mar 23rd, 2007
Originally Posted by masijade View Post
You should be overriding paintComponent, and not paint. You also need to call setOpaque on the component.
Thanks
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC