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

Wa0018.

The document discusses JavaFX, comparing it with older frameworks like AWT and Swing, highlighting its modern features such as CSS styling, FXML support, and rich UI controls. It explains the architecture of JavaFX, including the Scene Graph and rendering engine, and demonstrates basic event handling and animation effects. Additionally, it covers inner classes, adapter classes, and mouse events in Java, providing example programs for better understanding.

Uploaded by

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

Wa0018.

The document discusses JavaFX, comparing it with older frameworks like AWT and Swing, highlighting its modern features such as CSS styling, FXML support, and rich UI controls. It explains the architecture of JavaFX, including the Scene Graph and rendering engine, and demonstrates basic event handling and animation effects. Additionally, it covers inner classes, adapter classes, and mouse events in Java, providing example programs for better understanding.

Uploaded by

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

chuptem 1 AwT

) (ompue Jqryfx with Sajng qmd


2) S|23 - Q-s (()
32 Oisu3j Javufx GentE'tS
4) TIlsu ftyte (aic strucs tt of Jaryfx progr4m
S) coht do yo

<) pemonstrate qmimutisy tfe ct in JuvAX,


?) Explyin the conpt+ in ntr clasr ymd plein fht typs
0f innty rxample prugtam
S) Explain qleyt qd qpttr clyJry qnd mo)

txqmplt
9) s/21
lo) Explqin
Q.4 (a) Compare JavaFX with Swing and AWT

Q.4 (a) Compare JavaFX with Swing and AWT


Here's a comparison between JavaFX, Swing, and AWT:
AWT (Abstract
Feature Window Toolkit) Swing JavaFX

Introduced In JDK 1.0 (1995) JDK 1.2 (1998) JDK 8 (2014, fully integrated)

Graphics Heavyweight Lightweight Lightweight


Architecture

Look and Feel OS-dependent Pluggable Look and Modern, CSS-styled,


(native) Feel (cross customizable UI

platform)

UIControls Basic (Button, More advanced Rich controls (Charts, WebView,


TextField, Label, (JTable, JTree, etc.) Media)
etc.)

Layout Management Simple layouts Complex layout Flexible layout panes (VBox,
managers HBox, etc.)

Multimedia Support No Very limited Built-in (audio, video, web


content)

CSS Styling Support No No Yes (CSS-like styling)

FXML Support No No Yes (XML-based Ul design)

Scene Graph No No Yes (retained mode graphics)


Architecture

Hardware No No Yes (uses GPU for rendering)


Acceleration

Ease of Animation Difficult Limited support Built-in animation and transitions

Modern UI Outdated Basic Modern Ul (effects, 3D,


Capabilities responsive design)
(c) What do you understand by event source and event
object? Explain how to
register an event handler object and how to implement a
handler interface

Q.4 (c) Event Source and Event Object in JavaFX

1. Event Source

The event source is the component (Ul element) that generates an event.
" Example: A Button, TextField , or Slider that the user interacts with.

Example:
java O Copy Edit

Button btn = new Button("Click Me"): // btn is the event source

2. Event Object
" The event object is an instance of a class that describes the event.
" It contains information like event type, time, source, etc.
" Common classes:

ActionEvent for buttons

MouseEvent for mouse actions

KeyEvent for keyboard actions

Example:
java Copy Edit

btn.setonAction(e -> {
|/ 'e' is theevent object (ActionEvent)
System.out. println("Sour ce: " + e.getSource());
):

3.Registering an Event Handler Object


You can register an event handler using the setonAction() or similar method.

Using Lambda Expression:


java O Copy 2 Edit

btn.setOnAction(e -> {
System.out. println("Button Clicked");

Using Anonymous Inner Class:


java O Copy 2 Edit

btn.setOnAction(new EventHandler<ActionEvent>() {
@0verride
public void handle(ActionEvent e) {
System.out .println( "Handled by anonymous class") ;

);
Simple JavaFX Event Program
java Copy Edit

import javafx.application .Application;


import javafx.scene.Scene;
import javafx.Scene.control. Button;
import javafx.scene. layout.StackPane;
import javafx.stage.Stage;

public class SimpleEven tDemo extends Application {

@0verride

public void start(Stage primaryStage) {


1/ Create a button
Button btn = new Button(""Click Me");

1| Register event handler using lambda


btn.setOnAction(e -> System.out.println("Button was clicked !")) ;

I/ Add button to layout


StackPane root = new StackPane (btn);

1/ Set up the scene and stage


Scene scene = new Scene(root, 300, 200);
primaryStage. setTitle(" Java FX Simple Event Example");
primaryStage.setScene(scene);
primaryStage. show();

public static void main(String[] args) {


launch(args);

How to Run:

1. Save the file as SimpleEventDemo.java


2. Compile and run using:

bash Copy Edit

javac SimpleEventDemo.java
java SimpleEventDemo

Make sure JavaFX is properly set up in your system.

Let me know if youwant this program using anonymous inner class or with custom event
handler class.
b) Discuss JavaFX benefits?

Q.4 (b) Benefits of JavaFX


JavaFX is a modern GUI framework for Java that offers many advantages over older
frameworks like AWT and Swing. Here are its key benefits:

1. Rich UI Features

" JavaFX supports modern Ul elements like:

Charts (Pie, Line, Bar)


WebView (to display web content)
" Media (audio/video)
. 2D and 3D shapes

2. CSS Styling
JavaFX supports CSS-like styling for Ul components.
This makes it easy to separate design from logic and create visually appealing
applications.

3. FXML Support
" Ul can be designed using FXML (XML-based) files.
" Developers can design the layout using tools like Scene Builder without writing Java
code.

4. Property Binding
" JavaFX allows data binding, so Ul elements automatically update when data changes.
Useful for building reactive interfaces.

5. Hardware Acceleration
" Uses GPU-based rendering for better performance.
" Smooth animations, transitions, and multimedia playback.

6. Built-in Animation Support


" Provides easy-to-use APls for animations like:
"Fade, Rotate, Scale, Translate
" Timeline and KeyFrame animations

7. Cross-Platform
. JavaFX applications run on Windows, macoS, Linux, and mobile platfoms (via third
party tools like Gluon).

8. Integration with Java


" Written in Java, fully integrates with existing Java code.
" Easy for Java developers to learn and use.

9. MVC Support
" Promotes clean architecture using Model-View Controller(MVC) pattern, especialy
with FXML.

10. Scene Graph Architecture


" JavaFX uses a scene graph, a hierarchical tree structure that makes it easier to
manage complex Uls.
(c) Illustrate basic structure of JavaFX progran?

Q.4 (c) Basic Structure of a JavaFX Program


A JavaFX program always starts by extending the Application class and overriding the
start() method.

Basic Structure:

java O Copy 9 Edit

import javafx. application . Application;


import javafx.scene.Scene;
import javafx.scene. control.Button;
import javafx. scene. layout.StackPane;
import javafx.stage.Stage;

public class MyJava FXApp extends Application {

@0verride
public void start(Stage primaryStage) {
11 Create a button
Button btn = new Button("Click Me");

1| Set button action


btn.setOnAction(e -> System.out.println("Hello JavaFX!")):

1/ Create layout and add button


StackPane root = new StackPane(btn);

1| Create scene with layout and size


Scene scene = new Scene(root, 300, 200);

1/ Set up stage (window)


primaryStage.setTitle("My JavaFX App");
primaryStage. setScene(scene);
primaryStage.show();
}

1/ Main method
public static void main(String[] args) {
launch(args); // Launch JavaFX application

Explanation:
Part Description

extends Application JavaFX app must extend Application class.

start(Stage primaryStage ) Main entry point; sets up the Ul and shows the stage
(window).

Scene Holds the content (layout, controls) shown in the stage.

Stage Main window of the JavaFX application.

launch(args) Starts the JavaFX application lifecycle.


Q.5 (a) What do you understand by JavaFX? How it is
different from AWT

Q.5 (a) What do you understand by JavaFX? How is it different from


AWT?

What is JavaFX?

JavaFX isa modern Java GUI (Graphical User Interface) framework used to develop rich
desktop applications. It provides powerful features for building interactive and visually
appealing Uls using:
Scene Graph architecture
" CSS styling
" FXML for layout design
" Built-in animation and multimedia support

Key Features of JavaFX:


Rich Ul controls and 2D/3D graphics
" Easy animation and visual effects
Supports CSS and FXML

" Hardware-accelerated rendering


" Property binding for dynamic Uls

Difference between JavaFX and AWT:


Feature JavaFX AWT (Abstract Window Toolkit)

Release Year Introduced in Java SE 8 (2014) Introduced in JDK 1.0 (1995)

Graphics Architecture Uses Scene Graph and GPU Uses native OS components
acceleration (heavyweight)

Look and Feel Modern and customizable OS-dependent (cannot be styled


easily)

UIControls Rich controls (charts, media, Basic controls only (Button, Label,
WebView, etc.) TextField)

Styling Supports CSS styling No styling support

FXML Support Yes (XML-based UI design) No

Multimedia Support Built-in support for audio/video No multimedia support

Animation Built-in animation classes No animation support

Performance Fast, GPU-accelerated Slower, CPU-bassed rendering


(b) Explain the architecture of JavaFX

Q.5(b)Architecture of JavaFX
JavaFX architecture is designed to create rich and efficient GUl applications using a
modular and layered approach. It mainly consists of the following components:

1. JavaFX Application Layer


" The top layer where developers write code using JavaFX APIs.
" It includes:

" UI Controls (Buttons, Labels, etc.)


" Layouts (VBox, HBox, BorderPane, etc.)
" Events and Property Bindings

2. Scene Graph
" Core part of JavaFX.
" Atree-like structure where each element (node) is a part of the UI.
" Nodes include:

" UI Controls (Button, Label)


" Shapes (Circle , Rectangle)
" Layouts (Pane, VBOx )
" Media ( ImageView, Video )
" The Scene holds the scene graph, and the Stage displays the scene.

3. Glass Windowing Toolkit


Provides the low-level interface between JavaFX and the operating system.
" Manages windowing, screen events, and rendering support.

4. Prism

" The rendering engine of JavaFX.


" Handles rendering of graphics (2D and 3D) using GPU for high performance.
" Supports hardware acceleration.

5. Quantum Toolkit
Connects JavaFX Application Thread with Prism and Glass.
" Manages the coordination between UI, rendering, and window events.

6. Media Engine
" Supports playing audio and video.
" Based on GStreamer (a multimedia framework).
7. Web Engine
" Supports rendering HTML content using WebView.
" Buitt on top of WebKit (browser engine).

JavaFX Architecture Diagram (Text Form):


Ta O Copy Eit

JavaFX Application
| (Controls, Layouts, UI)

Scene Graph |

Quantum Toolkit

| Prism | Glass
| (GPU) | | Windowing|
+.
(b) Demonstrate animation effect in JavaFX

Q.5(b) Demonstrate Animation Effect in JavaFX


JavaFX provides built-in classes for animation, like TranslateTransition.
FadeTransition, RotateTransition, ScaleTransition, and Timeline.

JavaFX Animation - Short Program


java O Copy 9 Edit

import javafx. animation . TranslateTransition;


import javafx. application . Application;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
import javafx.scene. layout.Pane;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Shor tAnimation extends Application {

@0verride

public void start(Stage stage) {


Circle circle = new Circle(50, 100, 20);

TranslateTransition move = new TranslateTransition (Duration.second:


move.setByX(150);
move.setCycleCount (TranslateTransition.INDEFINITE);
move.setAutoReverse(true);
move.play();

Pane pane = new Pane(circle);


stage.setScene(new Scene(pane, 300, 200));
stage.setTitle ("Short Animation" );
stage.show();

public static void main(String[] args) {


launch(args);
}

What it does:
" Creates a circle.

" Moves it 150pixels right and back.


" Repeats forever.
(c) Explain the concept of inner classes and explain the
types of inner
classes with an example program

Q.5 (c) Inner Classes in Java

What is an Inner Class?


An inner class is a class defined within another class. It is used to logically group classes
that are only used in one place, and it helps increase encapsulation.

Types of Inner Classes:


1. Member Inner Class

Defined inside a class but outside any method, like a regular member.
2. Static Nested Class
A static class defined inside another class. It doesn't have access to instance
members of the outer class.

3. Local Inner Class

Defined inside a method (local to that method).


4. Anonymous Inner Class
A
class without a name, usually used to implement interfaces or extend classes on
the fly.
Bxample Program Showing Different Inner Classes:
Jve o Copy Edlt

public class OuterClass {

private String outerName = "Outer";

11 1. Member Inner Class


class Member Inner {
void display() {
System.out. print.in( "Member Inner Class: " + outerName);

11 2. Static Nested Class


static class StaticNested {
void display() {
System.out.println("Static Nested Class"):

void methodWithLocalClass() {
| 3. Local Inner Class
class Local Inner {
void display() {
System.out- printIn ("Local Inner Class");

LocalInner local = new Local Inner 0:


local.display();

void methodWithAnonymousClass() {
1| 4. Anonymous Inner Class implement:ing Runnable
Runnabler = new Runnable() {
public void run() {
System.out. println("Anonymous Inner Class");

r.run();

public static void main(String[] args) {


OuterClass outer = new OuterClass();

11 Member Inner Class


OuterClass . Member Inner member = outer.new MemberInner ():
menber. display();

11 Static Nested class


OuterClass.StaticNested staticNested = new OuterClass . StaticNested
staticNested. display();
1/ Local Inner Class
outer.methodWithLocalClass():

17 Anonymous Inner Class


outer.nethocWithAnonymousClass ();

Output:
Pgeql O Copy Edit

Member Inner Class: Outer


Static Nested Class
Local Inner Class
Anonymous Inner Class
(b) Explain about adapter classes and mouse events with
an example

Q.5 (b) Adapter Classes and Mouse Events in Java

1. Adapter Classes
" Adapter classes provide empty implementations of event listener interfaces.
" Useful when you want to handle only some events of an interface without
implementing all methods.
For example, MouseListener has 5 methods; if you want to implement just one, you
can extend MouseAdapter and override only the needed methods.

2. Mouse Events
" Mouse events include actions like:

mouseClicked

"mousePressed

"mouseReleased

"mouseEntered

"mouseExited

" These events happen when the user interacts with the mouse on a component.

Here's a short Java program using MouseAdapter to handle mouse clicks on a label:

java O Copy 9 Edit

import javax.swing. *;
import java. awt.event.*;

public class ShortMouseAdapterExample extends JFrame {


public Shor tMouseAdapter Example() {
JLabel label = new JLabel ("Click me!");
label.setHorizontalAlignment (SwingConstants.CENTER);
label.addMouselistener (new MouseAdapter() {
public void mouseClicked (MouseEvent e) {
label.setText ("Clicked at" + e.getX() + ", " + e.getY());

});

add(label);
setSize(300, 150);
setDefaultCloseOperation (EXIT_ON_CLOSE);
setVisible (true );

public static void main(String[] args) {


new Shor tMouseAdapter Example();
(c) With a neat diagram explain the Model view controller
design pattern
and list out the advantages and disadvantages of using it
in designing
an application

Q.5 (c) Model-View-Controller (MVC) Design Pattern

What is MVC?

MVC is a software design pattern that separates an application into three interconnected
components:
1. Model:

" Represents the data and business logic of the application.


" Manages data, logic, and rules.
2. View:

" Represents the UI (User Interface).


" Displays data from the model to the user.
3. Controller:

" Acts as an intermediary between Model and View.


" Handles user input, updates Model, and refreshes View.

Diagram of MVC Pattern


sql O Copy Edit

-+

Controller |

| View | <--> | Model |

" User interacts with the View.

Controller processes input and updates the Model.


" Model updates the View to reflect changes.

Advantages of MVC
" Separation of concerns: Clear division of Ul, data, and control logic.
Reusability: Models can be reused with different views.

" Maintainability: Easier to update or maintain parts independently.


" Parallel development: Teams can work simutaneously on Model, View, and
Controller.

Testability: Each component can be tested separately.

Disadvantages of MVC
" Complexity: Increases complexity for simple applications.
" Learning curve: Developers must understand the pattern well.
Overhead: More files and communication between components.
" Tight coupling risk: If not properly designed, Controller and View may become tightly
cOupled.
(b) Explain Inner class with exampl

Inner Class in Java

What is an Inner Class?


An inner class is a class defined within another class. It is used to logically group classes
that belong together and increase encapsulation.
Inner classes have access to the members (including private) of the outer class.

Types of Inner Classes


" Mermber inner class (non-static)
" Static nested class

" Local inner class (defined inside a method)


" Anonymous inner class (no name, inline)

Simple Example of Member Inner Class


java O Copy Edit

public class Outer Class {


private String message = "Hello from Outer Class" ;

1| Inner class
class Inner Class {
void display() {
System.out . println(message); // Accessing outer class member

public static void main(String[] args){


OuterClass outer = new OuterClass ();

1/ Create instance of inner class


OuterClass. InnerClass inner = outer. new InnerClass () :
inner.display():
}

Output:
Pgsql O Copy Edit

Hello from Outer class

Explanation:
" InnerClass is defined inside OuterClass.

" Inner class can access private members of outer class.


" To create an inner class object, you need an instance of the outer class.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy