Results 1 to 3 of 3
  1. #1
    collwill is offline Member
    Join Date
    Mar 2011
    Posts
    28
    Rep Power
    0

    Post 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?

    Java 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();
    	}
    
    }

    Java 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);
    
    }
    
    }
    Last edited by collwill; 04-21-2011 at 07:05 AM. Reason: clarify issue.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your program works for me, and what's more, on first glance, it looks to be a well-crafted program.

    edit: there is some intermittent problems... I may have to look more tomorrow (unless someone figures it out tonight!).
    Last edited by Fubarable; 04-21-2011 at 07:40 AM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Help with actionPerformed
    By mayhewj7 in forum New To Java
    Replies: 8
    Last Post: 02-10-2009, 06:45 PM
  2. trouble with actionPerformed
    By diggitydoggz in forum New To Java
    Replies: 2
    Last Post: 12-26-2008, 02:18 AM
  3. Issue with Buttons and ActionPerformed
    By Deathmonger in forum Advanced Java
    Replies: 1
    Last Post: 04-17-2008, 08:47 AM
  4. Help with actionPerformed Statements
    By wco5002 in forum New To Java
    Replies: 8
    Last Post: 03-26-2008, 04:02 AM
  5. actionPerformed problem
    By tomitzel in forum New To Java
    Replies: 1
    Last Post: 01-08-2008, 06:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •