Write a program that moves the ball in a pane You should def
Solution
public class Exe extends Application
{
public void start(Stage primaryStage) throws Exception
{
double width = 400;
double height = 400;
BallPane ballPane = new BallPane(width / 2,height / 2, Math.min(width,height) / 15);
Button btUp = new Button(\"Up\");
btUp.setOnAction(e -> ballPane.moveUp());
Button btDown = new Button(\"Down\");
btDown.setOnAction(e -> ballPane.moveDown());
Button btLeft = new Button(\"Left\");
btLeft.setOnAction(e -> ballPane.moveLeft());
Button btRight = new Button(\"Right\");
btRight.setOnAction(e -> ballPane.moveRight());
HBox buttons = new HBox(btUp, btDown, btLeft, btRight);
buttons.setAlignment(Pos.BOTTOM_CENTER);
buttons.setSpacing(10);
buttons.setPadding(new Insets(10, 10, 10, 10));
BorderPane pane = new BorderPane();
pane.setCenter(ballPane);
pane.setBottom(buttons);
Scene scene = new Scene(pane, width, height);
primaryStage.setScene(scene);
primaryStage.setTitle(\"Click to move ball\");
primaryStage.show();
}
private class BallPane extends Pane
{
private Circle mCircle;
public BallPane()
{
this(0, 0, 10);
}
public BallPane(double centerX, double centerY, double radius)
{
mCircle = new Circle(centerX, centerY, radius);
getChildren().add(mCircle);
}
public void moveUp()
{
if (mCircle.getCenterY() - mCircle.getRadius() - 10 < 0) return;
mCircle.setCenterY(mCircle.getCenterY() - 10);
}
public void moveDown()
{
if (mCircle.getCenterY() + mCircle.getRadius() + 10 > getHeight()) return;
mCircle.setCenterY(mCircle.getCenterY() + 10);
}
public void moveRight()
{
if (mCircle.getCenterX() + mCircle.getRadius() + 10 > getWidth()) return;
mCircle.setCenterX(mCircle.getCenterX() + 10);
}
public void moveLeft()
{
if (mCircle.getCenterX() - mCircle.getRadius() - 10 < 0) return;
mCircle.setCenterX(mCircle.getCenterX() - 10);
}
}
public static void main(String[] args)
{
Application.launch(args);
}
}

