developing graphical objects

I am trying to create an applet that displays a Dialog box giving the user a choice of shapes to draw and in the applets paint() method i need to have a for loop that loops at least 100 times. Inside the loop i need a switch statement that draws the line rectangle oval or polygon depending on the users choice. With each repeat of the loop, the shape drawn should be positioned in a slightly different place to create an interesting pattern. If an invalid number is entered, the default case should use the drawString() method to write an error message.

i do not understand how to repeat the object 100x to create the pattern, below is how far i have made it so far.

import javax.swing.JOptionPane;

import java.awt.*;

import javax.swing.*;

/**

* create a shape then have it repeat 100x.

*

* @author (Simon Orazi)

* @version (26/05/07)

*/

publicclass Shapesextends JApplet

{

String input;

int choice;

publicvoid init()

{

do

{

input = JOptionPane.showInputDialog("Enter 1 to draw lines\n" +"Enter 2 to draw rectangles\n" +

"Enter 3 to draw ovals\n" +"Enter 4 to draw polygons");

choice = Integer.parseInt(input);

publicvoid paint(Graphics g)

{

switch (choice)

{

case 1:

if (choice == 1);

drawLine(g);

break;

case 2:

if (choice == 2);

drawRectangle(g);

break;

case 3:

if (choice == 3);

drawOval(g);

break;

case 4:

if (choice == 4);

drawPolygon(g);

break;

default:

}g.drawString("Please enter 1, 2, 3 or 4", 20, 20);

}while (choice > 4);

}

publicvoid drawLine(Graphics g)

{

g.drawLine(20, 20, 20, 80);

for(choice=100; choice<100; choice++);

}

publicvoid drawRectangle(Graphics g)

{

g.drawRect(20, 20, 80, 80);

for(choice=100; choice<100; choice++);

}

publicvoid drawOval(Graphics g)

{

g.drawOval(20, 20, 12, 75);

for(choice=100; choice<100; choice++);

}

publicvoid drawPolygon(Graphics g)

{

int[] xCoords ={60, 100, 140, 140,

100, 60, 20, 20};

int[] yCoords ={20, 20, 60, 100,

140, 140, 100, 60};

g.drawPolygon(xCoords, yCoords, 8);

for(choice=100; choice<100; choice++);

}

}

[4491 byte] By [sorazia] at [2007-11-27 5:34:51]
# 1

// <applet code="ShapesRx" width="400" height="400"></applet>

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import javax.swing.*;

public class ShapesRx extends JApplet implements ActionListener

{

ShapesPanel shapesPanel = new ShapesPanel();

public void init()

{

getContentPane().setLayout(new BorderLayout());

getContentPane().add(shapesPanel);

getContentPane().add(getUIPanel(), "Last");

}

public void actionPerformed(ActionEvent e)

{

int choice = Integer.parseInt(e.getActionCommand());

shapesPanel.setChoice(choice);

}

private JPanel getUIPanel()

{

JPanel panel = new JPanel();

String[] ids = { "line", "rectangle", "oval", "polygon" };

for(int j = 0; j < ids.length; j++) {

JButton button = new JButton(ids[j]);

button.setActionCommand(String.valueOf(j+1));

button.addActionListener(this);

panel.add(button);

}

return panel;

}

}

class ShapesPanel extends JPanel

{

int choice = -1;

public void setChoice(int n)

{

choice = n;

repaint();

}

protected void paintComponent(Graphics g)

{

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

switch (choice)

{

case 1:

draw(new Line2D.Double(20, 20, 20, 80), g2);

break;

case 2:

draw(new Rectangle2D.Double(20, 20, 80, 80), g2);

break;

case 3:

draw(new Ellipse2D.Double(20, 20, 12, 75), g2);

break;

case 4:

int[] xCoords = {60, 100, 140, 140,

100, 60, 20, 20};

int[] yCoords = {20, 20, 60, 100,

140, 140, 100, 60};

draw(new Polygon(xCoords, yCoords, 8), g2);

break;

default:

g.drawString("Please enter 1, 2, 3 or 4", 20, 20);

}

}

private void draw(Shape shape, Graphics2D g2)

{

double theta = 0;

int steps = 100;

double thetaInc = Math.toRadians(360.0/steps);

Rectangle r = shape.getBounds();

double x = r.getCenterX() + r.width/4;

double y = r.getCenterY() + r.height/2;

double cx = getWidth()/2 - x;

double cy = getHeight()/2 - y;

for(int j = 0; j < steps; j++) {

AffineTransform at = AffineTransform.getTranslateInstance(cx, cy);

at.rotate(theta, x, y);

g2.draw(at.createTransformedShape(shape));

theta += thetaInc;

}

}

}

crwooda at 2007-7-12 15:03:13 > top of Java-index,Java Essentials,New To Java...