public boolean IsTooLow (GP.Attributes.Coordinate pos)
{
return (pos.GetY() > 400);
}
public GP.Attributes.Color ChooseColor (int number)
{
if (number % 2 == 0)
{
return new GP.Attributes.Colors.Blue();
}
else
{
return new GP.Attributes.Colors.Black();
}
}
![]() |
Make a class that represents the curve shown by drawing a given number of lines that stretch from one side of the applet to the other. For example, the image on the left was created with twenty lines each twenty pixels apart, such that the first line is positioned from (0, 20) to (380, 0), then from (0, 40) to (360, 0), and on and on, until finally (0, 400) to (0, 0).If the curve has only four lines then the lines created be positioned as follows: (0, 100) to (300, 0), then (0, 200) to (200, 0), then (0, 300) to (100, 0), and finally (0, 400) to (0, 0). |
public class Curve
{
public Curve (int resolution)
{
double END = 400.0;
double change = END / resolution;
int k = 0;
while (k <= END)
{
MakeLine(0, k, (END - k), 0);
k = k + change;
}
}
public void MakeLine (double startx, double starty, double endx, double endy)
{
GP.Shapes.Line tmp = new GP.Shapes.Line();
tmp.SetEndPoints(new GP.Attributes.Coordinate(startx, starty),
new GP.Attributes.Coordinate(endx, endy));
tmp.SetColor(new GP.Attributes.Colors.Black());
tmp.SetThickness(2);
}
}
![]() |
Make a class that represents a stair case built from squares with a given number of steps. For example, the image on the left was created with ten steps, meaning that each square is 40x40 pixels in size and there are ten levels each with one more square than the previous level. If the number given was two, then the size of each square would be 200x200 pixels and there would be only two levels: one with one square and the other with two squares. |
public class Steps
{
public Steps (int numSteps)
{
double size = 400.0 / numSteps;
int k = 1;
while (k <= numSteps)
{
MakeStep(k, size);
k = k + 1;
}
}
public void MakeStep (int limit, double size)
{
double k = 0.5;
while (k < limit)
{
MakeSquare(size * k, size * limit, size);
k = k + 1;
}
}
public void MakeSquare (double cx, double cy, double size)
{
GP.Shape tmp = new GP.Shapes.Rectangle();
tmp.SetPosition(new GP.Attributes.Coordinate(cx, cy));
tmp.SetSize(new GP.Attributes.Dimension(size, size));
}
}
![]() |
Make a class that represents a pyramid, with a given number of levels, like the one shown on the left with seven levels. Each level should be composed of twice as many triangles, each half as large as those in the previous level and positioned at either end of the larger triangle. For example, if the pyramid has three levels: the first level should have one circle of full size; the next should have two triangles, each half the full size; and the last level should have four triangles, each one-quarter the full size. The code given below is to help get you started. |
public class Pyramid
{
public Pyramid (int numLevels, double size)
{
MakePyramid(200, 150, size, numLevels);
}
public void MakePyramid (double cx, double cy, double size, int level)
{
if (level > 0)
{
MakeTriangle(cx, cy, size);
MakePyramid(cx - size / 2, cy + 2 * size / 3, size / 2, level - 1);
MakePyramid(cx + size / 2, cy + 2 * size / 3, size / 2, level - 1);
}
else
{
// do nothing
}
}
public void MakeTriangle (double cx, double cy, double size)
{
GP.Shapes.Triangle tmp = new GP.Shapes.Triangle();
tmp.SetPosition(new GP.Attributes.Coordinate(cx, cy));
tmp.SetSize(new GP.Attributes.Dimension(size));
}
}
public class MoveBehavior extends GP.Behaviors.Perpetual
{
GP.Shape myTarget;
public MoveBehavior (GP.Shapes.Oval target)
{
myTarget = target;
}
public void Step ()
{
myTarget.Move(-1, 0);
}
}
public class MoveBehavior extends GP.Behaviors.Conditional
{
GP.Shape myTarget;
public MoveBehavior (GP.Shapes.Oval target)
{
myTarget = target;
}
public boolean IsDone ()
{
return (myTarget.GetPosition().GetX() > 400);
}
public void Step ()
{
myTarget.Move(1, 0);
}
}