Winter Scene with Java FX Mini-Lab
While this example is centered around a winter scene, you can use this idea for any season. Let your creativity guide you on this one..
In this lab, you will write program code to use the following topics:
- File input
- ImageView class
- JavaFX drawing
- Abstraction through writing methods with parameters
In this project we will be decorating our houses for winter (or whatever season / holiday you choose). It doesn't have to be your house, it can be any house.
You will want to use methods with parameters in this program. 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.
This Winter scene example, uses a drawSnowflake method with a loop to create 50 snowflakes at different locations and sizes. It uses Math.random() to help determine the locations and the size of the snowflakes.
Here is the code used to create this winter 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();
}
}
Now add your own methods. Some ideas: snowmen, sleds, mittens, lights, etc. Whatever they think a winter scene (or whatever season) should contain.