import javafx.scene.paint.Color; import javafx.scene.text.Font; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.stage.Stage; import static javafx.application.Application.launch; /** * Use this template to create drawings in FX. Change the name of the class and * put your own name as author below. Change the size of the canvas and the * window title where marked and add your drawing code where marked. * * @author Dave Slemon */ public class SimpleGraphics extends Application { /** * Start method (use this instead of main). * * @param stage The FX stage to draw on * @throws Exception */ @Override public void start(Stage stage) throws Exception { Group root = new Group(); Scene scene = new Scene(root); Canvas canvas = new Canvas(500, 400); // Set canvas Size in Pixels stage.setTitle("Simple Graphics"); // Set window title root.getChildren().add(canvas); stage.setScene(scene); GraphicsContext gc = canvas.getGraphicsContext2D(); // YOUR CODE STARTS HERE //text gc.setFont(new Font("Arial",40)); gc.setFill(Color.web("red")); gc.fillText("hi",100,120); //lines gc.setLineWidth(10); gc.setStroke(Color.BLACK); gc.strokeLine(10,10,80,80); //black rectangle (100-row, 200-col, 20-width, 50-height) gc.setLineWidth(2); gc.strokeRect(100,200,20,50); //blue rectangle gc.setFill(Color.web("blue")); gc.fillRect(210,210,20,30); //POLYGONS // use an array to hold the coordinates of each point(vertex) double[] cols = new double[5]; double[] rows = new double[5]; int numVertices = 5; cols[0] = 150 ; rows[0] = 90 ; cols[1] = 400 ; rows[1] = 90 ; cols[2] = 400 ; rows[2] = 190 ; cols[3] = 200 ; rows[3] = 190 ; cols[4] = 350 ; rows[4] = 160 ; gc.setFill(Color.web("#c5cfe3")); //lightblue hex value from color picker gc.fillPolygon(cols,rows, numVertices); // YOUR CODE STOPS HERE stage.show(); } /** * The actual main method that launches the app. * * @param args unused */ public static void main(String[] args) { launch(args); } }