ActionListener - actionPerformed
I have program in which a user selects color from drop down, a shape and filled or non filled seletion. The problem is when user tries to draw the shape and color selected in the drawing pane, nothing happens. Not sure what I am missing. Can someone view the code please and help me? What am I missing?
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DrawPanel extends JPanel {
public final static int CIRCLE = 1, SQUARE = 2, LINE = 3;
private int x1, x2, y1, y2;
private int shape;
private boolean fill;
private boolean showStatus;
private int shapeSize = 100;
private Color foreground;
// draw a specified shape
public void paintComponent (Graphics g){
super.paintComponent(g);
// find center
int x=(getSize().width-shapeSize)/2;
int y=(getSize().height-shapeSize)/2;
if (shape == CIRCLE) {
if (fill == true){
g.setColor(foreground);
g.fillOval(x, y, shapeSize, shapeSize);
}
else{
g.setColor(foreground);
g.drawOval(x, y, shapeSize, shapeSize);
}
}
else if (shape == SQUARE){
if (fill == true){
g.setColor(foreground);
g.fillRect(x, y, shapeSize, shapeSize);
}
else if (shape == LINE){
if (fill == true){
g.drawLine(x1,y1, x2, y2);
}
else{
g.setColor(foreground);
g.drawRect(x, y, shapeSize, shapeSize);
}
}
}
}
// set showStatus value
public void setShowStatus (boolean s) {
showStatus = s;
}
// return showstatus value
public boolean getShowStatus () {
return showStatus;
}
// set fill value
public void setFill(boolean isFill) {
fill = isFill;
}
// set shape value
public void setShape(int shapeToDraw) {
shape = shapeToDraw;
}
// set shapeSize value
public void setShapeSize(int newShapeSize) {
shapeSize = newShapeSize;
}
// set foreground value
public void setForeground(Color newColor) {
foreground = newColor;
}
// repaint DrawPanel
public void draw (){
if(showStatus == true)
repaint();
}
}
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Shapes extends JFrame {
private JPanel buttonPanel,fillPanel; // panels for color, reset and fill
private DrawPanel myPanel; // panel for shapes
private JComboBox colorComboBox;
private JRadioButton circleButton, squareButton, lineButton;
private ButtonGroup radioGroup;
private JCheckBox filledButton;
private JButton resetButton;
private boolean isShow;
private int shape;
private boolean isFill = true;
private String colorNames[] = {"Blue", "Green", "Red", "Yellow"};// color names list in ComboBox
private Color colors[] = {Color.blue, Color.green, Color.red, Color.yellow};
public Shapes() {
super("Draw Shapes");
// creat custom drawing panel
myPanel = new DrawPanel(); //
myPanel.setBackground(Color.black);
// set up resetButton register an event handler for resetButton's ActionEvent
resetButton = new JButton ("reset");
resetButton.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent event) {
// call DrawPanel method setShowStatus and pass an parameter to decide if show the shape
myPanel.setShowStatus(true);
isShow = myPanel.getShowStatus();
shape = DrawPanel.SQUARE;
// call DrawPanel method setShape to indicate shape to draw
myPanel.setShape(shape);
// call DrawPanel method setFill to indicate to draw a filled shape
myPanel.setFill(true);
// call DrawPanel method draw
myPanel.draw();
myPanel.setFill(false);
myPanel.setForeground(Color.black);
colorComboBox.setSelectedIndex(0);
filledButton.setSelected(false);
squareButton.setSelected(false);
lineButton.setSelected(false);
}
}// end anonymous inner class
);// end call to addActionListener
// set up colorComboBox & register event handlers for colorComboBox's ItemEvent
colorComboBox = new JComboBox(colorNames);
colorComboBox.setMaximumRowCount(4);
colorComboBox.addItemListener( new ItemListener()
{
public void itemStateChanged(ItemEvent event)
{
if(event.getStateChange() == ItemEvent.SELECTED)
// call DrawPanel method setForeground and pass an element value of colors array
myPanel.setForeground(colors[colorComboBox.getSelectedIndex()]);
myPanel.draw();
}
}); // end call to addItemListener
// set up a pair of RadioButtons & register an event handler for RadioButtons' ItemEvent
squareButton = new JRadioButton ("Square", false);
circleButton = new JRadioButton ("Circle", false);
lineButton = new JRadioButton ("Line", true);
//buttonPanel.setBorder(BorderFactory.createTitledBorder("Shape"));
radioGroup = new ButtonGroup();
radioGroup.add(squareButton);
radioGroup.add(circleButton);
squareButton.addItemListener(new ItemListener()
{ // anonymous inner class to handle squareButton events
public void itemStateChanged (ItemEvent event)
{
if (isShow==true)
{
shape = DrawPanel.SQUARE;
myPanel.setShape(shape);
myPanel.draw();
}
}
});// end call to addItemListener
circleButton.addItemListener(new ItemListener()
{// anonymous inner class to handle circleButton events
public void itemStateChanged (ItemEvent event)
{
if (isShow==true)
{
shape = DrawPanel.CIRCLE;
myPanel.setShape(shape);
myPanel.draw();
}
}
});// end call to addItemListener
lineButton.addItemListener(new ItemListener()
{// anonymous inner class to handle circleButton events
public void itemStateChanged (ItemEvent event)
{
if (isShow==true)
{
shape = DrawPanel.LINE;
myPanel.setShape(shape);
myPanel.draw();
}
}
});// end call to addItemListener
// set up filledButton register an event handler for filledButton's ItemEvent
filledButton = new JCheckBox("Filled", false);
filledButton.addItemListener(new ItemListener()
{// anonymous inner class to handle filledButton events
public void itemStateChanged (ItemEvent event)
{
if (isShow==true)
{
if (event.getStateChange() == ItemEvent.SELECTED)
{
isFill=true;
myPanel.setFill(isFill);
myPanel.draw();
}
else
{
isFill=false;
myPanel.setFill(isFill);
myPanel.draw();
}
}
}
});// end call to addItemListener
// set up panel containing buttons
buttonPanel = new JPanel();
buttonPanel.setBorder(BorderFactory.createTitledBorder("Color"));
buttonPanel.add(colorComboBox);
buttonPanel.add(resetButton);
fillPanel = new JPanel();
fillPanel.setBorder(BorderFactory.createTitledBorder("Fill Shape"));
fillPanel.add(filledButton);
JPanel radioButtonPanel = new JPanel();
radioButtonPanel.setBorder(BorderFactory.createTitledBorder("Shape"));
radioButtonPanel.add(squareButton);
radioButtonPanel.add(circleButton);
radioButtonPanel.add(lineButton);
buttonPanel.add(radioButtonPanel);
//panel to hold everything
JPanel panel;
panel = new JPanel();
panel.setLayout(new GridLayout(1,0,5,5));
panel.add(buttonPanel);
panel.add(fillPanel);
panel.add(radioButtonPanel);
// attach button panel & draw panel to content panel
Container container = getContentPane();
container.add(myPanel, BorderLayout.CENTER);
container.add(panel, BorderLayout.SOUTH);
setLocation(200,200);
setSize(675,500);
setVisible(true);
}
public static void main(String args[]) {
Shapes application = new Shapes();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}