/************************************************** * Author: Morrison * Date: 16 Mar 202021 **************************************************/ import javafx.application.Application; import javafx.application.Platform; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.layout.BorderPane; import javafx.scene.canvas.GraphicsContext; import javafx.scene.canvas.Canvas; import javafx.scene.paint.Color; import javafx.scene.control.Menu; import javafx.scene.control.MenuBar; import javafx.scene.control.MenuItem; import java.util.ArrayList; public class Squeak extends Application { private ArrayList shapes; private Color currentColor; private ShapeType currentShape; private double currentSize; public Squeak() { shapes = new ArrayList<>(); currentColor = Color.BLUE; currentShape = ShapeType.BALL; currentSize = 50; } @Override public void init() { } @Override public void start(Stage primary) { primary.setTitle("Mouse Demonstration"); BorderPane bp = new BorderPane(); primary.setScene(new Scene(bp)); Canvas canvas = new Canvas(700, 500); GraphicsContext pen = canvas.getGraphicsContext2D(); //Puzzler: turn canvas red //pen.setFill(Color.RED); //pen.fillRect(0, 0, canvas.getWidth(), canvas.getHeight()); /* puzzler #2 * when the mouse enters the canvas, turn it red. * when it exits turn it white. */ canvas.setOnMouseEntered( e -> { pen.setFill(Color.RED); pen.fillRect(0, 0, canvas.getWidth(), canvas.getHeight()); redrawShapes(pen); }); canvas.setOnMouseExited( e -> { pen.setFill(Color.WHITE); pen.fillRect(0, 0, canvas.getWidth(), canvas.getHeight()); redrawShapes(pen); }); /* * puzzler #3: * When the user click on the canvas, have a blue ball of * radius 50 pixels appear centered at the click point. */ canvas.setOnMouseClicked( e -> { Drawable b = null; if( currentShape == ShapeType.BALL) { //TODO: when mouse is clicked set size correctly` b = new Ball(e.getX(), e.getY(), currentSize, currentColor); } else if( currentShape == ShapeType.SQUARE) { b = new Square(e.getX(), e.getY(), currentSize, currentColor); } shapes.add(b); b.draw(pen); }); bp.setCenter(canvas); bp.setTop(buildMenus()); primary.show(); } private MenuBar buildMenus() { MenuBar mbar = new MenuBar(); Menu colorMenu = new Menu("Color"); Menu shapeMenu = new Menu("Shape"); shapeMenu.getItems().add(new ShapeMenuItem(ShapeType.BALL)); shapeMenu.getItems().add(new ShapeMenuItem(ShapeType.SQUARE)); mbar.getMenus().addAll(colorMenu, shapeMenu); ColorMenuItem redItem = new ColorMenuItem(Color.RED, "RED"); ColorMenuItem blueItem = new ColorMenuItem(Color.BLUE, "BLUE"); ColorMenuItem greenItem = new ColorMenuItem(Color.GREEN, "GREEN"); ColorMenuItem orangeItem = new ColorMenuItem(Color.ORANGE, "ORANGE"); ColorMenuItem dookItem = new ColorMenuItem(Color.web("#001A57"), "DOOK"); colorMenu.getItems().addAll(redItem, greenItem, blueItem, orangeItem, dookItem); return mbar; } private void redrawShapes(GraphicsContext pen) { for(Drawable b: shapes) { b.draw(pen); } } @Override public void stop() { } class ColorMenuItem extends MenuItem { public ColorMenuItem(Color color, String colorName) { super(colorName); setOnAction( e -> { currentColor = color; }); } } class ShapeMenuItem extends MenuItem { public ShapeMenuItem(ShapeType s) { super(s.toString()); setOnAction( e -> { currentShape = s; }); } } enum ShapeType { SQUARE, BALL } }