Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. Text fx program
- import javafx.application.Application;
- import javafx.scene.Group;
- import javafx.scene.Scene;
- import javafx.stage.Stage;
- import javafx.scene.text.Text;
- import javafx.scene.text.Font;
- import javafx.scene.text.FontPosture;
- import javafx.scene.text.FontWeight;
- import javafx.scene.paint.Color;
- public class textfx extends Application
- {
- public void start(Stage stage)
- {
- //Creating a Text object
- Text text = new Text();
- text.setFont(Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 20)); // font of the text
- //Setting the text to be added.
- text.setText("Hi!! welcome to JAVA FX programming");
- //setting the position of the text
- text.setX(50);
- text.setY(50);
- text.setFill(Color.BROWN);
- //Setting the Stroke
- text.setStrokeWidth(2);
- text.setUnderline(true);
- // Setting the stroke color
- text.setStroke(Color.BLUE);
- //Creating a Group object
- Group root = new Group(text);
- //Creating a scene object
- Scene scene = new Scene(root, 600, 300);
- //Setting title to the Stage
- stage.setTitle("TEXT Application");
- //Adding scene to the stage
- stage.setScene(scene);
- //Displaying the contents of the stage
- stage.show();
- }
- public static void main(String args[]){
- launch(args);
- }
- }
- 2.Line program using javafx
- import javafx.application.Application;
- import javafx.scene.Group;
- import javafx.scene.Scene;
- import javafx.scene.shape.Line;
- import javafx.stage.Stage;
- public class line extends Application{
- @Override
- public void start(Stage stage) {
- //Creating a line object
- Line line = new Line();
- //Setting the properties to a line
- line.setStartX(100.0);
- line.setStartY(150.0);
- line.setEndX(500.0);
- line.setEndY(150.0);
- //Creating a Group
- Group root = new Group(line);
- //Creating a Scene
- Scene scene = new Scene(root, 600, 300);
- //Setting title to the scene
- stage.setTitle("Sample application");
- //Adding the scene to the stage
- stage.setScene(scene);
- //Displaying the contents of a scene
- stage.show();
- }
- public static void main(String args[]){
- launch(args);
- }
- }
- 3.Rectangle using javaFx
- import javafx.application.Application;
- import javafx.scene.Group;
- import javafx.scene.Scene;
- import javafx.stage.Stage;
- import javafx.scene.shape.Rectangle;
- public class rectangle1 extends Application {
- @Override
- public void start(Stage stage) {
- //Drawing a Rectangle
- Rectangle rectangle = new Rectangle();
- //Setting the properties of the rectangle
- rectangle.setX(150.0f);
- rectangle.setY(75.0f);
- rectangle.setWidth(300.0f);
- rectangle.setHeight(150.0f);
- //Creating a Group object
- Group root = new Group(rectangle);
- //Creating a scene object
- Scene scene = new Scene(root, 600, 300);
- //Setting title to the Stage
- stage.setTitle("Drawing a Rectangle");
- //Adding scene to the stage
- stage.setScene(scene);
- //Displaying the contents of the stage
- stage.show();
- }
- public static void main(String args[]){
- launch(args);
- }
- }
- 4. Rotate program using javaFx
- import javafx.animation.RotateTransition;
- import javafx.application.Application;
- import static javafx.application.Application.launch;
- import javafx.scene.Group;
- import javafx.scene.Scene;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Polygon;
- import javafx.stage.Stage;
- import javafx.util.Duration;
- public class rotate extends Application
- {
- public void start(Stage stage) {
- //Creating a hexagon
- Polygon hexagon = new Polygon();
- //Adding coordinates to the hexagon
- hexagon.getPoints().addAll(new Double[]{
- 200.0, 50.0,
- 400.0, 50.0,
- 450.0, 150.0,
- 400.0, 250.0,
- 200.0, 250.0,
- 150.0, 150.0,
- });
- //Setting the fill color for the hexagon
- hexagon.setFill(Color.BLUE);
- //Creating a rotate transition
- RotateTransition rotateTransition = new RotateTransition();
- //Setting the duration for the transition
- rotateTransition.setDuration(Duration.millis(1000));
- //Setting the node for the transition
- rotateTransition.setNode(hexagon);
- //Setting the angle of the rotation
- rotateTransition.setByAngle(360);
- //Setting the cycle count for the transition
- rotateTransition.setCycleCount(50);
- //Setting auto reverse value to false
- rotateTransition.setAutoReverse(false);
- //Playing the animation
- rotateTransition.play();
- //Creating a Group object
- Group root = new Group(hexagon);
- //Creating a scene object
- Scene scene = new Scene(root, 600, 300);
- //Setting title to the Stage
- stage.setTitle("Rotate transition example ");
- //Adding scene to the stage
- stage.setScene(scene);
- //Displaying the contents of the stage
- stage.show();
- }
- public static void main(String args[]){
- launch(args);
- }
- }
- 5.button program using javaFx
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.control.*;
- import javafx.scene.layout.*;
- import javafx.stage.Stage;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- public class buttonfx extends Application
- {
- public void start(Stage primaryStage) throws Exception
- {
- //StackPane root = new StackPane();
- Button btn=new Button("This is a button");
- Button b2 = new Button("Exit");
- TextField tf1=new TextField();
- btn.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent e)
- {
- tf1.setText("Button clicked");
- }
- } );
- b2.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent e)
- {
- System.exit(0);
- }
- } );
- GridPane root = new GridPane();
- root.addRow(0, tf1,btn);
- root.addRow(1,b2);
- Scene scene=new Scene(root,300,300);
- // root.getChildren().add(btn);
- // root.getChildren().add(tf1);
- primaryStage.setScene(scene);
- primaryStage.setTitle("Button Class Example");
- primaryStage.show();
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
- 6. Menu program using javaFx
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.control.*;
- import javafx.scene.layout.BorderPane;
- import javafx.stage.Stage;
- public class javafxmenu extends Application {
- public static void main(String[] args) {
- launch(args);
- }
- public void start(Stage primaryStage) throws Exception
- {
- BorderPane root = new BorderPane();
- Scene scene = new Scene(root,200,300);
- MenuBar menubar = new MenuBar();
- Menu FileMenu = new Menu("Colors");
- MenuItem filemenu1=new MenuItem("RED");
- MenuItem filemenu2=new MenuItem("GREEN");
- MenuItem filemenu3=new MenuItem("BLUE");
- Menu EditMenu=new Menu("Flowers");
- MenuItem EditMenu1=new MenuItem("ROSE");
- MenuItem EditMenu2=new MenuItem("JASMINE");
- MenuItem EditMenu3=new MenuItem("LILLY");
- EditMenu.getItems().addAll(EditMenu1,EditMenu2,EditMenu3);
- root.setTop(menubar);
- FileMenu.getItems().addAll(filemenu1,filemenu2,filemenu3);
- menubar.getMenus().addAll(FileMenu,EditMenu);
- primaryStage.setScene(scene);
- primaryStage.show();
- }
- }
- 7.Event filter program
- import javafx.application.Application;
- import static javafx.application.Application.launch;
- import javafx.event.EventHandler;// event handler
- import javafx.scene.Group;
- import javafx.scene.Scene;
- import javafx.scene.input.MouseEvent; //event
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Circle;
- import javafx.scene.text.Font;
- import javafx.scene.text.FontWeight;
- import javafx.scene.text.Text;
- import javafx.stage.Stage;
- public class eventfiltercircle extends Application
- {
- public void start(Stage stage)
- {
- Circle circle = new Circle();
- circle.setCenterX(300.0f);
- circle.setCenterY(135.0f);
- circle.setRadius(25.0f);
- circle.setFill(Color.BROWN);
- circle.setStrokeWidth(20);
- Text text = new Text("Click on the circle to change its color");
- // text.setFont(Font.font(null, FontWeight.BOLD, 15));
- text.setFill(Color.RED);
- text.setX(150);
- text.setY(50);
- EventHandler<MouseEvent> eventHandler = new EventHandler<MouseEvent>()
- {
- public void handle(MouseEvent e)
- {
- System.out.println("Sucess");
- circle.setFill(Color.BLUE);
- // System.exit(0);
- }
- };
- //Registering the event filter
- circle.addEventFilter(MouseEvent.MOUSE_CLICKED, eventHandler);
- Group root = new Group(circle, text);
- Scene scene = new Scene(root, 600, 300);
- scene.setFill(Color.LAVENDER);
- stage.setTitle("Event Filters Example");
- stage.setScene(scene);
- stage.show();
- }
- public static void main(String args[]){
- launch(args);
- }
- }
- 8.Event handler program using javaFx
- import javafx.animation.RotateTransition;
- import javafx.application.Application;
- import javafx.event.EventHandler;
- import javafx.scene.Group;
- import javafx.scene.PerspectiveCamera;
- import javafx.scene.Scene;
- import javafx.scene.control.TextField;
- import javafx.scene.input.KeyEvent;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Box;
- import javafx.scene.text.Font;
- import javafx.scene.text.FontWeight;
- import javafx.scene.text.Text;
- import javafx.scene.transform.Rotate;
- import javafx.stage.Stage;
- import javafx.util.Duration;
- public class eventhandler extends Application
- {
- public void start(Stage stage) {
- Box box = new Box();
- box.setWidth(150.0);
- box.setHeight(150.0);
- box.setDepth(100.0);
- box.setTranslateX(350);
- box.setTranslateY(150);
- box.setTranslateZ(50);
- Text text = new Text("Write something on the text box and click box to stop");
- text.setFont(Font.font(null, FontWeight.BOLD, 15));
- text.setFill(Color.RED);
- text.setX(20);
- text.setY(50);
- RotateTransition rotateTransition = new RotateTransition();
- rotateTransition.setDuration(Duration.millis(1000));
- rotateTransition.setNode(box);
- rotateTransition.setAxis(Rotate.Y_AXIS);
- rotateTransition.setByAngle(360);
- rotateTransition.setCycleCount(50);
- rotateTransition.setAutoReverse(false);
- TextField textField = new TextField();
- textField.setLayoutX(50);
- textField.setLayoutY(100);
- //Handling the key typed event
- EventHandler<KeyEvent> etext = new EventHandler<KeyEvent>()
- {
- public void handle(KeyEvent event) {
- rotateTransition.play();
- }
- };
- //Adding an event handler to the text feld
- textField.addEventHandler(KeyEvent.KEY_TYPED, etext);
- //Handling the mouse clicked event(on box)
- EventHandler<javafx.scene.input.MouseEvent> ebox =
- new EventHandler<javafx.scene.input.MouseEvent>()
- {
- public void handle(javafx.scene.input.MouseEvent e)
- {
- rotateTransition.stop();
- }
- };
- //Adding the event handler to the box
- box.addEventHandler(javafx.scene.input.MouseEvent.MOUSE_CLICKED, ebox);
- Group root = new Group(box, textField, text);
- Scene scene = new Scene(root, 600, 300);
- stage.setTitle("Event Handlers Example");
- stage.setScene(scene);
- stage.show();
- }
- public static void main(String args[]){
- launch(args);
- }
- }
Add Comment
Please, Sign In to add comment