Object Oriented Programming-I (LAB Session :- 10)


Basic JavaFX Programming 


1.Write a program that moves a circle up, down, left or right using            arrow keys.

CODE :

package application;

import javafx.application.Application;

import javafx.event.EventHandler;

import javafx.scene.layout.BorderPane;

import javafx.scene.paint.Color;

import javafx.scene.shape.Circle;

import javafx.stage.Stage;

import javafx.scene.Scene;

import javafx.scene.input.KeyCode;

import javafx.scene.input.KeyEvent;

 

public class p1 extends Application {

public void start(Stage s) {

BorderPane p = new BorderPane();

Circle c1 = new Circle();

c1.setCenterX(500);

c1.setCenterY(500);

c1.setRadius(50);

c1.setFill(Color.RED);

p.getChildren().add(c1);

Scene sc = new Scene(p);

 

sc.setOnKeyPressed(new EventHandler<KeyEvent>() {

public void handle(KeyEvent k) {

if(k.getCode().equals (KeyCode.UP)) {

c1.setCenterX(c1.getCenterX());

c1.setCenterY(c1.getCenterY()-100);}

else if(k.getCode().equals (KeyCode.DOWN))

{

c1.setCenterX(c1.getCenterX());

c1.setCenterY(c1.getCenterY()+100);

}

else if(k.getCode().equals (KeyCode.LEFT)) {

c1.setCenterX(c1.getCenterX()-100);

c1.setCenterY(c1.getCenterY());

}

else if(k.getCode().equals (KeyCode.RIGHT)) {

c1.setCenterX(c1.getCenterX()+100);

c1.setCenterY(c1.getCenterY());

}

else

{

}

}

});

 

s.setScene(sc);

s.sho

w();

}}

 

OUTPUT:

  








2.Write a program that displays the colour of a circle as red when the mouse button is pressed and as blue when the mouse button is released.

CODE :

package sample;

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 Main  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 :





Download pdf here........


No comments:

Post a Comment