Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Enrollment No.

200170107069 Practical 9

Name: Patel Aditi Mukeshkumar


Enrollment No.: 200170107069
Subject: Object Orientated Programming

Practical : 9
Practical 9.1)
Write a program that moves a circle up, down, left or right using arrow keys.
Code:

import javafx.application.Application; circle.getCenterY() + 15 : circle.getCenterY());


import javafx.scene.Scene; break;
import javafx.scene.shape.Circle; case LEFT :
import javafx.scene.layout.Pane; circle.setCenterX(circle.getCenterX() >
import javafx.geometry.Insets; circle.getRadius() ? circle.getCenterX() - 15 :
import javafx.stage.Stage; circle.getCenterX()); break;
public class Practical9_01 extends Application case RIGHT :
{ circle.setCenterX(circle.getCenterX() <
@Override pane.getWidth() - circle.getRadius() ?
public void start(Stage primaryStage) { circle.getCenterX() + 15: circle.getCenterX());
Pane pane = new Pane(); }
pane.setPadding(new Insets(30, 30, 30, 30)); });

Circle circle = new Circle(30, 30, 30); Scene scene = new Scene(pane, 500, 500);
pane.getChildren().add(circle); primaryStage.setTitle("Practical9_01");
pane.setOnKeyPressed(e -> { primaryStage.setScene(scene);
switch (e.getCode()) { primaryStage.show();
case UP : pane.requestFocus();
circle.setCenterY(circle.getCenterY() > }
circle.getRadius() ? circle.getCenterY() - 15 : public static void main(String[] args) {
circle.getCenterY()); break; Application.launch(args);
case DOWN : }
circle.setCenterY(circle.getCenterY() < }
pane.getHeight() - circle.getRadius() ?

Output:

Object Orientated Programming 1|Page


Enrollment No. 200170107069 Practical 9

5 step left
+
3 steps down 

Practical 9.2)
Write a program that displays the color of a circle as red when the mouse button is
pressed and as blue when the mouse button is released.
Code:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class Practical9_02 extends Application{
@Override
public void start(Stage primaryStage) {
double width = 400;
double height = 400;
Circle c = new Circle(width / 2, height / 2, Math.min(width, height) / 10, Color.WHITE);
c.setStroke(Color.BLACK);
StackPane pane = new StackPane(c);
primaryStage.setScene(new Scene(pane, width, height));
pane.setOnMousePressed(e -> c.setFill(Color.RED));
pane.setOnMouseReleased(e -> c.setFill(Color.BLUE));
primaryStage.setTitle("Click circle..");
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Output:

When mouse button pressed When mouse button released

Object Orientated Programming 2|Page


Enrollment No. 200170107069 Practical 9

Practical 9.3)
Write a GUI program that use button to move the message to the left and right and use
the radio button to change the color for the message displayed.
Code:

import javafx.application.Application;
import javafx.stage.Stage; paneForRadioButtons.getChildren().addAll(rbR
import javafx.scene.Scene; ed, rbYellow, rbBlack, rbOrange, rbGreen);
import javafx.geometry.Pos; ToggleGroup group = new
import javafx.scene.control.Button; ToggleGroup();
import javafx.scene.layout.HBox; rbRed.setToggleGroup(group);
import javafx.scene.layout.Pane; rbYellow.setToggleGroup(group);
import javafx.scene.layout.BorderPane; rbBlack.setToggleGroup(group);
import javafx.scene.text.Text; rbOrange.setToggleGroup(group);
import javafx.scene.control.RadioButton; rbGreen.setToggleGroup(group);
import javafx.scene.control.ToggleGroup; Pane paneForText = new
import javafx.scene.paint.Color; Pane();
public class Practical9_03 extends Application paneForText.setStyle("-fx-
{ border-color: black");
protected Text text = new Text(50, 50, paneForText.getChildren().add(text);
"Programming is fun"); pane.setCenter(paneForText);
@Override pane.setTop(paneForRadioButtons);
public void start(Stage primaryStage) { btLeft.setOnAction(e ->
HBox paneForButtons = new text.setX(text.getX() - 10));
HBox(20); btRight.setOnAction(e ->
Button btLeft = new text.setX(text.getX() + 10));
Button("<="); rbRed.setOnAction(e -> {
Button btRight = new if (rbRed.isSelected())
Button("=>"); {
text.setFill(Color.RED);
}
paneForButtons.getChildren().addAll(btLeft, });
btRight); rbYellow.setOnAction(e -> {
Object Orientated Programming 3|Page
if (rbYellow.isSelected()) {
text.setFill(Color.YELLOW);
paneForButtons.setAlignment(Pos.CENTER); }
Enrollment No. 200170107069 Practical 9

rbGreen.setOnAction(e -> {
if (rbGreen.isSelected()) {
text.setFill(Color.GREEN);
}
});
Scene scene = new Scene(pane, 450, 150);
primaryStage.setTitle("Exercise_16_01");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Output:

Object Orientated Programming 4|Page

You might also like