Tino APCS

JavaFX Lab 1.1 Picture

Picture Template

Here is a template that draws a centered circle with an outline (stroke). Note that every imported class comes from the JavaFX library. You should not be using anything from java.awt.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class JavaFXPicture extends Application {

    // Define global constants
    private final double RADIUS = 100; // changing this scales everything

    // Colors: You should have imported and be using the
    // javafx.scene.paint.Color class, which is different from java.awt.Color.
    private final Color CIRCLE_COLOR = Color.CORNFLOWERBLUE;

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

    @Override
    public void start(Stage stage) throws Exception {

        // Change this to match your picture
        stage.setTitle("My JavaFX Octopus (or whatever you drew)");

        // Don't allow the user to resize the window
        stage.setResizable(false);

        // When you eventually show the window, shrinkwrap it to the size of your picture
        stage.sizeToScene();

        // In a Group, elements can be placed at positions of your choosing.
        // Otherwise, elements would automatically be positioned based on the
        // kind of parent container they are in.
        Group root = new Group();

        // Choose your own canvas size
        Scene scene = new Scene(root, 500, 500);
        stage.setScene(scene);

        // Draw your picture here using subclasses of 

        // Create a centered circle, as an example
        Circle c = new Circle(RADIUS);
        c.setCenterX(scene.getWidth() / 2);
        c.setCenterY(scene.getHeight() / 2);
        c.setFill(CIRCLE_COLOR);
        c.setStrokeWidth(3);
        c.setStroke(Color.BLACK);

        // In JavaFX, you don't add things directly to the scene.  Instead, you add them to a Node's
        // list of children to be rendered in that Node.  Here, we add the circle to our scene's
        // Root Node's list of children to be rendered.
        root.getChildren().add(c);

        // Finally, show the window
        stage.show();
    }

}

Directions

  • Draw a creative picture using JavaFX

    • You can draw a thing, an animal, a scene, some artwork, or whatever you'd like as long as it's thoughtful and has some detail.

    • Use at least 3 different subclasses of the JavaFX Shape API

    • Make your overall picture scalable by having a single constant at the top that controls the size of your picture. For example, if you have a picture of an octopus that uses a circle as its center, everything might be scaled according to BODY_RADIUS.

    • Set the size of the scene explicitly (as shown in the example above) and then use scene.getWidth() and scene.getHeight() to help position shapes rather than just hard-coding them at a particular location. The picture should be centered in the canvas, no matter what the canvas dimensions are.

  • Name your class PX_LastName_FirstName_JavaFX1 and submit it below.

You must Sign In to submit to this assignment

Last modified: March 02, 2023

Back to Graphics In Java

Dark Mode

Outline