Results 1 to 1 of 1
- 11-17-2008, 11:46 PM #1
Member
- Join Date
- Aug 2008
- Posts
- 4
- Rep Power
- 0
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:
Any help is greatly appreciated. Please let me know if you need more info from me.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(); } }
Thanks in advanceLast edited by evapisces; 11-17-2008 at 11:50 PM.
Similar Threads
-
Help with Move Shape
By romina in forum AWT / SwingReplies: 2Last Post: 12-09-2010, 03:25 AM -
Java mail problem(working in intranet,but not working in iternet)
By sundarjothi in forum Advanced JavaReplies: 8Last Post: 05-28-2008, 07:00 AM -
implementing shape
By sidkdbl07 in forum Java 2DReplies: 1Last Post: 01-12-2008, 06:42 PM -
Applet button not working
By letmeoutofhere in forum Java AppletsReplies: 9Last Post: 11-14-2007, 12:15 PM -
weird looking shape JButton is it possible?
By unhurt in forum AWT / SwingReplies: 8Last Post: 11-03-2007, 09:10 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks