Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 14 JavaFX Basics.

Slides:



Advertisements
Similar presentations
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Advertisements

1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L01 (Chapter 13) Graphics.
Graphical User Interfaces (GUI) Abstract Windows Toolkit (AWT): java.awt GUI elements: Primitive Button, Label, Checkbox, Scrollbar, etc. Container Panel,
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
Introduction to Java Swing “We are the sultans of swing” – Mark Knopfler.
Written by Liron Blecher
GUI Programming in Java
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Standard Graphics in Java.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
3461A Readings from the Swing Tutorial. 3461A Overview  The follow is the Table of Contents from the trail “Creating a GUI with JFC/Swing” in the “The.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 Object-Oriented Languages.
GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout.
Objectives of This Session
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 Unit 5 GUI Aum Amriteshwaryai Namah. 2 Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
CS 4244: Internet Programming User Interface Programming in Java 1.0.
Object Oriented Programming.  Interface  Event Handling.
SEEM3460 Tutorial GUI in Java. Some Basic GUI Terms Component (Control in some languages) the basic GUI unit something visible something that user can.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Swing. Introduction to Swing What is Swing? “ Swing is a diverse collection of lightweight components that can be used to build sophisticated user interfaces.”
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Basics of GUI Programming Chapter 11 and Chapter 22.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 14 JavaFX Basics.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Sep 181 Example Program DemoTranslateEnglishGUI.java.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Lecture 7:Introduction to JavaFX Michael Hsu CSULA.
Lecture 7:Introduction to JavaFX Michael Hsu CSULA.
Chapter 14 JavaFX Basics.
CSC 205 Programming II Lecture 5 AWT - I.
Welcome To java
CHAPTER Reacting to the user.
Aum Amriteshwaryai Namah
Lecture 7:Introduction to JavaFX
A First Look at GUI Applications
Java FX.
Ellen Walker Hiram College
Java Tutorial Zhe Li.
EE 422C Java FX.
Chapter 14 JavaFX Basics Dr. Clincy - Lecture.
Chapter 14 JavaFX Basics Dr. Clincy - Lecture.
Introduction to Computing Using Java
Graphics Programming - Frames
Programming Graphical User Interface (GUI)
Chapter 14 JavaFX Basics Part 1
Introduction to Java FX
Graphical User Interface
Presentation transcript:

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 14 JavaFX Basics

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 2 Objectives F To distinguish between JavaFX, Swing, and AWT (§14.2). F To write a simple JavaFX program and understand the relationship among stages, scenes, and nodes (§14.3). F To create user interfaces using panes, UI controls, and shapes (§14.4). F To use binding properties to synchronize property values (§14.5). F To use the common properties style and rotate for nodes (§14.6). F To create colors using the Color class (§14.7). F To create fonts using the Font class (§14.8). F To create images using the Image class and to create image views using the ImageView class (§14.9). F To layout nodes using Pane, StackPane, FlowPane, GridPane, BorderPane, HBox, and VBox (§14.10). F To display text using the Text class and create shapes using Line, Circle, Rectangle, Ellipse, Arc, Polygon, and Polyline (§14.11). F To develop the reusable GUI components ClockPane for displaying an analog clock (§14.12).

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 3 Motivations F JavaFX is a new framework for developing Java GUI programs. F The JavaFX API is an excellent example of how the object-oriented principle is applied. –This chapter serves two purposes. u First, it presents the basics of JavaFX programming. u Second, it uses JavaFX to demonstrate OOP. –Specifically, this chapter introduces the framework of JavaFX and discusses JavaFX GUI components and their relationships.

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. Introduction F There are two sets of Java APIs for graphics programming: –AWT (Abstract Windowing Toolkit) and –Swing F AWT API was introduced in JDK 1.0. Most of the AWT components have become obsolete and should be replaced by newer Swing components. F Swing API, a much more comprehensive set of graphics libraries that enhances the AWT, was introduced as part of Java Foundation Classes (JFC) after the release of JDK 1.1. F JFC consists of Swing, Java2D, Accessibility, Internationalization, and Pluggable Look- and-Feel Support APIs. JFC was an add-on to JDK 1.1 but has been integrated into core Java since JDK

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. JavaFX F JavaFX is a software platform for creating and delivering desktop applications, as well as rich internet applications (RIAs) that can run across a wide variety of devices. F JavaFX is intended to replace Swing as the standard GUI library for Java SE, but both will be included for the foreseeable future. 5 IFX is just a name, which is normally related with sound or visual effects in the javafx i was in the beilef that the fx was function.... FIPS standsfor the Federal Information Processing Standardisation

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 6 JavaFX vs Swing and AWT F Swing and AWT are replaced by the JavaFX platform for developing rich Internet applications. F When Java was introduced, the GUI classes were bundled in a library known as the Abstract Windows Toolkit (AWT). F AWT is fine for developing simple graphical user interfaces, but not for developing comprehensive GUI projects. In addition, AWT is likely to platform-specific bugs. F The AWT user-interface components were replaced by a more robust, versatile, and flexible library known as Swing components. F Swing components are painted directly on canvases using Java code.

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 7 // Create AWT Button Example // This java example shows how to create a Button using AWT Button class. import java.applet.Applet; import java.awt.Button; /* */ public class CreateAWTButtonExample extends Applet{ public void init(){ /* * To create a button use * Button() constructor. */ Button button1 = new Button(); /* * Set button caption or label using * void setLabel(String text) * method of AWT Button class. */ button1.setLabel("My Button 1"); /* * To create button with the caption use * Button(String text) constructor of * AWT Button class. */ Button button2 = new Button("My Button 2"); //add buttons using add method add(button1); add(button2); } }

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. Links for API Resources F You need to check the JDK API specification ( for the AWT and Swing APIs while reading this chapter. The best online reference for Graphics programming is the "Swing For advanced 2D graphics programming, read "Java 2D 8

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. Programming GUI with AWT Java Graphics APIs - AWT and Swing - provide a huge set of reusable GUI components, such as: –Button –text field label, –choice, –panel and –frame for building GUI applications. 9

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. AWT Packages F AWT is huge! It consists of 12 packages F Swing is even bigger, with 18 packages as of JDK 1.7!. F Fortunately, only 2 packages - java.awt andjava.awt.event - are commonly- used. F The java.awt package contains the core AWT graphics classes: u GUI Component classes (such as Button, TextField, and Label), u GUI Container classes (such as Frame, Panel, Dialog and ScrollPane), u Layout managers (such as FlowLayout, BorderLayout and GridLayout), u Custom graphics classes (such as Graphics, Color and Font). F The java.awt.event package supports event handling: u Event classes (such as ActionEvent, MouseEvent, KeyEvent and WindowEvent), u Event Listener Interfaces (such as ActionListener, MouseListener, KeyListener and WindowListener), u Event Listener Adapter classes (such as MouseAdapter, KeyAdapter, and WindowAdapter). F AWT provides a platform-independent and device-independent interface to develop graphic programs that runs on all platforms, such as Windows, Mac, and Linux. 10

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 11 import java.awt.*; // Using AWT container and component classes import java.awt.event.*; // Using AWT event classes and listener interfaces // An AWT program inherits from the top-level container java.awt.Frame public class AWTCounter extends Frame implements ActionListener { private Label lblCount; // Declare component Label private TextField tfCount; // Declare component TextField private Button btnCount; // Declare component Button private int count = 0; // Counter's value /** Constructor to setup GUI components and event handling */ public AWTCounter () { setLayout(new FlowLayout()); // "super" Frame sets its layout to FlowLayout, which arranges the components // from left-to-right, and flow to next row from top-to-bottom. lblCount = new Label("Counter"); // construct Label add(lblCount); // "super" Frame adds Label tfCount = new TextField("0", 10); // construct TextField tfCount.setEditable(false); // set to read-only add(tfCount); // "super" Frame adds tfCount btnCount = new Button("Count"); // construct Button add(btnCount); // "super" Frame adds Button btnCount.addActionListener(this); // Clicking Button source fires ActionEvent // btnCount registers this instance as ActionEvent listener setTitle("AWT Counter"); // "super" Frame sets title setSize(250, 100); // "super" Frame sets initial window size // System.out.println(this); // System.out.println(lblCount); // System.out.println(tfCount); // System.out.println(btnCount); setVisible(true); // "super" Frame shows // System.out.println(this); // System.out.println(lblCount); // System.out.println(tfCount); // System.out.println(btnCount); } /** The entry main() method */ public static void main(String[] args) { // Invoke the constructor to setup the GUI, by allocating an instance AWTCounter app = new AWTCounter(); } /** ActionEvent handler - Called back upon button-click. */ public void actionPerformed(ActionEvent evt) { ++count; // increase the counter value // Display the counter value on the TextField tfCount tfCount.setText(count + ""); // convert int to String } } Example: AWT Counter Output

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. Containers and Components F There are two types of GUI elements: –Component: Components are elementary GUI entities (such asButton, Label, and TextField.) –Container: Containers (such as Frame, Panel and Applet) are used to hold components in a specific layout (such as flow or grid). A container can also hold sub-containers. –GUI components are also called controls (Microsoft ActiveX Control),widgets (Eclipse's Standard Widget Toolkit, Google Web Toolkit), which allow users to interact with (i.e., control) the application through these components ( such as button-click and text-entry). 12

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 13

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 14 Basic Structure of JavaFX F Application F Override the start(Stage) method F Stage, Scene, and Nodes RunMyJavaFX RunMultipleStageDemo

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. My JavaFx F import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage; public class MyJavaFX extends Application // Override the start method in the Application class public void start(Stage primaryStage) { // Create a button and place it in the scene Button btOK = new Button("OK"); Scene scene = new Scene(btOK, 200, 250); primaryStage.setTitle("MyJavaFX"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage } /** * The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line. */ public static void main(String[] args) { launch(args); } } MultipleStage Demo F import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage; public class MultipleStageDemo extends Application // Override the start method in the Application class public void start(Stage primaryStage) { // Create a scene and place a button in the scene Scene scene = new Scene(new Button("OK"), 200, 250); primaryStage.setTitle("MyJavaFX"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage Stage stage = new Stage(); // Create a new stage stage.setTitle("Second Stage"); // Set the stage title // Set a scene with a button in the stage stage.setScene(new Scene(new Button("New Stage"), 100, 100)); stage.show(); // Display the stage } /** * The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line. */ //public static void main(String[] args) { // launch(args); // } } 15

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 16 Panes, UI Controls, and Shapes RunButtonInPane

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 17 Display a Shape This example displays a circle in the center of the pane. RunShowCircle

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 18 Binding Properties JavaFX introduces a new concept called binding property that enables a target object to be bound to a source object. If the value in the source object changes, the target property is also changed automatically. The target object is simply called a binding object or a binding property. ShowCircleCentered import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.stage.Stage; public class ShowCircleCentered extends Application { public void start(Stage primaryStage) { // Create a pane to hold the circle Pane pane = new Pane(); // Create a circle and set its properties Circle circle = new Circle(); circle.centerXProperty().bind(pane.widthProperty().divide(2)); circle.centerYProperty().bind(pane.heightProperty().divide(2)); circle.setRadius(50); circle.setStroke(Color.BLACK); circle.setFill(Color.WHITE); pane.getChildren().add(circle); // Add circle to the pane // Create a scene and place it in the stage Scene scene = new Scene(pane, 200, 200); primaryStage.setTitle("ShowCircleCentered"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage } /** * The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line. */ public static void main(String[] args) { launch(args); } }

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 19 Binding Property: getter, setter, and property getter

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 20 The Color Class

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 21 The Font Class RunFontDemo

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 22 The Image Class

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 23 The ImageView Class RunShowImage

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 24 Layout Panes JavaFX provides many types of panes for organizing nodes in a container.

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 25 FlowPane RunShowFlowPane

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 26 GridPane Run ShowGridPane

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 27 BorderPane RunShowBorderPane

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 28 HBox

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 29 VBox RunShowHBoxVBox

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 30 Shapes JavaFX provides many shape classes for drawing texts, lines, circles, rectangles, ellipses, arcs, polygons, and polylines.

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 31 Text

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 32 Text Example RunShowText

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 33 Line Run ShowLine

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 34 Rectangle

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 35 Rectangle Example RunShowRectangle

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 36 Circle

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 37 Ellipse Run ShowEllipse

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 38 Arc

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 39 Arc Examples RunShowArc

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 40 Polygon and Polyline RunShowArc

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 41 Polygon RunShowPolygon

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 42 Case Study: The ClockPane Class This case study develops a class that displays a clock on a pane. ClockPane