Hi,
I am trying to draw a stop light using Ellipse2D.Double but I'm running into a problem. Everytime I draw a new circle of a different color, it makes all of the circles the same color. Any help is appreciated!
Here's my code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* This class (StopLightView.java) displays the stop light's GUI
* @author Steve
*
*/
public class StopLightView
{
/** The JFrame representing a stop light */
private JFrame theStopLight_;
/** The JPanel representing the red light */
private JPanel theRedLight_;
/** The JPanel representing the yellow light */
private JPanel theYellowLight_;
/** The JPanel representing the green light */
private JPanel theGreenLight_;
/**
* Instantiates all of the necessary components of the stop light.
* @return StopLightView an instance of this class.
*/
public StopLightView createStopLight()
{
StopLightFactory myFactory = new StopLightFactory();
theRedLight_ = myFactory.createLight(Color.RED);
theYellowLight_ = myFactory.createLight(Color.YELLOW);
theGreenLight_ = myFactory.createLight(Color.GREEN);
theStopLight_ = myFactory.makeStopLight();
return (this);
}
/**
* Makes the instance of this class visible
*/
public void show()
{
theStopLight_.setVisible(true);
}
/**
* This class creates StopLights
*/
private final class StopLightFactory
{
/** The current color of the light to be produced. */
private Color theColor_;
/** The shape of the light */
private Ellipse2D.Double theLightShape_;
/**
* This method is used to make a stop light with one red light, one green light, and one yellow light.
* @return JFrame that represents a stop light
*/
private JFrame makeStopLight()
{
// Create our lights
theRedLight_ = createLight(Color.RED);
theYellowLight_ = createLight(Color.YELLOW);
theGreenLight_ = createLight(Color.GREEN);
// Setup the constraints for our layout
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.CENTER;
// Create the panel and set its attributes
JPanel stopLightPanel = new JPanel(new GridBagLayout());
stopLightPanel.setSize(new Dimension(300, 500));
// Add our lights
stopLightPanel.add(theRedLight_, gbc);
gbc.gridy = 1;
stopLightPanel.add(theYellowLight_, gbc);
gbc.gridy = 2;
stopLightPanel.add(theGreenLight_, gbc);
// Create the frame that holds everything and add the components
JFrame stopLightFrame = new JFrame("Stop Light v1.0");
stopLightFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
stopLightFrame.setPreferredSize(new Dimension(450, 650));
stopLightFrame.getContentPane().add(stopLightPanel);
stopLightFrame.pack();
return stopLightFrame;
}
/**
* Creates a light with a specified color
* @param color the color the light should be
* @return JPanel a panel with a light drawn on it
*/
private JPanel createLight(Color color)
{
theColor_ = color;
theLightShape_ = new Ellipse2D.Double(25, 0, 150, 150);
JPanel lightPanel = new JPanel()
{
public void paintComponent(Graphics graphics)
{
Graphics2D graphics2D = (Graphics2D)graphics;
graphics2D.setColor(theColor_);
graphics2D.fill(theLightShape_);
paintComponents(graphics);
}
};
lightPanel.setPreferredSize(new Dimension(200, 200));
return lightPanel;
}
}
}
/**
* Test driver
* @author (StopLight.java)
*
*/
public class StopLight
{
public static void main(String[] args)
{
System.out.println("Creating an instance of the view");
StopLightView view = new StopLightView();
view.createStopLight().show();
}
}