0% found this document useful (0 votes)
18 views11 pages

JAVAFX

Uploaded by

paxobe3782
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views11 pages

JAVAFX

Uploaded by

paxobe3782
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

220760116025

PRACTICAL-17
AIM: Write a program that displays a tic-tac-toe board. A cell may be
X, O, or empty. What to display at each cell is randomly decided.
The X and O are images in the files X.gif and O.gif.
/*

* To change this template, choose Tools | Templates * and open the template in the
editor.

*/

package javafxapplication4;

import javafx.application.Application; import javafx.event.ActionEvent; import


javafx.event.EventHandler; import javafx.scene.Scene; import
javafx.scene.control.Button; import javafx.scene.image.Image; import
javafx.scene.image.ImageView; import javafx.scene.layout.GridPane; import
javafx.scene.layout.StackPane; import javafx.stage.Stage;

/**

* @author 576

*/

public class JavaFXApplication4 extends Application { public static void


main(String[] args) { launch(args);

@Override

public void start(Stage stage) {

GridPane pane = new GridPane();

for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { int n = (int)(Math.random() *
3); if (n == 0)

pane.add(new ImageView(new Image("image/x.gif")), j, i); else if (n == 1)


220760116025

pane.add(new ImageView(new Image("image/o.gif")), j, i); else continue;

stage.setTitle("tic-tac-toe"); stage.setScene(new Scene(pane, 300, 400));


stage.show();

OUTPUT:
220760116025

PRACTICAL-18
AIM: Write a program that moves a circle up, down, left or right
using arrow keys.
package practical18;

import javafx.application.Application; import javafx.scene.Scene; import


javafx.scene.input.KeyCode; import javafx.scene.layout.Pane; import
javafx.scene.shape.Circle; import javafx.stage.Stage;

public class Practical18 extends Application {

@Override

public void start(Stage primaryStage) { final Pane pane = new Pane(); final Circle
circle = new Circle(30, 30, 30); pane.getChildren().add(circle);

pane.setOnKeyPressed(new
javafx.event.EventHandler<javafx.scene.input.KeyEvent>() {

@Override

public void handle(javafx.scene.input.KeyEvent event) { handleKeyPress(event,


circle, pane);

});

Scene scene = new Scene(pane, 200, 200); primaryStage.setTitle("Arrow Keys


Move Circle");

primaryStage.setScene(scene); primaryStage.show(); pane.requestFocus();

private void handleKeyPress(javafx.scene.input.KeyEvent event, Circle circle,


Pane pane) { switch (event.getCode()) { case UP:

circle.setCenterY(circle.getCenterY() > circle.getRadius() ? circle.getCenterY() -


15 : circle.getCenterY());

break; case DOWN:


220760116025

circle.setCenterY(circle.getCenterY() < pane.getHeight() - circle.getRadius() ?


circle.getCenterY() + 15 : circle.getCenterY()); break; case LEFT:

circle.setCenterX(circle.getCenterX() > circle.getRadius() ? circle.getCenterX() -


15 : circle.getCenterX());

break; case RIGHT:

circle.setCenterX(circle.getCenterX() < pane.getWidth() - circle.getRadius() ?


circle.getCenterX() + 15 : circle.getCenterX()); break; default:

// Do nothing for other keys

public static void main(String[] args) { launch(args);

OUTPUT:
220760116025

PRACTICAL-19
AIM: 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.

/*

* To change this template, choose Tools | Templates * and open the template in the
editor.

*/

package practical19;

import javafx.application.Application; import javafx.event.EventHandler; import


javafx.scene.Scene; import javafx.scene.input.MouseEvent; import
javafx.scene.layout.StackPane; import javafx.scene.paint.Color; import
javafx.scene.shape.Circle; import javafx.stage.Stage;

public class Practical19 extends Application {

@Override

public void start(Stage primaryStage) { final Circle circle = new Circle(50, 50, 20);
circle.setStroke(Color.BLACK);

StackPane stackPane = new StackPane(); stackPane.getChildren().add(circle);

Scene scene = new Scene(stackPane, 200, 200);

primaryStage.setScene(scene);

// Event handler for mouse pressed

stackPane.setOnMousePressed(new EventHandler<MouseEvent>() {

@Override

public void handle(MouseEvent event) { circle.setFill(Color.RED);

});
220760116025

// Event handler for mouse released

stackPane.setOnMouseReleased(new EventHandler<MouseEvent>() {

@Override

public void handle(MouseEvent event) { circle.setFill(Color.BLUE);

});

primaryStage.setTitle("Circle Color Demo"); primaryStage.show();

public static void main(String[] args) { launch(args);

OUTPUT:
220760116025

PRACTICAL-20
AIM: 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.

/*

* To change this template, choose Tools | Templates * and open the template in the
editor.

*/

package practocal20;

import javafx.application.Application; import javafx.event.ActionEvent; import


javafx.event.EventHandler; import javafx.scene.Scene; import
javafx.scene.control.Button; import javafx.scene.control.RadioButton; import
javafx.scene.control.ToggleGroup; import javafx.scene.layout.BorderPane; import
javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import
javafx.scene.paint.Color; import javafx.scene.text.Text; import javafx.stage.Stage;

public class Practocal20 extends Application {

protected Text text = new Text(50, 50, "CodingKick");

@Override

HBox paneForButtons = new HBox(20);

Button btLeft = new Button("<="); Button btRight = new Button("=>");


paneForButtons.getChildren().addAll(btLeft, btRight);
paneForButtons.setAlignment(javafx.geometry.Pos.CENTER); BorderPane pane =
new BorderPane(); pane.setBottom(paneForButtons);

final RadioButton rbRed = new RadioButton("Red"); final RadioButton rbYellow


= new RadioButton("Yellow"); final RadioButton rbBlack = new
RadioButton("Black"); final RadioButton rbOrange = new
RadioButton("Orange"); final RadioButton rbGreen = new RadioButton("Green");

final ToggleGroup group = new ToggleGroup(); // Declare group as final


rbRed.setToggleGroup(group); rbYellow.setToggleGroup(group);
220760116025

rbBlack.setToggleGroup(group); rbOrange.setToggleGroup(group);
rbGreen.setToggleGroup(group);

HBox paneForRadioButtons = new HBox(20);

paneForRadioButtons.getChildren().addAll(rbRed, rbYellow, rbBlack, rbOrange,


rbGreen);

Pane paneForText = new Pane(); paneForText.setStyle("-fx-border-color: black");


paneForText.getChildren().add(text); pane.setCenter(paneForText);

pane.setTop(paneForRadioButtons);

btLeft.setOnAction(new EventHandler<ActionEvent>() {

@Override

public void handle(ActionEvent e) { text.setX(text.getX() - 10);

});

btRight.setOnAction(new EventHandler<ActionEvent>() {

@Override

public void handle(ActionEvent e) { text.setX(text.getX() + 10);

});

rbRed.setOnAction(new EventHandler<ActionEvent>() {

@Override

public void handle(ActionEvent e) { if (rbRed.isSelected()) {


text.setFill(Color.RED);

});

rbYellow.setOnAction(new EventHandler<ActionEvent>() {
220760116025

@Override

public void handle(ActionEvent e) {

if (rbYellow.isSelected()) { text.setFill(Color.YELLOW);

});

rbBlack.setOnAction(new EventHandler<ActionEvent>() {

@Override

public void handle(ActionEvent e) { if (rbBlack.isSelected()) {


text.setFill(Color.BLACK);

});

rbOrange.setOnAction(new EventHandler<ActionEvent>() {

@Override

public void handle(ActionEvent e) { if (rbOrange.isSelected()) {


text.setFill(Color.ORANGE);

});

rbGreen.setOnAction(new EventHandler<ActionEvent>() {

@Override

public void handle(ActionEvent e) { if (rbGreen.isSelected()) {

text.setFill(Color.GREEN);

}
220760116025

});

Scene scene = new Scene(pane, 450, 150); primaryStage.setTitle("GUI program");


primaryStage.setScene(scene); primaryStage.show();

public static void main(String[] args) { launch(args);

}
220760116025

OUTPUT:

You might also like