JavaFX Programowanie Obiektowe Mateusz Cicheński Is JavaFX replacing Swing as the new client UI library for Java SE? Yes. http://www.oracle.com/technetwork/java/javafx/overview/faq-1446554.html
Zastosowania Krótka historia Alternatywy 4 Architektura Projektowanie interfejsu Witaj świecie! Modyfikowanie wyglądu To samo, ale inaczej Animacja i efekty wizualne Przydatne odnośniki Plan zajęć
Maj 2007 pierwsza informacja o JavaFX Maj 2008 plany uruchomienia JavaFX w przeglądarce i komputerze użytkownika 4 grudnia 2008 JavaFX 1.0, JavaFX Script, wsparcie dla systemów Windows i Macintosh 12 lutego 2009 JavaFX 1.1 dla urządzeń mobilnych 2 czerwca 2009 JavaFX 1.2, dodanie CSS, wykresów, obsługa I/O, poprawki związane z wydajnością, wsparcie dla systemów Linux i Solaris 22 kwietnia 2010 JavaFX 1.3 10 października 2011 JavaFX 2.0, usunięcie JavaFX Script, usunięcie JavaFX Mobile, wprowadzenie FXML, tylko Windows 27 kwietnia 2012 JavaFX 2.1, wsparcie dla Macintosh 14 sierpnia 2012 JavaFX 2.2, wsparcie dla Linuxa, element Java SE7u6 Historia 27 stycznia 2010 przejęcie Sun przez Oracle
AWT wrapper na komponenty systemowe Swing rozszerzenie AWT o bogatsze elementy SWT IBM, biblioteka zewnętrzna QT Jambi wrapper na QT Alternatywy 4
Prism - renderowanie grafiki w miarę możliwość sprzętowo! Glass Windowing Toolkit systemowa kolejka zdarzeń Media Engine wsparcie dla multimediów Web Engine silnik przeglądarki internetowej, bazuje na WebKit Trzy główne wątki aplikacja, Prism, Media Architektura
Witaj świecie!
package helloworld; import javafx.application.application; import javafx.event.actionevent; import javafx.event.eventhandler; import javafx.scene.scene; import javafx.scene.control.button; import javafx.scene.layout.stackpane; import javafx.stage.stage; Witaj świecie kod
public class HelloWorld extends Application { public static void main(string[] args) { launch(args); @Override public void start(stage primarystage) { Witaj świecie kod c.d.
primarystage.settitle("witaj świecie!!"); Button btn = new Button(); btn.settext("powiedz 'Witaj świecie!'"); btn.setonaction(new EventHandler<ActionEvent>() { @Override public void handle(actionevent event) { System.out.println("Witaj świecie!"); ); StackPane root = new StackPane(); root.getchildren().add(btn); primarystage.setscene(new Scene(root, 300, 250)); primarystage.show(); Witaj świecie kod c.d.
Witaj świecie! jeszcze raz :)
StackPane root = new StackPane(); root.getchildren().add(btn); Scene scene = new Scene(root, 300, 250); scene.getstylesheets().add(helloworld.class.getresource("helloworld.css").toexternalform()); primarystage.setscene(scene); primarystage.show(); Modyfikowanie wyglądu
.root { -fx-background-color: #000000; Modyfikowanie wyglądu c.d.
Label label = new Label("Powitanie świata"); VBox box = new VBox(); box.getchildren().add(label); box.getchildren().add(btn); box.setalignment(pos.center); Scene scene = new Scene(box, 300, 250); Modyfikowanie wyglądu c.d.
.root { -fx-background-color: #000000;.label { -fx-text-fill: greenyellow; -fx-font-weight: bold; -fx-font-size: 22px; Modyfikowanie wyglądu c.d.
btn.setid("big-red-button"); #big-red-button { -fx-background-color: linear-gradient(#ff5400, #be1d00); -fx-background-radius: 30; -fx-background-insets: 0; -fx-text-fill: white; -fx-padding: 15 30 15 30; -fx-font-family: "Helvetica"; -fx-font-size: 18px; #big-red-button:hover { -fx-background-color: #ecebe9, rgba(0,0,0,0.05), linear-gradient(#dcca8a, #c7a740), linear-gradient(#f9f2d6 0%, #f4e5bc 20%, #e6c75d 80%, #e2c045 100%), linear-gradient(#f6ebbe, #e6c34d); -fx-background-insets: 0,9 9 8 9,9,10,11; -fx-background-radius: 50; -fx-text-fill: #311c09; -fx-effect: innershadow( three-pass-box, rgba(0,0,0,0.1), 2, 0.0, 0, 1); #big-red-button:hover Text { -fx-effect: dropshadow(one-pass-box, rgba(255,255,255,0.5), 0, 0.0, 0, 1); Modyfikowanie wyglądu c.d.
<?xml version="1.0" encoding="utf-8"?> <?import javafx.scene.layout.*?> <?import javafx.scene.control.*?> <VBox fx:controller="helloworld.helloworld" xmlns:fx="http://javafx.com/fxml" alignment="center"> <Label text="powitanie świata" /> <Button text="powiedz 'Witaj świecie!'" id="big-red-button" onaction="#sayhelloworld"/> </VBox> To samo, ale inaczej
package helloworld; import java.io.ioexception;( ) public class HelloWorld extends Application{ public static void main(string[] args) { launch(args); @FXML protected void sayhelloworld(actionevent event) { System.out.println("Witaj świecie!"); @Override public void start(stage primarystage) { primarystage.settitle("witaj świecie!!"); Parent root = null; try { root = FXMLLoader.load(HelloWorld.class.getResource("HelloWorld.fxml")); catch (IOException e) { e.printstacktrace(); Scene scene = new Scene(root, 300, 250); scene.getstylesheets().add(helloworld.class.getresource("helloworld.css").toexternalform()); primarystage.setscene(scene); primarystage.show(); To samo, ale inaczej c.d.
<?xml version="1.0" encoding="utf-8"?> <?import javafx.scene.layout.*?> <?import javafx.scene.control.*?> <VBox fx:controller="helloworld.helloworld" xmlns:fx="http://javafx.com/fxml" alignment="center"> <Label text="powitanie świata" fx:id="mylabel"/> <Button text="powiedz 'Witaj świecie!'" id="big-red-button" onaction="#sayhelloworld"/> </VBox> To samo, ale inaczej cz. 2
@FXML private Label mylabel; @FXML protected void sayhelloworld(actionevent event) { System.out.println("Witaj świecie!"); mylabel.settext("świat wita Ciebie"); To samo, ale inaczej cz. 2 c.d.
<?xml version="1.0" encoding="utf-8"?> <?import javafx.scene.layout.*?> <?import javafx.scene.control.*?> <?language javascript?> <VBox xmlns:fx="http://javafx.com/fxml" alignment="center" fx:controller="helloworld.helloworld"> <fx:script> function sayhelloworld() { mylabel.settext("świat wita Ciebie"); </fx:script> <Label text="powitanie świata" fx:id="mylabel"/> <Button text="powiedz 'Witaj świecie!'" id="big-red-button" onaction="sayhelloworld(event)"/> </VBox> To samo, ale inaczej cz. 3
<?xml version="1.0" encoding="utf-8"?> <?import javafx.scene.layout.*?> <?import javafx.scene.control.*?> <?language javascript?> <VBox xmlns:fx="http://javafx.com/fxml" alignment="center" fx:controller="helloworld.helloworld" stylesheets="helloworld/helloworld.css" > <fx:script> function sayhelloworld() { mylabel.settext("świat wita Ciebie"); </fx:script> <Label text="powitanie świata" fx:id="mylabel"/> <Button text="powiedz 'Witaj świecie!'" id="big-red-button" onaction="sayhelloworld(event)"/> </VBox> To samo, ale inaczej cz. 4
Group circles = new Group(); for (int i = 0; i < 30; i++) { Circle circle = new Circle(150, Color.web("white", 0.05)); circle.setstroketype(stroketype.outside); circle.setstroke(color.web("white", 0.16)); circle.setstrokewidth(4); circle.settranslatex(random() * 800); circle.settranslatey(random() * 600); circles.getchildren().add(circle); root.getchildren().add(circles); Parent circles Circle 1 Circle 30 Efekty wizualne
Group circles = new Group(); for (int i = 0; i < 30; i++) { Circle circle = new Circle(150, Color.web("white", 0.05)); circle.setstroketype(stroketype.outside); circle.setstroke(color.web("white", 0.16)); circle.setstrokewidth(4); circle.settranslatex(random() * 800); circle.settranslatey(random() * 600); circles.getchildren().add(circle); circles.seteffect(new BoxBlur(10, 10, 3)); root.getchildren().add(circles); Efekty wizualne c.d.
Rectangle colors = new Rectangle(scene.getWidth(), scene.getheight(), new LinearGradient(0f, 1f, 1f, 0f, true, CycleMethod.NO_CYCLE, new Stop[] { new Stop(0, Color.web("#f8bd55")), new Stop(0.14, Color.web("#c0fe56")), new Stop(0.28, Color.web("#5dfbc1")), new Stop(0.43, Color.web("#64c2f8")), new Stop(0.57, Color.web("#be4af7")), new Stop(0.71, Color.web("#ed5fc2")), new Stop(0.85, Color.web("#ef504c")), new Stop(1, Color.web("#f2660f")), )); colors.widthproperty().bind(scene.widthproperty()); colors.heightproperty().bind(scene.heightproperty()); root.getchildren().add(colors); Parent colors circles Circle 1 Circle 30 Efekty wizualne c.d.
Group blendmodegroup = new Group(new Group(new Rectangle( scene.getwidth(), scene.getheight(), Color.BLACK), circles), colors); colors.setblendmode(blendmode.overlay); root.getchildren().add(blendmodegroup); Parent blendmodegroup colors Group Rectangle circles Circle 1 Circle 30 Efekty wizualne c.d.
Timeline timeline = new Timeline(); for (Node circle: circles.getchildren()) { timeline.getkeyframes().addall( new KeyFrame(Duration.ZERO, new KeyValue(circle.translateXProperty(), circle.gettranslatex()), new KeyValue(circle.translateYProperty(), circle.gettranslatey()) ), new KeyFrame(new Duration(5000), new KeyValue(circle.translateXProperty(), 400), new KeyValue(circle.translateYProperty(), 300) ), new KeyFrame(new Duration(10000), new KeyValue(circle.translateXProperty(), circle.gettranslatex()), new KeyValue(circle.translateYProperty(), circle.gettranslatey()) ), new KeyFrame(new Duration(12500), new KeyValue(circle.translateXProperty(), random() * 800), new KeyValue(circle.translateYProperty(), random() * 600) ) ); timeline.play(); Animacja
http://docs.oracle.com/javafx/ http://docs.oracle.com/javafx/2/api/index.html http://docs.oracle.com/javafx/2/api/javafx/scene /doc-files/cssref.html http://fxexperience.com/ http://wiki.netbeans.org/javafx http://www.oracle.com/technetwork/java/javafx/ samples/index.html http://efxclipse.org/ Odnośniki