Introduction
Create a beautiful landscape of snowflakes using Java Greenfoot. This is great fun and a great way to start learning how to code with Java.
What you will make
Digital snowflakes out of code, like this one:
What you will learn
By making snowflakes with code you will learn how to:
- Draw lines and make turns with Java Greenfoot
- Change the pen color randomly
- Use loops to repeat some instructions and create shapes
- Use more loops to create spiral patterns
- Create a function to draw a snowflake
Starter Project
private void moveAndDraw(int distance)
{
final int X_VECTOR = (int)(getX() + distance * Math.cos(Math.toRadians(getRotation())));
final int Y_VECTOR = (int)(getY() + distance * Math.sin(Math.toRadians(getRotation())));
getWorld().getBackground().drawLine(getX(), getY(), X_VECTOR, Y_VECTOR);
move(distance);
}
private void setColor(int r, int g, int b)
{
getWorld().getBackground().setColor(new Color(r, g, b));
}
/**
* Replace the act code in your program with this one.
*/
public void act()
{
int r = 1;
int g = 100;
int b = 5;
int[][] colors = { {85, 211, 136}, {197, 196, 126}, {235, 233, 166}, {25, 135, 222}, {211, 64, 159}, {159, 165, 106}, {178, 160, 125}, {36, 192, 70}, {231, 184, 204}, {63, 203, 219} };
for (int i = 0; i < 4; i++)
{
setColor(colors[i][0], colors[i][1], colors[i][2]);
moveAndDraw(50);
turn(90);
}
}