Foundations of Programming 2: JavaFX Basics

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
Object Oriented Programming
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L01 (Chapter 13) Graphics.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 14 Applets, Images,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L14 (Chapter 21) Generics.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Benedicto Fernandez Software Engineer CERN / GS-AIS Dubna 2012.
Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Lab 6: Shapes & Picture Extended Ellipse & Rectangle Classes Stand-Alone GUI Applications.
Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.
CS 112 Department of Computer Science George Mason University CS 112 Department of Computer Science George Mason University Writing Graphic Applications.
Topics Introduction Scene Graphs
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Generics.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization Codes.
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.
Computer Science 320 A First Program in Parallel Java.
Section §14.1 Introduction  Graphical User Interface (GUI) applications, as opposed to console UI applications, rely heavily on objects  JavaFX.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 14 JavaFX Basics.
Lecture 7:Introduction to JavaFX Michael Hsu CSULA.
Va installato Qui le istruzioni
Lecture 12 CS 202. FXML JavaFX also offers a way to code UI controls in XML, while writing the event handlers and other application logic in Java. The.
Lecture 7:Introduction to JavaFX Michael Hsu CSULA.
Chapter 14 JavaFX Basics.
Lecture 18 CS
Java Programming Java FX.
Lecture 7:Introduction to JavaFX
Java FX.
Exceptions, Interfaces & Generics
Chapter 6 More Conditionals and Loops
Chapter 19 Java Never Ends
Creating and Modifying Text part 2
“Processing” easy coding Jens Dalsgaard Nielsen
Java Software Solutions Foundations of Program Design 9th Edition
EE 422C Java FX.
Chapter 14 JavaFX Basics Dr. Clincy - Lecture.
Chapter 14 JavaFX Basics Dr. Clincy - Lecture.
Java Software Solutions Foundations of Program Design 9th Edition
Recall: Timeline Class
Computing Adjusted Quiz Total Score
More inheritance, Abstract Classes and Interfaces
What you should already know…. (Appendices C, D, and E)
class PrintOnetoTen { public static void main(String args[]) {
ㅡ.
Chapter 14 JavaFX Basics Part 1
Chapter 14 JavaFX Basics Part 2
Chapter 15 Event-Driven Programming and Animations Part 1
CMPE212 – Reminders Assignment 5, a JavaFX GUI, due next Friday.
Foundations of Programming 2: Abstract Classes and Interfaces
Dasar-Dasar Pemrograman 2: Recursion
Using JavaFX Applications
Dasar-Dasar Pemrograman 2: Java Conditions and Loops
Introduction to Java FX
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Foundations of Programming 2: JavaFX Basics FoP 2 Teaching Team, Faculty of Computer Science, Universitas Indonesia Correspondence: Fariz Darari (fariz@cs.ui.ac.id) https://www.pexels.com/photo/time-lapse-photography-of-people-walking-on-pedestrian-lane-842339/ Feel free to use, reuse, and share this work: the more we share, the more we have!

Why? Not everything has to be in command-line interfaces (CLIs). Non-CS people (and GUI-minded CS people) would appreciate GUIs a lot! https://www.iconfinder.com/icons/103422/browser_window_icon

Why? Not everything has to be in command-line interfaces (CLIs). Non-CS people (and GUI-minded CS people) would appreciate GUIs a lot! https://www.iconfinder.com/icons/103422/browser_window_icon

Why? Not everything has to be in command-line interfaces (CLIs). Non-CS people (and GUI-minded CS people) would appreciate GUIs a lot! https://www.iconfinder.com/icons/103422/browser_window_icon

Intro JavaFX is a library for creating GUIs in Java. OOPs are heavily utilized in JavaFX. A JavaFX application can run both on a desktop and a Web browser.

The very basics of JavaFX import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage; public class GUI001 extends Application { @Override public void start(Stage stg) throws Exception { Button button = new Button("Readyyy!"); Scene scene = new Scene(button, 250, 100); stg.setTitle("GUI 001"); stg.setScene(scene); stg.show(); } public static void main(String[] args) { Application.launch(args);

The very basics of JavaFX import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage; public class GUI001 extends Application { @Override public void start(Stage stg) throws Exception { Button button = new Button("Readyyy!"); Scene scene = new Scene(button, 250, 100); stg.setTitle("GUI 001"); stg.setScene(scene); stg.show(); } public static void main(String[] args) { Application.launch(args);

The very basics of JavaFX import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage; public class GUI001 extends Application { @Override public void start(Stage stg) throws Exception { Button button = new Button("Readyyy!"); Scene scene = new Scene(button, 250, 100); stg.setTitle("GUI 001"); stg.setScene(scene); stg.show(); } public static void main(String[] args) { Application.launch(args); The stage won't be resizable! Add stg.setResizable(false); and what would happen?

Multiple stages // ... other code parts follow previous code public void start(Stage stg1) throws Exception { stg1.setTitle("GUI 002"); stg1.setScene(new Scene(new Button("Gooo!"), 200,100)); stg1.show(); Stage stg2 = new Stage(); stg2.setTitle("GUI 002"); stg2.setScene(new Scene(new Button("Steadyyy!"), 200,100)); stg2.show(); Stage stg3 = new Stage(); stg3.setTitle("GUI 002"); stg3.setScene(new Scene(new Button("Readyyy!"), 200,100)); stg3.show(); } GUI002.java

Multiple stages // ... other code parts follow previous code public void start(Stage stg1) throws Exception { stg1.setTitle("GUI 002"); stg1.setScene(new Scene(new Button("Gooo!"), 200,100)); stg1.show(); Stage stg2 = new Stage(); stg2.setTitle("GUI 002"); stg2.setScene(new Scene(new Button("Steadyyy!"), 200,100)); stg2.show(); Stage stg3 = new Stage(); stg3.setTitle("GUI 002"); stg3.setScene(new Scene(new Button("Readyyy!"), 200,100)); stg3.show(); } Close the window, you'll see the Steadyyy! stage, close the window again, you'll see the Gooo! stage.

A pane is like a container public void start(Stage stg1) throws Exception { StackPane pn = new StackPane(); pn.getChildren().add(new Button("Niceee!")); // because Nice! would sound rude Scene scn = new Scene(pn, 300, 100); stg1.setScene(scn); stg1.show(); } GUI003.java

A pane is like a container public void start(Stage stg1) throws Exception { StackPane pn = new StackPane(); pn.getChildren().add(new Button("Niceee!")); // because Nice! would sound rude Scene scn = new Scene(pn, 300, 100); stg1.setScene(scn); stg1.show(); } Note that the button now doesn't occupy the whole scene, thanks to Pane!

Stacked square and button public void start(Stage stg1) throws Exception { Rectangle rect = new Rectangle(100, 100, 180, 140); rect.setFill(Color.BLUE); StackPane pn = new StackPane(rect, new Button("Niceee!")); Scene scn = new Scene(pn, 500, 200); stg1.setScene(scn); stg1.show(); } GUI003c.java

Stacked square and button public void start(Stage stg1) throws Exception { Rectangle rect = new Rectangle(100, 100, 180, 140); rect.setFill(Color.BLUE); StackPane pn = new StackPane(rect, new Button("Niceee!")); Scene scn = new Scene(pn, 500, 200); stg1.setScene(scn); stg1.show(); }

Quiztime: Java art GUI003b Checkpoint of Class B

Quiztime: Java art public void start(Stage stg1) throws Exception { StackPane pn = new StackPane(); for(int i = 100; i > 0; i--) { Rectangle rect = new Rectangle(0,0,5*i,5*i); switch(i % 3) { case 0: rect.setFill(Color.RED); break; case 1: rect.setFill(Color.GREEN); break; case 2: rect.setFill(Color.BLUE); break; } pn.getChildren().add(rect); Scene scn = new Scene(pn, 500, 500); stg1.setScene(scn); stg1.show(); GUI003b Checkpoint of Class C

Circle public void start(Stage stg1) throws Exception { Circle c = new Circle(); c.setCenterX(200); c.setCenterY(100); c.setRadius(50); c.setStroke(Color.ALICEBLUE); c.setStrokeWidth(5); c.setFill(Color.BISQUE); Pane pn = new Pane(); pn.getChildren().add(c); Scene scn = new Scene(pn, 400, 200); stg1.setTitle("Life's like a circle"); stg1.setScene(scn); stg1.show(); } GUI004

Circle public void start(Stage stg1) throws Exception { Circle c = new Circle(); c.setCenterX(200); c.setCenterY(100); c.setRadius(50); c.setStroke(Color.ALICEBLUE); c.setStrokeWidth(5); c.setFill(Color.BISQUE); Pane pn = new Pane(); pn.getChildren().add(c); Scene scn = new Scene(pn, 400, 200); stg1.setTitle("Life's like a circle"); stg1.setScene(scn); stg1.show(); }

Circle public void start(Stage stg1) throws Exception { Circle c = new Circle(); c.setCenterX(70); c.setCenterY(100); c.setRadius(50); c.setStroke(Color.ALICEBLUE); c.setStrokeWidth(5); c.setFill(Color.BISQUE); Pane pn = new Pane(); pn.getChildren().add(c); Scene scn = new Scene(pn, 400, 200); stg1.setTitle("Life's like a circle"); stg1.setScene(scn); stg1.show(); }

Circle public void start(Stage stg1) throws Exception { Circle c = new Circle(); c.setCenterX(70); c.setCenterY(0); c.setRadius(50); c.setStroke(Color.ALICEBLUE); c.setStrokeWidth(5); c.setFill(Color.BISQUE); Pane pn = new Pane(); pn.getChildren().add(c); Scene scn = new Scene(pn, 400, 200); stg1.setTitle("Life's like a circle"); stg1.setScene(scn); stg1.show(); }

Quiztime: Loops of circles GUI005

Solution public Circle createCircle(int X, int Y) { Random r = new Random(); Circle c = new Circle(); c.setCenterX(X); c.setCenterY(Y); c.setRadius(30); c.setFill(Color.rgb(r.nextInt(256), r.nextInt(256), r.nextInt(256))); return c; } // ... cont

Solution // ... cont @Override public void start(Stage stg1) throws Exception { Pane pn = new Pane(); for(int i = 0; i < 5; i++) for(int j = 0; j < 5; j++) pn.getChildren().add(createCircle(50 + i*65,50 + j*65)); Scene scn = new Scene(pn, 360, 360); stg1.setTitle("Circles"); stg1.setScene(scn); stg1.setResizable(false); stg1.show(); }

JavaFX provides many other shapes Image copyright: https://dzone.com/refcardz/javafx-8-1?chapter=6

Quiztime: We love buttons GUI006

Solution @Override public void start(Stage stg1) throws Exception { VBox pn = new VBox(); pn.setAlignment(Pos.CENTER); pn.setSpacing(20); for(int i = 1; i <= 9; i++) pn.getChildren().add(new Button("Button " + i)); Scene scn = new Scene(pn, 300, 500); stg1.setTitle("We love buttons"); stg1.setScene(scn); stg1.setResizable(false); stg1.show(); }

Quiztime: We love horizontal buttons GUI007

Solution @Override public void start(Stage stg1) throws Exception { HBox pn = new HBox(); pn.setAlignment(Pos.CENTER); pn.setSpacing(20); for(int i = 1; i <= 9; i++) pn.getChildren().add(new Button("Button " + i)); Scene scn = new Scene(pn, 800, 200); stg1.setTitle("We love buttons"); stg1.setScene(scn); stg1.setResizable(false); stg1.show(); }

JavaFX Labels and Images GUI008

Solution public void start(Stage stg1) throws Exception { VBox pn = new VBox(); pn.setAlignment(Pos.CENTER); pn.setSpacing(20); pn.getChildren().add(new Label("Happy Ramadhan!")); FileInputStream fis = new FileInputStream("pics/ramadhan.jpg"); Image img = new Image(fis); ImageView iv = new ImageView(img); iv.setFitHeight(400); iv.setPreserveRatio(true); pn.getChildren().add(iv); Scene scn = new Scene(pn, 500, 500); stg1.setTitle("Happy Ramadhan!"); stg1.setScene(scn); stg1.setResizable(false); stg1.show(); }

Quiztime: Create your own Happy Ramadhan (or Merry X-Mas, or any greetings!

ChoiceBox GUI009

ChoiceBox: Code public void start(Stage stg1) throws Exception { VBox pn = new VBox(); pn.setAlignment(Pos.CENTER); pn.setSpacing(20); pn.getChildren().add(new Label("Pick your fav K-pop star!")); String[] stars = {"Joo Ko-Wee", "Park Bo-Wow"}; ChoiceBox cb = new ChoiceBox(FXCollections.observableArrayList(stars)); pn.getChildren().add(cb); Scene scn = new Scene(pn, 180, 150); stg1.setScene(scn); stg1.setResizable(false); stg1.show(); }

TextField GUI010 Sandiaga Yunho

TextField: Code public void start(Stage stg1) throws Exception { HBox pn = new HBox(); pn.setAlignment(Pos.CENTER); pn.setSpacing(20); pn.getChildren().add(new Label("Who's fav K-pop star?")); TextField tf = new TextField(); tf.setPrefWidth(150); pn.getChildren().add(tf); Scene scn = new Scene(pn, 300, 100); stg1.setScene(scn); stg1.show(); } Sandiaga Yunho

CheckBox GUI011

CheckBox: Code public void start(Stage stg1) throws Exception { VBox pn = new VBox(); pn.setAlignment(Pos.CENTER); pn.setSpacing(20); pn.getChildren().add(new Label("Who are your fav K-pop stars?")); CheckBox cb1 = new CheckBox("Joo Ko-Wee"); CheckBox cb2 = new CheckBox("Park Bo-Wow"); CheckBox cb3 = new CheckBox("Sandiaga Yunho"); pn.getChildren().add(cb1); pn.getChildren().add(cb2); pn.getChildren().add(cb3); Scene scn = new Scene(pn, 200, 200); stg1.setScene(scn); stg1.show(); }

Liang, Introduction to Java, 10th edition, Pearson. Picture: https://unsplash.com/photos/VyC0YSFRDTU Inspired by: Liang, Introduction to Java, 10th edition, Pearson. https://www.geeksforgeeks.org/ Google