Java Foundations. 9-1 Introduction to JavaFX. Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Similar documents
Multimedia-Programmierung Übung 3

Computational Expression

Grafica e non solo: Java FX

Essential JavaFX. Using layouts. Panes in JavaFX. Layouts. Tobias Andersson Gidlund LAYOUTS

Come organizzare gli ascoltatori/osservatori

JavaFX Application Structure. Tecniche di Programmazione A.A. 2017/2018

Event-Driven Programming

Grafica e non solo: Java FX

Posizionamento automa-co: Layouts di base. h5p://docs.oracle.com/javafx/2/ layout/jfxpub- layout.htm

The main method. The program s entry point

Grafica e non solo: Java FX

Building Graphical user interface using JavaFX

Event-Driven Programming with GUIs. Slides derived (or copied) from slides created by Rick Mercer for CSc 335

JavaFX fundamentals. Tecniche di Programmazione A.A. 2012/2013

PART ONE CREATING THE BOARD AND MAIN SPRITES

c 2017 All rights reserved. This work may be distributed or shared at no cost, but may not be modified.

Week 5: Images & Graphics. Programming of Interactive Systems. JavaFX Images. images. Anastasia Bezerianos. Anastasia Bezerianos

JavaFX. Using Image Ops Release 2.2 E


Graphical User Interfaces

Graphical User Interfaces

1. (5 points) In your own words, describe what an instance is.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

GUI Output. Adapted from slides by Michelle Strout with some slides from Rick Mercer. CSc 210

Composite Pattern Diagram. Explanation. JavaFX Subclass Hierarchy, cont. JavaFX: Node. JavaFX Layout Classes. Top-Level Containers 10/12/2018

JavaFX. by Doug Lowe

JavaFX a Crash Course. Tecniche di Programmazione A.A. 2015/2016

For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to

Throughout the exam, write concisely and underline key words or phrases. Have fun! Exam 2. Week 7 (Winter 2013). Dr. Yoder. Sec 031.

Java Foundations. 7-2 Instantiating Objects. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

IT In the News. Login tokens have been reset for those affected and vulnerabilities have been fixed. o Vulnerabilities existed since July 2017

COMP1406 Tutorial 5. Objectives: Getting Started: Tutorial Problems:

AP Computer Science Summer Assignment (updated 5/29/2018) DUE : Sept. 4, 2018

JavaFX a Crash Course. Tecniche di Programmazione A.A. 2016/2017

CS 112 Programming 2. Lecture 14. Event-Driven Programming & Animations (1) Chapter 15 Event-Driven Programming and Animations

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Wednesday, November 16, 11

Copyright 2012, Oracle and/or its affiliates. All rights reserved.

Horse in Mo&on. HorseInMo&on

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

JavaFX. Working with the JavaFX Scene Graph Release 8 E March 2014 Learn about the concept of a scene graph and how it is used in JavaFX.

C12: JavaFX Scene Graph, Events, and UI Components

Chapter 15 Event-Driven Programming and Animations

Week 12 Thursday. For milestone #2 and #3, also submit a screenshot of your GUI once it is launched.

Final Exam 90 minutes Eng. Mohammed S. F. Abdual Al

How to make a "hello world" program in Java with Eclipse *

event driven programming user input Week 2 : c. JavaFX user input Programming of Interactive Systems

Chapter 6, Case Study: BallWorld

CO Java SE 8: Fundamentals

CON Visualising GC with JavaFX Ben Evans James Gough

Final Assignment for CS-0401

CST141 JavaFX Basics Page 1

46 Advanced Java for Bioinformatics, WS 17/18, D. Huson, December 21, 2017

Instructions PLEASE READ (notice bold and underlined phrases)

canoo Engineering AG

Real World. static methods and Console & JavaFX App Intros. Lecture 16. Go ahead and PULL Lecture Materials & Sign-in on PollEv

Java Programming. Events and Listeners

For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to

interactive systems graphical interfaces Week 2 : a. Intro to JavaFX Programming of Interactive Systems

Chapter 15. Event-Driven Programming and Animations

JavaFX a Crash Course. Tecniche di Programmazione A.A. 2017/2018

JavaFX. Getting Started with JavaFX Scene Builder Release 1.1 E

CSCI-142 Exam 2 Review September 25, 2016 Presented by the RIT Computer Science Community

Rules and syntax for inheritance. The boring stuff

Lesson 5: Review and Deliver Presentations Microsoft PowerPoint 2016 IN THIS CHAPTER, YOU WILL LEARN HOW TO

JavaFX Scene Builder

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

Threads & Timers. CSE260, Computer Science B: Honors Stony Brook University

C15: JavaFX: Styling, FXML, and MVC

//Create BorderPane layout manager. layout = new BorderPane(); //This is the "root node".

The Essence of OOP using Java, Nested Top-Level Classes. Preface

COMP6700/2140 Scene Graph, Layout and Styles

Retro Gaming With Lambdas. Stephen Chin Java Technology Ambassador JavaOne Content Chair

Creating Professional Swing UIs Using the NetBeans GUI Builder

Beautiful User Interfaces with JavaFX

Ges$one di base degli even$

CST141 JavaFX Events and Animation Page 1

Computer Applications Information Processing 1

Chapter11 practice file folder. For more information, see Download the practice files in this book s Introduction.

Recipes4Success. Animate Plant Growth. Share 4 - Animation

C14: JavaFX: Overview and Programming User Interface

Graphical User Interfaces

JavaFX Technology Building GUI Applications With JavaFX - Tutorial Overview

Skills Exam Objective Objective Number

Pro JavaFX 2. Weiqi Gao, Ph.D. Stephen Chin. Apress* James L. Weaver. Dean Iverson with Johan Vos, Ph.D.

MS WORD INSERTING PICTURES AND SHAPES

Using Dreamweaver. 4 Creating a Template. Logo. Page Heading. Home About Us Gallery Ordering Contact Us Links. Page content in this area

Object-Oriented Programming Design. Topic : Graphics Programming GUI Part I

COMP6700/2140 GUI and Event Driven Programming

AP Computer Science Unit 1. Writing Programs Using BlueJ

Advanced Java for Bioinformatics Winter 2017/18. Prof. Daniel Huson

Using Microsoft Word. Tables

CISC 1600 Lecture 3.1 Introduction to Processing

Creating Vector Shapes Week 2 Assignment 1. Illustrator Defaults

Curriculum Map Grade(s): Subject: AP Computer Science

AP Computer Science A Summer Assignment

Lesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014

AP Computer Science Unit 1. Writing Programs Using BlueJ

Java FX GUI. Pieter van den Hombergh. May 31, Fontys Hogeschool voor Techniek en Logistiek. Java FX GUI. Pieter van den Homb

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Transcription:

Java Foundations 9-1 Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Objectives This lesson covers the following objectives: Create a JavaFX project Explain the components of the default JavaFX project Describe different types of Nodes and Panes Explain the Scene Graph, Root Node, Scenes, and Stages Copyright 2015, Oracle and/or its affiliates. All rights reserved. 3

Topics Preview Creating a JavaFX Program The Root Node The Scene Graph, Scene, and Stage Introduction to JavaFX Colors and Shapes Graphics, Audio, and Mouse Events Section 9 Copyright 2015, Oracle and/or its affiliates. All rights reserved. 4

It s Almost Time for Final Exams! It s important to study. Do you like to study with friend? But do your friends live in other dorms? Where is the best place to meet your friends? What is the most centrally located point on campus? Thanks for reminding me Copyright 2015, Oracle and/or its affiliates. All rights reserved. 5

JavaFX Can Help JavaFX is used to create GUI applications. GUI: Graphical user interface A GUI application allows us to see the answer on a map. Copyright 2015, Oracle and/or its affiliates. All rights reserved. 6

Exercise 1 Run CampusMap.jar. Align each square with the correct dorm on the map. Estimate and adjust each dorm s population Click and drag the text below each square. Observe changes in the following center points: All students in all dorms A study group of three friends living in Dorms 1, 2, and 4 Copyright 2015, Oracle and/or its affiliates. All rights reserved. 7

But That s Not my Campus! You re right. It would be better if the program used your school s Map of campus Dorm names Dorm populations And your group of friends That s this section s problem set. Section 9 discusses everything you ll need to re-create the program. Copyright 2015, Oracle and/or its affiliates. All rights reserved. 8

Topics Preview Creating a JavaFX Program The Root Node The Scene Graph, Scene, and Stage Introduction to JavaFX Colors and Shapes Graphics, Audio, and Mouse Events Section 9 Copyright 2015, Oracle and/or its affiliates. All rights reserved. 9

How to Create a JavaFX Program 1. In NetBeans, click the New Project button ( ). 2. Select JavaFX for Category and JavaFX Application for Project, and then click Next. 3. Continue like you re creating any other Java project. Copyright 2015, Oracle and/or its affiliates. All rights reserved. 10

Exercise 2 Create a JavaFX project. Java should provide you with a default program. Experiment with the program. Can you make these changes? Change the button s label. Change what s printed when the button is clicked. Create another button and display both buttons. Change the default size of the application s window. Copyright 2015, Oracle and/or its affiliates. All rights reserved. 11

The Default JavaFX Project public class JavaFXTest extends Application { @Override public void start(stage primarystage) { Button btn = new Button(); btn.settext("say 'Hello World'"); btn.setonaction(new EventHandler<ActionEvent>() { }); @Override public void handle(actionevent event) { System.out.println("Hello World!"); } StackPane root = new StackPane(); root.getchildren().add(btn); Scene scene = new Scene(root, 300, 250); } primarystage.settitle("hello World!"); primarystage.setscene(scene); primarystage.show(); public static void main(string[] args) { launch(args); } Copyright 2015, Oracle and/or its affiliates. All rights reserved. 12

Two Methods: start() and main() start() is the entry point for all JavaFX applications. Think of it as the main method for JavaFX. public void start(stage primarystage) { } main() is still required in your programs. It launches the JavaFX application. public static void main(string[] args) { launch(args); } Copyright 2015, Oracle and/or its affiliates. All rights reserved. 13

Buttons Are Objects Buttons are like any other object. They can be instantiated. They contain fields. They contain methods. public void start(stage primarystage) { Button btn = new Button(); btn.settext("say 'Hello World'"); } From this code, we can tell Buttons contain a text field. Buttons contain a method for changing the text field. Copyright 2015, Oracle and/or its affiliates. All rights reserved. 14

Buttons Are Nodes Some of these fields and methods are designed to store and manipulate visual properties: btn.gettext() btn.setminheight() btn.setlayoutx() //set x position btn.setlayouty() //set y position btn.ispressed() //is it pressed? Objects like this are called JavaFX Nodes. Copyright 2015, Oracle and/or its affiliates. All rights reserved. 15

Nodes There are many types of JavaFX Nodes: Button Rectangle PieChart ScrollBar Text ImageView Visual objects you ll create will most likely Be a Node, or Include a Node as a field Copyright 2015, Oracle and/or its affiliates. All rights reserved. 16

Node Interaction The following helps handle Button interaction: public void start(stage primarystage) { btn.setonaction(new EventHandler<ActionEvent>() { @Override public void handle(actionevent event) { System.out.println("Hello World!"); } }); } This is called an anonymous inner class. Doesn t the syntax look messy? Java SE 8 Lambda expressions are an elegant alternative. We ll discuss Lambda expressions later in this section. Copyright 2015, Oracle and/or its affiliates. All rights reserved. 17

Topics Preview Creating a JavaFX Program The Root Node The Scene Graph, Scene, and Stage Introduction to JavaFX Colors and Shapes Graphics, Audio, and Mouse Events Section 9 Copyright 2015, Oracle and/or its affiliates. All rights reserved. 18

Creating Nodes Nodes are instantiated like any other Java object: public void start(stage primarystage) { Button btn1 = new Button(); Button btn2 = new Button(); btn1.settext("say 'Hello World'"); btn2.settext("222"); } After you instantiate a Node: It exists and memory is allocated to store the object. Its fields can be manipulated, and methods can be called. But it might not be displayed Copyright 2015, Oracle and/or its affiliates. All rights reserved. 19

Displaying Nodes There are a few steps to displaying a node. public void start(stage primarystage) { Button btn1 = new Button(); Button btn2 = new Button(); btn.settext("say 'Hello World'"); btn.settext("222"); } StackPane root = new StackPane(); root.getchildren().add(btn1); root.getchildren().add(btn2); First, add each Node to the Root Node. It s usually named root. It s very much like an ArrayList of all Nodes. Copyright 2015, Oracle and/or its affiliates. All rights reserved. 20

Adding Nodes to the Root Node You could add each Node separately: root.getchildren().add(btn1); root.getchildren().add(btn2); root.getchildren().add(btn3); Or you could add many Nodes at once: root.getchildren().addall(btn1, btn2, btn3); But don t add the same Node more than once. This causes a compiler error: root.getchildren().add(btn1); root.getchildren().add(btn1); Copyright 2015, Oracle and/or its affiliates. All rights reserved. 21

StackPane Root Node The Root Node in this example is a StackPane. StackPane root = new StackPane(); root.getchildren().addall(btn1, btn2); The StackPane stacks Nodes on top of each other. But small buttons could become buried and unreachable. Copyright 2015, Oracle and/or its affiliates. All rights reserved. 22

Panes as Root Nodes Each Pane determines the layout of Nodes. Copyright 2015, Oracle and/or its affiliates. All rights reserved. 23

Programming Different Panes as Root Nodes It's easy to design the root node as a different pane. Just specify a different reference type and object type. StackPane root = new StackPane(); root.getchildren().addall(btn1, btn2); TilePane root = new TilePane(); root.getchildren().addall(btn1, btn2); VBox root = new VBox(); root.getchildren().addall(btn1, btn2); Copyright 2015, Oracle and/or its affiliates. All rights reserved. 24

Exercise 3 Edit your current JavaFX project. We re going to do a little experimenting. After adding a button to the Root Node, try to change its position. btn1.setlayouty(100); Will a button s position change if the Root Node wasn't a StackPane? Try these alternatives: TilePane VBox Group Copyright 2015, Oracle and/or its affiliates. All rights reserved. 25

Group Root Node A Group allows you to place Nodes anywhere. Group root = new Group(); root.getchildren().addall(btn1, btn2); btn1.setlayouty(100); A pane may restrict where Nodes are placed. You couldn t move them even if you wanted to. You couldn t click and drag a node that s locked in a pane. StackPane root = new StackPane(); root.getchildren().addall(btn1, btn2); btn1.setlayouty(100); //Has no effect Copyright 2015, Oracle and/or its affiliates. All rights reserved. 26

A Group Can Contain a Pane Panes are also Nodes. Any node can be added to the Root Node. A Pane may be a good option for storing buttons, text input dialog boxes, and other GUI elements. You can t quite move individual Nodes in a Pane. But you can move the entire Pane in a Group. Move the Pane like you would any other Node. Copyright 2015, Oracle and/or its affiliates. All rights reserved. 27

Exercise 4 Edit your current JavaFX project. It s time for more experimenting. Can you figure out how to do the following? Create an HBox pane and add several buttons to it. Add the HBox pane to a Group Root Node. Position the HBox near the bottom of the window. Copyright 2015, Oracle and/or its affiliates. All rights reserved. 28

Topics Preview Creating a JavaFX Program The Root Node The Scene Graph, Scene, and Stage Introduction to JavaFX Colors and Shapes Graphics, Audio, and Mouse Events Section 9 Copyright 2015, Oracle and/or its affiliates. All rights reserved. 29

The JavaFX Scene Graph How you decide to add nodes can be drawn as a Scene Graph. The Root Node contains an Hbox. The HBox acts as a container for buttons. Group Root Node HBox Button Button Button Copyright 2015, Oracle and/or its affiliates. All rights reserved. 30

The Scene Graph The HBox keeps the GUI organized and conveniently located. The rest of the window could be used for other Nodes. Group Root Node HBox Rectangle Text ImageView Button Button Button Copyright 2015, Oracle and/or its affiliates. All rights reserved. 31

The Scene and Stage If we look at the rest of the default JavaFX program, we notice two more things: A Scene (which contains the Root Node) A Stage (which contains the Scene) public void start(stage primarystage) { Scene scene = new Scene(root, 300, 250); } primarystage.settitle("hello World!"); primarystage.setscene(scene); primarystage.show(); Copyright 2015, Oracle and/or its affiliates. All rights reserved. 32

What Is the Scene? There are a few notable properties that describe a Scene: Scene Graph The Scene is the container for all content in the JavaFX Scene Graph. Size The width and height of the Scene can be set. Background The background can be set as a Color or BackgroundImage. Cursor Information The Scene can detect mouse events and handles cursor properties. Scene scene = new Scene(root, 300, 250, Color.BLACK); Copyright 2015, Oracle and/or its affiliates. All rights reserved. 33

What Is the Stage? Think of the Stage as the application window. Here are two notable Stage properties: Title The title of the Stage can be set. Scene The Stage contains a Scene. primarystage.settitle("hello World!"); primarystage.setscene(scene); primarystage.show(); Copyright 2015, Oracle and/or its affiliates. All rights reserved. 34

Hierarchy Animation A Stage is the top-level container. A Stage contains a Scene. A Scene contains a Root Node. The Root Node contains other Nodes. Stage Scene Root Node Node Node Node Copyright 2015, Oracle and/or its affiliates. All rights reserved. 35

Many Scenes, One Stage It s possible to swap any scene into a single Stage. Copyright 2015, Oracle and/or its affiliates. All rights reserved. 36

Many Scenes, Many Stages It s also possible to create many Stages. Copyright 2015, Oracle and/or its affiliates. All rights reserved. 37

Interactive Quizzes Copyright 2015, Oracle and/or its affiliates. All rights reserved. 38

Summary This lesson covers the following objectives: Create a JavaFX project Explain the components of the default JavaFX project Describe different types of Nodes and Panes Explain the Scene Graph, Root Node, Scenes, and Stages Copyright 2015, Oracle and/or its affiliates. All rights reserved. 39