In java please Write a program that lets the user click on a
In java please,
Write a program that lets the user click on a pane to dynamically create and remove points (see Figure). When the user left-clicks the mouse (primary button), a point is created and displayed at the mouse point. The user can remove a point by pointing to it and right clicking the mouse (secondary button). Exercise allows the user to create/remove points dynamically.Solution
import javafx.application.Application;
import javafx.collections.*;
import javafx.scene.*;
import javafx.stage.*;
public class MyApplication extends Application
{
Pane p = new Pane();
@Override
public void start(Stage ps)
{
double width = 400;
double height = 400;
p.setOnMouseClicked(e -> {
double x = e.getX();
double y = e.getY();
if(e.getButton() == MouseButton.PRIMARY) {
Circle c = drawPoint(x, y);
p.getChildren().add(c);
}
else if(e.getButton() == MouseButton.SECONDARY) {
removePoint(x, y);
}
});
Scene sc = new Scene(p,width, height);
ps.setScene(sc);
ps.setTitle(\"Click to draw circle\");
ps.show();
}
private Circle drawPoint(double a, double b)
{
Circle c= new Circle(a, b, 10, Color.TRANSPARENT);
c.setStroke(Color.BLACK);
return c;
}
private void removePoint(double a, double b)
{
ObservableList<Node> list = p.getChildren();
for(int i=list.size-1; i>=0; i--)
{
Node c= list.get(i);
if(c instance of Circle && c.contains(a,b))
{
pane.getChildren().remove(c);
break;
}
}
}
public static void main(String args[])
{
Application.launch(args);
}
}

