Download Java source from MouseDemo.zip or MouseDemo.jar
Click to insert a capacitor, drag to move it, double-click to remove it.
MouseDemo.java
import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Group; //import javafx.scene.Cursor; import javafx.scene.*; import javafx.event.EventHandler; import javafx.scene.paint.*; import javafx.scene.shape.*; import javafx.collections.*; import javafx.geometry.Point2D; import javafx.geometry.Bounds; import javafx.scene.input.* ; import javafx.scene.layout.Pane; import java.lang.Math; import java.awt.image.BufferedImage; import java.awt.Graphics2D; import javafx.scene.control.Button; import javafx.event.ActionEvent; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import java.util.ArrayList; public class MouseDemo extends Application { Group grp = new Group(); ArrayList<Capacitor3> list = new ArrayList<Capacitor3>(); public void start(Stage stage) { // Create a border pane BorderPane pane = new BorderPane(); Scene scene = new Scene(pane, 400, 400); Pane mypane = new Pane(); pane.setCenter(mypane); mypane.getChildren().add(grp); grp.setTranslateX(scene.getWidth()/2); grp.setTranslateY(scene.getHeight()/2); double res = 96; grp.setScaleX(res); grp.setScaleY(res); Grid.draw(grp); grp.setOnMousePressed( e -> { //System.out.println("event type " + e.getEventType()); //System.out.println("Local " + e.getX() + " " + e.getY()); Point2D pt = new Point2D(e.getScreenX(),e.getScreenY()); Capacitor3 current = find(pt); if (current==null) { Capacitor3 c = new Capacitor3(); c.setPosition(e.getX(),-e.getY()); c.draw(grp); list.add(c); } } ); grp.setOnMouseClicked( e -> { Point2D pt = new Point2D(e.getScreenX(),e.getScreenY()); if (e.getClickCount()>=2) { Capacitor3 current = find(pt); if (current==null) return; list.remove(current); grp.getChildren().remove(current.grp); } //System.out.println(e.getEventType() + " click count " + e.getClickCount()); } ); /* Capacitor3 c1 = new Capacitor3(); c1.setPosition(-1.0,0.0); c1.draw(grp); list.add(c1); Capacitor3 c2 = new Capacitor3(); c2.setPosition(1.0,1.0); c2.setAngle(30.0); c2.draw(grp); list.add(c2); */ stage.setTitle("MouseDemo"); stage.setScene(scene); stage.show(); } public Capacitor3 find(Point2D pt) { for (Capacitor3 ct: list) { if (ct.contains(pt)) return ct; } return null; } }
Maintained by John Loomis, updated Mon Mar 05 12:00:09 2018