Twas the Week Before Break...Fun JavaFX Project to End the Year

Twas the Week Before Break...Fun JavaFX Project to End the Year

‘Twas the week before winter break when all through the school, the CS teachers wondered what interesting things they could do. The projects and exams were all set for the year, in the hopes of something fun to finish the year.

JavaFX to the rescue a fun creative tool. Allow students to explore and learn something new…

Ok, a poet, I may not be, but I have some ideas for how to wrap up the year in a fun way…

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 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.

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.

Hello Winter and Happy New Year!

Crystal

#LearnJava #TeachJava #JavaEducators

?

要查看或添加评论,请登录

Crystal Sheldon的更多文章

社区洞察

其他会员也浏览了