This repository has been archived on 2023-04-04. You can view files and clone it, but cannot push or open issues or pull requests.
progra19/tut6/src/Squares.java
2019-11-29 19:27:56 +01:00

83 lines
1.8 KiB
Java

/**
* Program for drawing a square fractal.
*/
public class Squares
{
/**
* @param args Contains the parameters for the drawing. The first
* (mandatory) is the recursion level. The second (optional)
* is the base length of the first square. The default value
* for this length is 100 if it is not specified. Each
* parameter must be positive.
*/
public static void main(String args[])
{
int level;
int length = 100;
switch (args.length)
{
case 2:
length = Integer.parseInt(args[1]);
// fall-through
case 1:
level = Integer.parseInt(args[0]);
break;
default:
System.out.println(
"Es muessen zwischen 1 und 2 Parameter angegeben werden "
+ "(Level und eventuell Basislaenge)!"
);
return;
}
if (level < 1)
{
System.out.println("Das Rekursionslevel muss positiv sein!");
return;
}
if (length < 1)
{
System.out.println("Die Basislaenge muss positiv sein!");
return;
}
Canvas c = new Canvas();
Squares.paintFractal(c, level, length);
c.refresh();
}
/**
* Draws a square fractal with the specified recursion level and base length
* on the specified canvas.
*
* @param c The canvas to draw the tree on.
* @param level The current recursion level.
* @param length The current length.
*/
private static void paintFractal(Canvas c, int level, double length)
{
if (level <= 0)
return;
c.square(length);
// oben links
c.move(-length/2, -length/2);
paintFractal(c, level-1, length/2);
// oben rechts
c.move(length, 0);
paintFractal(c, level-1, length/2);
// unten rechts
c.move(0, length);
paintFractal(c, level-1, length/2);
// unten links
c.move(-length, 0);
paintFractal(c, level-1, length/2);
// zurueck zum mittelpunkt
c.move(length/2, -length/2);
}
}