Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Good day, How can I let the shape follow my mouse cursor when I move it? This is the beginning of a paint program, the
Good day,
How can I let the shape follow my mouse cursor when I move it? This is the beginning of a paint program, the code is here below. Works fine to draw the shape but I would like the i.e. line show when I am drawing
import javafx.application.Application; import javafx.scene.control.*; import javafx.scene.layout.HBox; import javafx.stage.Stage; import java.awt.image.RenderedImage; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Stack; import javafx.application.Application; import javafx.embed.swing.SwingFXUtils; import javafx.geometry.Insets; import javafx.scene.Cursor; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.image.Image; import javafx.scene.image.WritableImage; import javafx.scene.layout.BorderPane; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.shape.Ellipse; import javafx.scene.shape.Line; import javafx.scene.shape.Rectangle; import javafx.scene.shape.Shape; import javafx.scene.text.Font; import javafx.stage.FileChooser; import javafx.stage.Stage; import javax.imageio.ImageIO; public class TinyPainter extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { Menu menu = new Menu("Choose shape"); RadioMenuItem linebtn = new RadioMenuItem("Line"); RadioMenuItem rectbtn = new RadioMenuItem("Rectangle"); RadioMenuItem circlebtn = new RadioMenuItem("Circle"); RadioMenuItem dotbtn = new RadioMenuItem("Dot"); ToggleGroup toggleGroup = new ToggleGroup(); toggleGroup.getToggles().add(linebtn); toggleGroup.getToggles().add(rectbtn); toggleGroup.getToggles().add(circlebtn); toggleGroup.getToggles().add(dotbtn); menu.getItems().add(linebtn); menu.getItems().add(rectbtn); menu.getItems().add(circlebtn); menu.getItems().add(dotbtn); MenuBar menuBar = new MenuBar(); menuBar.getMenus().add(menu); ColorPicker cpLine = new ColorPicker(Color.BLACK); HBox btns = new HBox(10); btns.getChildren().addAll(menuBar, cpLine); btns.setPadding(new Insets(5)); btns.setStyle("-fx-background-color: #999"); Canvas canvas = new Canvas(1080, 790); GraphicsContext gc; gc = canvas.getGraphicsContext2D(); gc.setLineWidth(1); Line line = new Line(); Rectangle rect = new Rectangle(); Circle circ = new Circle(); Circle dot = new Circle(); canvas.setOnMousePressed(e->{ if(linebtn.isSelected()) { gc.setStroke(cpLine.getValue()); line.setStartX(e.getX()); line.setStartY(e.getY()); menu.setText("Line"); } else if(rectbtn.isSelected()) { gc.setStroke(cpLine.getValue()); rect.setX(e.getX()); rect.setY(e.getY()); menu.setText("Rectangle"); } else if(circlebtn.isSelected()) { gc.setStroke(cpLine.getValue()); circ.setCenterX(e.getX()); circ.setCenterY(e.getY()); menu.setText("Circle"); } else if(dotbtn.isSelected()) { gc.setStroke(cpLine.getValue()); gc.setFill(cpLine.getValue()); dot.setCenterX(e.getX()); dot.setCenterY(e.getY()); menu.setText("Dot"); } }); canvas.setOnMouseDragged(e->{ if(linebtn.isSelected()) { gc.setStroke(cpLine.getValue()); line.setEndX(e.getX()); line.setEndY(e.getY()); gc.strokeLine(line.getStartX(), line.getStartY(), line.getEndX(), line.getEndY()); menu.setText("Line"); } }); canvas.setOnMouseReleased(e->{ if(linebtn.isSelected()) { line.setEndX(e.getX()); line.setEndY(e.getY()); gc.strokeLine(line.getStartX(), line.getStartY(), line.getEndX(), line.getEndY()); } else if(rectbtn.isSelected()) { rect.setWidth(Math.abs((e.getX() - rect.getX()))); rect.setHeight(Math.abs((e.getY() - rect.getY()))); //rect.setX((rect.getX() > e.getX()) ? e.getX(): rect.getX()); if(rect.getX() > e.getX()) { rect.setX(e.getX()); } //rect.setY((rect.getY() > e.getY()) ? e.getY(): rect.getY()); if(rect.getY() > e.getY()) { rect.setY(e.getY()); } gc.strokeRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight()); } else if(circlebtn.isSelected()) { circ.setRadius((Math.abs(e.getX() - circ.getCenterX()) + Math.abs(e.getY() - circ.getCenterY())) / 2); if(circ.getCenterX() > e.getX()) { circ.setCenterX(e.getX()); } if(circ.getCenterY() > e.getY()) { circ.setCenterY(e.getY()); } gc.strokeOval(circ.getCenterX(), circ.getCenterY(), circ.getRadius(), circ.getRadius()); } else if(dotbtn.isSelected()) { dot.setRadius(Math.abs(e.getX() - dot.getCenterX())); if(dot.getCenterX() > e.getX()) { dot.setCenterX(e.getX()); } if(dot.getCenterY() > e.getY()) { dot.setCenterY(e.getY()); } gc.strokeOval(dot.getCenterX(), dot.getCenterY(), dot.getRadius(), dot.getRadius()); gc.fillOval(dot.getCenterX(), dot.getCenterY(), dot.getRadius(), dot.getRadius()); } }); // color picker cpLine.setOnAction(e->{ gc.setStroke(cpLine.getValue()); }); /* ----------STAGE & SCENE---------- */ BorderPane pane = new BorderPane(); pane.setTop(btns); pane.setCenter(canvas); Scene scene = new Scene(pane, 1200, 800); primaryStage.setTitle("Paint"); primaryStage.setScene(scene); primaryStage.show(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started