JavaFX - Lib.fbtuit.uz

Transcription

JavaFXAbout the TutorialJavaFX is a Java library used to build Rich Internet Applications. The applications written usingthis library can run consistently across multiple platforms. The applications developed usingJavaFX can run on various devices such as Desktop Computers, Mobile Phones, TVs, Tablets,etc.To develop GUI Applications using Java programming language, the programmers rely onlibraries such as Advanced Windowing Toolkit and Swings. After the advent of JavaFX, theseJava programmers can now develop GUI applications effectively with rich content.In this tutorial, we will discuss all the necessary elements of JavaFX that you can use to developeffective Rich Internet Applications.AudienceThis tutorial has been prepared for beginners who want to develop Rich Internet Applicationsusing JavaFX.PrerequisitesFor this tutorial, it is assumed that the readers have a prior knowledge of Java programminglanguage.Copyright & Disclaimer Copyright 2016 by Tutorials Point (I) Pvt. Ltd.All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt.Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish anycontents or a part of contents of this e-book in any manner without written consent of thepublisher.We strive to update the contents of our website and tutorials as timely and as precisely aspossible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd.provides no guarantee regarding the accuracy, timeliness or completeness of our website or itscontents including this tutorial. If you discover any errors on our website or in this tutorial,please notify us at contact@tutorialspoint.comi

JavaFXTable of ContentsAbout the Tutorial. iAudience . iPrerequisites . iCopyright & Disclaimer . iTable of Contents . ii1.JAVAFX – OVERVIEW. 1What is JavaFX? . 2Need for JavaFX . 2Features of JavaFX . 2History of JavaFX . 32.JAVAFX – ENVIRONMENT . 4Installing Java8 . 4Setting up the Path for Windows .10Setting NetBeans Environment of JavaFX .11Installing JavaFX in Eclipse .283.JAVAFX – ARCHITECTURE. 39Scene Graph .40Prism .41GWT (Glass Windowing Toolkit) .41Quantum Toolkit .41WebView .41Media Engine .42ii

JavaFX4.JAVAFX – APPLICATION . 43JavaFX Application Structure.43Creating a JavaFX Application .45Lifecycle of JavaFX Application .48Example 1 – Creating an Empty Window (Stage) .49Example 2 – Drawing a Straight Line .52Example 3 – Displaying Text .565.JAVAFX – 2D SHAPES . 622D Shape .62Creating a 2D Shape .622D Shapes – Line .662D Shapes – Rectangle .70Rounded Rectangle .752D Shapes – Circle .782D Shapes – Ellipse.832D Shapes – Polygon .882D Shapes – Polyline .942D Shapes – QuadCurve .982D Shapes – CubicCurve .1042D Shapes – Arc .1102D Shapes – SVGPath .116Drawing More Shapes Through Path Class .121Example – Drawing a Complex Path .124PathElement – Line .129PathElement – Horizontal Line .134PathElement – Vertical Line .139iii

JavaFXPathElement – Quadratic Curve .145PathElement – Cubic Curve .151PathElement – Arc .156Properties of 2D Objects .162Operations on 2D Objects .1676.JAVAFX – TEXT . 177Creating a Text Node.177Position and Font of the Text .179Stroke and Color .182Applying Decorations to Text .1857.JAVAFX – EFFECTS. 189Applying Effects to a Node .189Color Adjust Effect .193Color Input Effect .196Image Input Effect .199Blend Effect .202Bloom Effect .208Glow Effect .211Box Blur Effect .213Gaussian Blur Effect .216Motion Blur Effect .219Reflection Effect .222Sepia Tone Effect .225Shadow Effect .228Drop Shadow Effect .231Inner Shadow Effect .235iv

JavaFXLighting Effect (Default Source) .239Lighting Effect (Distant Source) .242Lighting Effect (Spot Light as Source) .2468.JAVAFX – TRANSFORMATIONS. 255Rotation .255Scaling.258Translation .262Shearing .265Multiple Transformations .268Transformations on 3D Objects .2719.JAVAFX – ANIMATIONS . 274Fade Transition .274Fill Transition .277Rotate Transition .279Scale Transition .282Stroke Transition.284Translate Transition .286Sequential Transition .289Parallel Transition .293Pause Transition .297PathTransition .30010. JAVAFX – COLORS . 307Applying Color to the Nodes .307Applying Image Pattern to the Nodes .310Applying Linear Gradient Pattern .313v

JavaFXApplying Radial Gradient Pattern .31611. JAVAFX – IMAGES . 320Loading an Image .320Multiple Views of an Image .323Writing Pixels .32512. JAVAFX – 3D SHAPES . 329Creating a 3D Shape .3293D Shapes – Box .3313D Shapes – Cylinder .3353D Shapes – Sphere .340Properties of 3D Objects .34513. JAVAFX – EVENT HANDLING . 358Types of Events .358Events in JavaFX .358Event Handling .359Phases of Event Handling in JavaFX .359Adding and Removing Event Filter .361Event Handling Example .361Adding and Removing Event Handlers .364Using Convenience Methods for Event Handling .36914. JAVAFX – CONTROLS (UI CONTROLS) . 37515. JAVAFX – CHARTS . 386Creating a Chart .386Charts – Pie Chart.390Charts – Line Chart .397vi

JavaFXCharts – Area Chart .404Charts – Bar Chart .410Charts – Bubble Chart .417Charts – Scatter Chart .424Charts – StackedArea Chart .430Charts – Stacked Bar Chart .43916. JAVAFX – LAYOUT PANES (CONTAINERS) . 448HBox .451VBox .453BorderPane .456StackPane .458FlowPane .461AnchorPane .464TextFlow .467TilePane .470GridPane .47317. JAVAFX – CSS . 477CSS in JavaFX .477vii

1. JavaFX – OverviewJavaFXRich Internet Applications are those web applications which provide similar featuresand experience as that of desktop applications. They offer a better visual experience whencompared to the normal web applications to the users. These applications are delivered asbrowser plug-ins or as a virtual machine and are used to transform traditional staticapplications into more enhanced, fluid, animated and engaging applications.Unlike traditional desktop applications, RIA’s don’t require to have any additional softwareto run. As an alternative, you should install software such as ActiveX, Java, Flash,depending on the Application.In an RIA, the graphical presentation is handled on the client side, as it has a plugin thatprovides support for rich graphics. In a nutshell, data manipulation in an RIA is carried outon the server side, while related object manipulation is carried out on the client side.We have three main technologies using which we can develop an RIA. These include thefollowing: Adobe Flash Microsoft Silverlight JavaFXAdobe FlashThis software platform is developed by Adobe Systems and is used in creating Rich InternetApplications. Along with these, you can also build other Applications such asVector, Animation, Browser Games, Desktop Applications, Mobile Applications and Games,etc.This is the most commonly used platform for developing and executing RIA’s with adesktop browser penetration rate of 96%.Microsoft SilverlightJust like Adobe flash, Microsoft Silverlight is also a software application framework fordeveloping as well as executing Rich Internet Applications. Initially this framework wasused for streaming media. The present versions support multimedia, graphics, andanimation as well.This platform is rarely used with a desktop browser penetration rate of 66%.JavaFXJavaFX is a Java library using which you can develop Rich Internet Applications. By usingJava technology, these applications have a browser penetration rate of 76%.1

JavaFXWhat is JavaFX?JavaFX is a Java library used to build Rich Internet Applications. The applications writtenusing this library can run consistently across multiple platforms. The applicationsdeveloped using JavaFX can run on various devices such as Desktop Computers, MobilePhones, TVs, Tablets, etc.To develop GUI Applications using Java programming language, the programmers relyon libraries such as Advanced Windowing Toolkit and Swings. After the advent ofJavaFX, these Java programmers can now develop GUI applications effectively with richcontent.Need for JavaFXTo develop Client Side Applications with rich features, the programmers used to dependon various libraries to add features such as Media, UI controls, Web, 2D and 3D, etc.JavaFX includes all these features in a single library. In addition to these, the developerscan also access the existing features of a Java library such as Swings.JavaFX provides a rich set of graphics and media API’s and it leverages the modernGraphical Processing Unit through hardware accelerated graphics. JavaFX also providesinterfaces using which developers can combine graphics animation and UI control.One can use JavaFX with JVM based technologies such as Java, Groovy and JRuby. Ifdevelopers opt for JavaFX, there is no need to learn additional technologies, as priorknowledge of any of the above-mentioned technologies will be good enough to developRIA’s using JavaFX.Features of JavaFXFollowing are some of the important features of JavaFX – Written in Java: The JavaFX library is written in Java and is available for thelanguages that can be executed on a JVM, which include – Java, Groovy andJRuby. These JavaFX applications are also platform independent. FXML: JavaFX features a language known as FXML, which is a HTML like declarativemarkup language. The sole purpose of this language is to define a user Interface. Scene Builder: JavaFX provides an application named Scene Builder. Onintegrating this application in IDE’s such as Eclipse and NetBeans, the users canaccess a drag and drop design interface, which is used to develop FXML applications(just like Swing Drag & Drop and DreamWeaver Applications). Swing Interoperability: In a JavaFX application, you can embed Swing contentusing the Swing Node class. Similarly, you can update the existing Swingapplications with JavaFX features like embedded web content and rich graphicsmedia. Built-in UI controls: JavaFX library caters UI controls using which we can developa full-featured application.2

JavaFX CSS like Styling: JavaFX provides a CSS like styling. By using this, you canimprove the design of your application with a simple knowledge of CSS. Canvas and Printing API: JavaFX provides Canvas, an immediate mode style ofrendering API. Within the package javafx.scene.canvas it holds a set of classesfor canvas, using which we can draw directly within an area of the JavaFX scene.JavaFX also provides classes for Printing purposes in the package javafx.print. Rich set of API’s: JavaFX library provides a rich set of API’s to develop GUIapplications, 2D and 3D graphics, etc. This set of API’s also includes capabilities ofJava platform. Therefore, using this API, you can access the features of Javalanguages such as Generics, Annotations, Multithreading, and Lambda Expressions.The traditional Java Collections library was enhanced and concepts like observablelists and maps were included in it. Using these, the users can observe the changesin the data models. Integrated Graphics library: JavaFX provides classes for 2d and 3d graphics. Graphics pipeline: JavaFX supports graphics based on the Hardware-acceleratedgraphics pipeline known as Prism. When used with a supported Graphic Card orGPU it offers smooth graphics. In case the system does not support graphic cardthen prism defaults to the software rendering stack.History of JavaFXJavaFX was originally developed by Chris Oliver, when he was working for a companynamed See

CSS like Styling: JavaFX provides a CSS like styling. By using this, you can improve the design of your application with a simple knowledge of CSS. Canvas and Printing API: JavaFX provides Canvas, an immediate mode style of rendering API. Within the package javafx.scene.canvas it holds a set of classes