Skip Top Navigation Bar

Winter Scene with Java FX Mini-Lab

When I was teaching Intro to CS, my students would do a project where they decorated a house for Fall. It didn’t have to be their house, but it could be. At the time, we used Java Applets, but for this project, I suggest using JavaFX.

This project can be completed at any time of the year and might make a fun post-AP CSA exam project. It is fun and creative, but most importantly, it motivated students to write methods with parameters. See, if you wanted to draw one pumpkin, you’d probably want to draw a few pumpkins. By including parameters, you can “stamp” out several pumpkins in different locations and different sizes. Maybe even different colors depending on your parameter list.

In my Winter scene, I created a drawSnowflake method and then used a loop to create 50 snowflakes at different locations and sizes. I used Math.random() to help determine the locations and the size of the snowflakes.

If you are looking at this as a post-AP CSA project, maybe a scene related to summer with summer flowers or a beach scene with beach balls, blankets, and umbrellas. The ideas are endless, so have fun with it and let the creativity flow.

I used IntelliJ to create my scene. Here is the code I used to create my scene:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;



import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;


public class WinterApplication extends Application {

   /**
    * draws a snow flake with a size of size and the middle line starting at x, y 
    */
    public void drawSnowflake (int x, int y, int size, Group g) {
        Line horizontal = new Line (x, y, x + size, y);
        horizontal.setStroke(Color.WHITE);
        horizontal.setStrokeWidth(3);
        Line vertical = new Line (x + size / 2, y - size / 2, x + size / 2, y + size / 2 );
        vertical.setStroke(Color.WHITE);
        vertical.setStrokeWidth(3);
        Line diagonalLeft = 
             new Line ( x + size / 4, y - size / 2, x + 3 * size / 4 , y + (size /2));
        diagonalLeft.setStroke(Color.WHITE);
        diagonalLeft.setStrokeWidth(3);
        Line diagonalRight = 
             new Line (x + size / 4, y + size / 2, x +  3 * size / 4, y - (size / 2));
        diagonalRight.setStroke(Color.WHITE);
        diagonalRight.setStrokeWidth(3);
        g.getChildren().addAll(horizontal);
        g.getChildren().addAll(vertical);
        g.getChildren().addAll(diagonalLeft);
        g.getChildren().addAll(diagonalRight);

    }


    @Override
    public void start(Stage stage) throws IOException {
        stage.setTitle("Winter Scene");

        InputStream stream = new FileInputStream(//your path to a photo);
        Image image = new Image(stream);

        ImageView imageView = new ImageView();
        imageView.setImage(image);
        imageView.setX(10);
        imageView.setY(10);
        imageView.setFitWidth(575);
        imageView.setPreserveRatio(true);

        Group group = new Group();
        group.getChildren().addAll(imageView);

        for (int count = 1; count <= 50; count++)
        {
            int x = (int)(Math.random() * 515) + 1;
            int y = (int) (Math.random() * 400) + 1;
            int size = (int) (Math.random() * 30) + 1;
            drawSnowflake(x, y, size, group);
        }
        Scene scene = new Scene(group, 550, 300);

        stage.setScene (scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

Have your students create their own methods for snowmen, sleds, mittens, lights, etc. Whatever they think a winter scene should contain. This project works great at any season. So, whenever you are ready for students to write methods and find real reasons to leverage parameters, this project is a fun one to use.