import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.control.*; import javafx.scene.input.MouseEvent; import javafx.scene.input.MouseButton; import javafx.scene.paint.Color; import javafx.scene.text.Font; import javafx.event.ActionEvent; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.stage.Stage; /** * Use this template to create Apps with Graphical User Interfaces. * * @author YOUR NAME */ public class TestMouse extends Application { // TODO: Instance Variables for View Components and Model private GraphicsContext gc; // TODO: Private Event Handlers and Helper Methods public void mousePressed(MouseEvent me) { //System.out.println("pressed: " + me.getButton() + " " + me.getX() + " " + me.getY()); Circle circle = new Circle(20,me.getX(), me.getY()); if (me.getButton() == MouseButton.PRIMARY) circle.setColor(Color.ORANGE,Color.YELLOW); else circle.setColor(Color.BROWN,Color.RED); circle.draw(gc); } public void mouseDragged(MouseEvent me) { //System.out.println("dragged: " + me.getButton() + " " + me.getX() + " " + me.getY()); Circle circle = new Circle(20,me.getX(), me.getY()); circle.setColor(Color.ORANGE,Color.YELLOW); circle.draw(gc); } public void mouseReleased(MouseEvent me) { //System.out.println("released: " + me.getButton() + " " + me.getX() + " " + me.getY()); gc.setFill(Color.LIGHTYELLOW); gc.fillRect(0,0,400,225); Circle circle = new Circle(20,me.getX(), me.getY()); circle.setColor(Color.ORANGE,Color.YELLOW); circle.draw(gc); } /** * This is where you create your components and the model and add event * handlers. * * @param stage The main stage * @throws Exception */ @Override public void start(Stage stage) throws Exception { Pane root = new Pane(); Scene scene = new Scene(root, 400, 225); // set the size here stage.setTitle("The Mouse"); // set the window title here stage.setScene(scene); // TODO: Add your GUI-building code here // 1. Create the model // 2. Create the GUI components Canvas c = new Canvas(400,225); gc = c.getGraphicsContext2D(); gc.setFill(Color.LIGHTYELLOW); gc.fillRect(0,0,400,225); // 3. Add components to the root root.getChildren().addAll(c); // 4. Configure the components (colors, fonts, size, location) // 5. Add Event Handlers and do final setup c.addEventHandler(MouseEvent.MOUSE_PRESSED,this::mousePressed); c.addEventHandler(MouseEvent.MOUSE_RELEASED,this::mouseReleased); c.addEventHandler(MouseEvent.MOUSE_DRAGGED,this::mouseDragged); // 6. Show the stage stage.show(); } /** * Make no changes here. * * @param args unused */ public static void main(String[] args) { launch(args); } }