# loops in java to produce shapes

cheryl joy
Greenhorn
Posts: 2
Hi

just need help on my assignment, im trying to draw pattern onto the canvas so that the whole canvas is covered with that pattern. At the moment ive drawn a polygon shape and it only draws it in the x axis horizontally across the canvas but not vertically!!! below is a snipet of my code
many thanks

if(patterntype == "PM")

g.setColor(new Color(255,0,0));

int xPoints[] = { 20, 40, 50, 30, 20, 15 };
int yPoints[] = { 50, 50, 60, 80, 80, 60 };
int numPoints = xPoints.length;

Polygon polygon = new Polygon( xPoints, yPoints, numPoints);

g.fillPolygon(polygon);

} // end of paint
if(!tiled)
{
for(int x=0;x<xPoints.length;x++)

for(int y=0;y<xPoints.length;y++)
{
g.drawPolygon(xPoints, yPoints, xPoints.length);

for(int i=0; i<xPoints.length;i++)
xPoints[i] += 90;

g.setColor(new Color(255,0,0));
g.drawPolygon(xPoints, yPoints, xPoints.length);

}
}

Stephen Huey
Ranch Hand
Posts: 618
In general, it might make your code cleaner to have curly braces after every for loop (e.g. your for(int x=0;x<xPoints.length;x++) doesn't have any curly braces, and while that doesn't seem to mess up your code, it does make it harder to read). Anyway, take a look at this line:

Maybe this code block is part of the problem...you seem to be iterating over your xPoints here and incrementing each element in the array by 90, but I see no corresponding bit of code like this for yPoints...

Oh--actually, look above that. You have 2 for loops at the top...one is x < xPoints.length and the other is y < xPoints.length -- is that supposed to be y < yPoints.length instead?

cheryl joy
Greenhorn
Posts: 2
hi thanks for the reply, with your advice i have change some of the codes, but i still can not tile/display the polygon over the whole canvas its it either vertically or horizontally or different places not both!!

//////////////////
// do tiling ///

if(tiled)
{
for(int x=0;x<xPoints.length;x++)
{
for(int y=0;y<yPoints.length;y++)
{

for(int i=0; i<xPoints.length;i++)
xPoints[i] +=100;
{
g.drawPolygon(xPoints, yPoints, xPoints.length);
{

for(int i=0; i<yPoints.length;i++)
yPoints[i] +=100;

g.drawPolygon(xPoints, yPoints, xPoints.length);
g.setColor(new Color(255,0,0));

}

}

MANY THANKS
CHERYL

Ryan McGuire
Ranch Hand
Posts: 1085
4
You have three nested loops, right? The two outer ones, for x and y, iterate through the different areas where you want to draw the shape. The inner one, for i, increments the values in xPoints.

The problem is that there is nothing to increase the yPoints values. Instead, what happens is that the xPoints values keep getting incremented and never decremented. It's like a printer that doesn't do Carriage Return/Line Feed. What you need is some way to shift down one line and back to the left side. How about this: at the end of the for(y) loop, put another for(i) loop that decrements the xPoints values back to their original values and adds a little to each yPoints element.

That's the change that would require the least work, but it's still ugly. A better solution would be to leave the untranslated shape points in xPoints and yPoints, and use a couple temporary copy arrays to draw the translated shapes.

The other thing that might be an issue is the limits you're using for your x and y loops. xPoints.length is a constant 6, so you're going to get 36 shapes regardless of the size of your Graphics. Even if the Graphics is 2000x2000 pixels, you'll get 36 shapes in the 20<x<1130, 50<y<1160 area. If you want to fill the Graphics, use getHeight and getWidth on the parent component.
[ August 17, 2005: Message edited by: Ryan McGuire ]

cj joy
Greenhorn
Posts: 1
hi its me again,

just wondering if it is possible to g.translate( ) it down one line and then loop again??

thanks