*15.8 (Display the mouse position) Write two programs, such that one displays the mouse position when the mouse button is clicked (see Figure 15.26a) and the other displays the mouse position when the mouse button is pressed and ceases to display it when the mouse button is released.
package section_15;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class _08_1 extends Application {
public void start(Stage primaryStage) {
Text text = new Text();
Pane pane = new Pane();
pane.getChildren().add(text);
pane.setOnMouseClicked(e -> {
double x = e.getX();
double y = e.getY();
text.setText("(" + x + "," + y + ")");
text.setX(x);
text.setY(y);
});
Scene scene = new Scene(pane, 400, 200);
primaryStage.setTitle("_15_08_1");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
package section_15;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class _08_2 extends Application {
public void start(Stage primaryStage) {
Text text = new Text();
Pane pane = new Pane();
pane.setOnMousePressed(e -> {
double x = e.getX();
double y = e.getY();
text.setText("(" + x + ", " + y + ")");
text.setX(x);
text.setY(y);
pane.getChildren().add(text);
});
pane.setOnMouseReleased(e -> pane.getChildren().remove(text));
Scene scene = new Scene(pane, 400, 200);
primaryStage.setTitle("_15_08_2");
primaryStage.setScene(scene);
primaryStage.show();
}
}