applet loops

i got the following code:

import java.awt.*;

import javax.swing.*;

publicclass Shapesextends JApplet

{

privateint shapeSelect;

privateint cont;

publicvoid init()

{

shapeSelect = Integer.parseInt(JOptionPane.showInputDialog(" Enter 1 to draw lines "

+" \n Enter 2 to draw rectangles "

+" \n Enter 3 to draw ovals "

+" \n Enter 4 to draw triangles"));

}

publicvoid paint(Graphics shape)

{

int x1 = 0;

int y1 = 0;

int x2 = 10;

int y2 = 10;

int x3 = 40;

int y3 = 10;

int[] xCoordinates ={0, 175, 175};

int[] yCoordinates ={20, 0, 20};

for(int index = 0; index <= 100; index++)

{

switch(shapeSelect)

{

case 1:

shape.drawLine(x1,y1,x2,y2);

x1 = x1 + 5;

y1 = y1 + 10;

x2 = x2 + 15;

y2 = y2 + 20;

break;

case 2:

shape.drawRect(x1,y1,x3,y3);

x1 = x1 + 5;

y1 = y1 + 5;

x3 = x3 + 10;

y3 = y3 + 10;

break;

case 3:

shape.drawOval(x1,y1,x3,y3);

x1 = x1 + 5;

y1 = y1 + 5;

x3 = x3 + 10;

y3 = y3 + 10;

break;

case 4:

shape.drawPolygon (xCoordinates,yCoordinates,xCoordinates.length);

break;

default:

Font errorMessage =new Font("SansSerif",Font.BOLD + Font.ITALIC,38);

shape.setFont(errorMessage);

shape.setColor(Color.blue);

shape.drawString("ERROR: Incorrect Entry!",38,200);

}

}

}

}

Now you see in case 4, am drawing a triangle how do i loop its like the other so it produces multiple triangles in different locations?

Message was edited by:

The_One

[3372 byte] By [The_Onea] at [2007-11-26 15:10:37]
# 1

Within case 4, after you draw the polygon, change the values in the array:

for (int i = 0; i < xCoordinates.length; i++)

{

xCoordinates[i] = xCoordinates[i] + 5;

yCoordinates[i] = yCoordinates[i] + 10;

}

or:

xCoordinates[0] = xCoordinates[0] + 5;

yCoordinates[0] = yCoordinates[0] + 10;

// and same for indexes 1 and 2

// (using different increments instead of 5 and 10, if desired)

doremifasollatidoa at 2007-7-8 9:01:28 > top of Java-index,Java Essentials,Java Programming...
# 2

> you see in case 4, am drawing a triangle how do i

> loop its like the other so it produces multiple

> triangles in different locations?

Just change the variables that define where it's drawn, just like you did with the other shapes. In this case, it's xCoordinates and yCoordinates that you change.

xCoordinates[0] += 5;

xCoordinates[1] += 10;

xCoordinates[2] += 15;

yCoordinates[0] += 5;

...

hunter9000a at 2007-7-8 9:01:28 > top of Java-index,Java Essentials,Java Programming...
# 3
ok thanks
The_Onea at 2007-7-8 9:01:28 > top of Java-index,Java Essentials,Java Programming...
# 4

jus a question. can u explain to be wat each of those lines:

xCoordinates[0] += 5;

xCoordinates[1] += 10;

xCoordinates[2] += 15;

yCoordinates[0] += 5;

mean. like wat happens to the pic. cos am trying to display it like the others ones but it goes all funny and i have no idea how its drawn it?

The_Onea at 2007-7-8 9:01:28 > top of Java-index,Java Essentials,Java Programming...
# 5

xCoordinates[0] += 5;

That means that the first element of the xCoordinates array will be increased by 5. You can add or subtract whatever amount to want to put it where you want, I just made up those numbers. If you just want to move the triangle down and to the right by 5 pixels for instance, just add 5 to each element of the x and y coords.

hunter9000a at 2007-7-8 9:01:28 > top of Java-index,Java Essentials,Java Programming...