Professional Documents
Culture Documents
1 JAVA
GRAPHICAL USER INTERFACES
2
JAVA FX VS SWING
Java Swing JavaFX
Swing is the standard toolkit for Java developers in creating GUI JavaFX grants platform assistance for designing desktop applications.
Swing is a legacy library that fully features and provides pluggable UI JavaFX has UI components that are still growing with a further advanced look
components and feel.
Swing can implement UI components with a conventional look and feel. JavaFX can provide rich internet applications having a modern UI.
JavaFX has also support from numerous IDEs as well, but it is not as
Swing has various IDEs, which offer a tool for rapid development.
experienced as Swing.
A swing was renamed from Java Foundation Classes, and sun microsystems JavaFX was initially released in December 2008 by Sun microsystem and is
announced it in the year 1997 now acquired by Oracle.
Events in JavaFX are higher thought-out and additional consistent than their
Events in Swing aren’t that consistent
equivalents in Swing.
Swing has basic controls like buttons, checkboxes, and bandboxes. JavaFX has many eye-catching controls that Swing doesn’t have
GRAPHICAL USER INTERFACES
JAVA FX
○ Platform independent
○ Scene builder
○ Built-in UI controls,
○ CSS styling
4
JAVA FX ARCHITECTURE COMPONENTS
1. JavaFX API
1. Topmost layer with classes for building rich JavaFX applications.
2. Key packages:
1. javafx.animation: Transition-based animations for nodes.
2. Scene Graph
1. Foundation of GUI Applications in JavaFX.
2. Comprises nodes, the basic building blocks.
3. Nodes form a hierarchical tree describing the UI.
4. Nodes can have various attributes like Effects, Opacity, Transforms, and Event Handlers.
5. Three general types of nodes.
SIMPLE JAVA FX APPLICATION : A BUTTON
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
button.setOnAction(e -> {
button.setText("Hello, JavaFX!");
});
@Override
public void start(Stage primaryStage) { Create a text input field and a button.
TextField textField = new TextField();
Button button = new Button("Show Text");
Scene scene = new Scene(root, 300, 200); // Create content for the new window
primaryStage.setScene(scene); StackPane newLayout = new StackPane();
Scene newScene = new Scene(newLayout, 300, 200);
primaryStage.show(); newStage.setScene(newScene);
}
// Show the new window
newStage.show();
}
EVENT HANDLING: KEY EVENTS
import javafx.event.EventHandler;
import javafx.scene.input.KeyEvent;
switch (keyCode) {
case LEFT:
translate.setX(translate.getX() - stepSize);
break;
case RIGHT:
translate.setX(translate.getX() + stepSize);
break;
case UP:
translate.setY(translate.getY() - stepSize);
break;
case DOWN:
translate.setY(translate.getY() + stepSize);
break;
default:
break;
}
});
EVENT HANDLING: MOUSE MOTION
scene.setOnMouseMoved(event -> {
double x = event.getX();
double y = event.getY();
coordinatesLabel.setText("Mouse
Coordinates: (" + x + ", " + y + ")");
});
DRAW ON MOUSE DRAGGED
scene.setOnMouseDragged(event -> {
double x = event.getX();
double y = event.getY();
drawPoint(x, y); private void clearCanvas() {
}); gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
}
DRAW IN JAVAFX
private void drawLine(double x1, double y1, double x2, double y2)
{
gc.setStroke(javafx.scene.paint.Color.BLACK);
gc.setLineWidth(2); // Adjust the line width as needed
gc.strokeLine(x1, y1, x2, y2);
}
ADDING AN IMAGE IN JAVAFX
// Create an ImageView
ImageView imageView = new ImageView(image);
imageView.setFitWidth(200);
imageView.setFitHeight(150);
imageView.setPreserveRatio(true);