Results 1 to 1 of 1
  1. #1
    evapisces is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Question Shape drawing applet not working...

    Hi:
    I am pretty new to applets, and I am writing an applet that draws shapes. It allows the user to determine which shape (circle, oval, rectangle, square), how many shapes (1, 2, 4, 8, 16), and what color the shapes will appear as. I am using check boxes for the number of shapes so that the user can choose any combination of the numbers listed. I am using radio buttons for the shapes so the user can only pick one of the shapes listed. I am using a combo box for the colors.

    The program works fine initially: I pick the number of shapes, the shape, and the color. But when I try to change the shape, number, and/or color, the program does not clear the original configuration of shapes, it just draws right on top of the existing shapes.

    I could really use some help because I'm not sure how to fix this.

    Here's the code I have so far:

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class ShapeDrawing extends JApplet implements ItemListener
    {
        private int noOfShapes = 0;
    	 private int shape;
    	 private int color;
    	 private JLabel numLabel, shapeLabel, colorLabel;
    	 private JCheckBox oneCB, twoCB, fourCB, eightCB, sixteenCB;
    	 private JRadioButton circleRB, ovalRB, rectRB, squareRB;
    	 private ButtonGroup ShapeSelectGroup;
    	 private JComboBox colorSelectDD; 
    	 private String [] colors = {"Red", "Blue", "Green", "Yellow", "Pink", "Black", "Cyan", "Magenta"};
    
        public void init()
        {
    				Container c = getContentPane();
    				c.setLayout(null);
    				numLabel = new JLabel("# of Shapes");
    				oneCB = new JCheckBox("1");
    				twoCB = new JCheckBox("2");
    				fourCB = new JCheckBox("4");
    				eightCB = new JCheckBox("8");
    				sixteenCB = new JCheckBox("16");
    				shapeLabel = new JLabel("Shape");
    				circleRB = new JRadioButton("Cirle");
    				ovalRB = new JRadioButton("Oval");
    				rectRB = new JRadioButton("Rectangle");
    				squareRB = new JRadioButton("Square");
    				colorSelectDD = new JComboBox(colors);
    				colorSelectDD.setMaximumRowCount(3);
    				
    				numLabel.setSize(80, 30);
    				oneCB.setSize(80, 30);
    				twoCB.setSize(80, 30);
    				fourCB.setSize(80, 30);
    				eightCB.setSize(80, 30);
    				sixteenCB.setSize(80, 30);
    				shapeLabel.setSize(80, 30);
    				circleRB.setSize(80, 30);
    				ovalRB.setSize(80, 30);
    				rectRB.setSize(80, 30);
    				squareRB.setSize(80, 30);
    				colorSelectDD.setSize(80, 30);
    				
    				numLabel.setLocation(100, 270);
    				oneCB.setLocation(100, 310);
    				twoCB.setLocation(100, 350);
    				fourCB.setLocation(100, 390);
    				eightCB.setLocation(100, 430);
    				sixteenCB.setLocation(100, 470);
    				shapeLabel.setLocation(300, 270);
    				circleRB.setLocation(300, 320);
    				ovalRB.setLocation(300, 370);
    				rectRB.setLocation(300, 420);
    				squareRB.setLocation(300, 470);
    				colorSelectDD.setLocation(200, 270);
    				
    				oneCB.addItemListener(this);
    				twoCB.addItemListener(this);
    				fourCB.addItemListener(this);
    				eightCB.addItemListener(this);
    				sixteenCB.addItemListener(this);
    
    				circleRB.addItemListener(this);
    				ovalRB.addItemListener(this);
    				rectRB.addItemListener(this);
    				squareRB.addItemListener(this);
    				colorSelectDD.addItemListener(this);
    				
    				c.add(numLabel);
    				c.add(oneCB);
    				c.add(twoCB);
    				c.add(fourCB);
    				c.add(eightCB);
    				c.add(sixteenCB);
    				c.add(shapeLabel);
    				c.add(circleRB);
    				c.add(ovalRB);
    				c.add(rectRB);
    				c.add(squareRB);
    				c.add(colorSelectDD);
    				ShapeSelectGroup = new ButtonGroup();
    				ShapeSelectGroup.add(circleRB);
    				ShapeSelectGroup.add(ovalRB);
    				ShapeSelectGroup.add(rectRB);
    				ShapeSelectGroup.add(squareRB);
        }
    
        public void paint(Graphics g)
        {
       		switch (color)
    			{
    			case 0: g.setColor(Color.red);
    					  break;
    			case 1: g.setColor(Color.blue);
    			  		  break;
    			case 2: g.setColor(Color.green);
    					  break;
    			case 3: g.setColor(Color.yellow);
    					  break;
    			case 4: g.setColor(Color.pink);
    					  break;
    			case 5: g.setColor(Color.black);
    					  break;
    			case 6: g.setColor(Color.cyan);
    					  break;
    			case 7: g.setColor(Color.magenta);
    					  break;
    			}
    			
    			switch (shape)
    			{
    			case 0: for (int i = 0; i < noOfShapes; i++)							// Drawing circles
           			  {
           		         g.drawOval(10 + i * 4, 10 + i * 4,
           		                    30 + i * 2, 30 + i * 2);
    					  }
    					  break;
    			case 1: for (int i = 0; i < noOfShapes; i++)							// Drawing ovals
           			  {
           				   g.drawOval(10 + i * 4, 10 + i * 4,
           		                    30 + i * 2, 10 + i * 2);
    					  }
    					  break;
    			case 2: for (int i = 0; i < noOfShapes; i++)							// Drawing rectangles
           			  {
           				   g.drawRect(10 + i * 4, 10 + i * 4,
           		                    30 + i * 2, 10 + i * 2);
    					  }
    					  break;
    			case 3: for (int i = 0; i < noOfShapes; i++)							// Drawing squares
           			  {
           				   g.drawRect(10 + i * 4, 10 + i * 4,
           		           			  30 + i * 2, 30 + i * 2);
    					  }
    					  break;
    			}
       }
    	
    	public void itemStateChanged(ItemEvent e)
    	{
    	    if (e.getSource() == oneCB)
    		 {
    		     if (e.getStateChange() == ItemEvent.SELECTED)
    			      noOfShapes = noOfShapes + 1;
    			  if (e.getStateChange() == ItemEvent.DESELECTED)
    			      noOfShapes = noOfShapes - 1;
    		 }
    		 
    		 if (e.getSource() == twoCB)
    		 {
    		     if (e.getStateChange() == ItemEvent.SELECTED)
    			      noOfShapes = noOfShapes + 2;
    			  if (e.getStateChange() == ItemEvent.DESELECTED)
    			      noOfShapes = noOfShapes - 2;
    		 }
    		 
    		 if (e.getSource() == fourCB)
    		 {
    		     if (e.getStateChange() == ItemEvent.SELECTED)
    			      noOfShapes = noOfShapes + 4;
    			  if (e.getStateChange() == ItemEvent.DESELECTED)
    			      noOfShapes = noOfShapes - 4;
    		 }
    		 
    		 if (e.getSource() == eightCB)
    		 {
    		     if (e.getStateChange() == ItemEvent.SELECTED)
    			      noOfShapes = noOfShapes + 8;
    			  if (e.getStateChange() == ItemEvent.DESELECTED)
    			      noOfShapes = noOfShapes - 8;
    		 }
    		 
    		 if (e.getSource() == sixteenCB)
    		 {
    		     if (e.getStateChange() == ItemEvent.SELECTED)
    			      noOfShapes = noOfShapes + 16;
    			  if (e.getStateChange() == ItemEvent.DESELECTED)
    			      noOfShapes = noOfShapes - 16;
    		 }
    		 
    		 if (e.getSource() == circleRB)
    		     shape = 0;
    		 else if (e.getSource() == ovalRB)
    		     shape = 1;
    		 else if (e.getSource() == rectRB)
    		     shape = 2;
    		 else if (e.getSource() == squareRB)
    		     shape = 3;
    			  
    		 if (e.getSource() == colorSelectDD)
    		     color = colorSelectDD.getSelectedIndex();
    			  
    		 repaint();
    	}
    }
    Any help is greatly appreciated. Please let me know if you need more info from me.
    Thanks in advance
    Last edited by evapisces; 11-18-2008 at 12:50 AM.

Similar Threads

  1. Help with Move Shape
    By romina in forum AWT / Swing
    Replies: 2
    Last Post: 12-09-2010, 04:25 AM
  2. Replies: 8
    Last Post: 05-28-2008, 08:00 AM
  3. implementing shape
    By sidkdbl07 in forum Java 2D
    Replies: 1
    Last Post: 01-12-2008, 07:42 PM
  4. Applet button not working
    By letmeoutofhere in forum Java Applets
    Replies: 9
    Last Post: 11-14-2007, 01:15 PM
  5. weird looking shape JButton is it possible?
    By unhurt in forum AWT / Swing
    Replies: 8
    Last Post: 11-03-2007, 10:10 AM

Posting Permissions

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