The problem solved: an elegant solution to link the languages in Java

Generator QR Code ISO/IEC18004 in Java The problem solved: an elegant solution to link the languages

11.3.2 The problem solved: an elegant solution to link the languages
Generate QR-Code In Java
Using Barcode creator for Java Control to generate, create QR Code image in Java applications.
www.OnBarcode.com
Recognizing QR In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
If we encode the interactions between our two languages into a Java interface and get the JavaFX Script code to implement (or extend, to use the JFX parlance) this interface, we have a guaranteed Java-compatible bridge between the two languages that Java can exploit to communicate with the JavaFX Script software. Listing 11.3 shows the Java interface created for our game engine. It has only two methods: the first is used to fetch the control panel user interface as a Java
Barcode Creation In Java
Using Barcode creator for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
Encode Barcode In Java
Using Barcode creator for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
Best of both worlds: using JavaFX from Java
GTIN - 128 Drawer In Java
Using Barcode drawer for Java Control to generate, create EAN128 image in Java applications.
www.OnBarcode.com
Drawing Data Matrix ECC200 In Java
Using Barcode creator for Java Control to generate, create ECC200 image in Java applications.
www.OnBarcode.com
Skinning cats
Printing European Article Number 13 In Java
Using Barcode encoder for Java Control to generate, create EAN-13 Supplement 5 image in Java applications.
www.OnBarcode.com
Print UPCE In Java
Using Barcode generator for Java Control to generate, create GTIN - 12 image in Java applications.
www.OnBarcode.com
We can also form a bridge by subclassing a Java class. However, interfaces, with their lack of inherited behavior, generally provide a cleaner (sharper, less complicated) coupling. But, as the saying goes, there s more than one way to skin a cat. Choose the way that makes sense to you and your current project.
QR Code ISO/IEC18004 Encoder In None
Using Barcode maker for Office Word Control to generate, create QR-Code image in Office Word applications.
www.OnBarcode.com
Scanning QR Code ISO/IEC18004 In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Swing compatible object (a JComponent), and the second is used to interact with the user interface.
Reading PDF 417 In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Code 39 Extended Recognizer In .NET Framework
Using Barcode scanner for VS .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Listing 11.3 ControlPanel.java
Code 39 Extended Reader In None
Using Barcode decoder for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Draw QR Code In None
Using Barcode maker for Online Control to generate, create QR Code image in Online applications.
www.OnBarcode.com
package jfxia.chapter11; import javax.swing.JComponent; public interface ControlPanel { public JComponent getUI(); public void setTitle(String s); }
Reading Barcode In Visual Basic .NET
Using Barcode Control SDK for .NET framework Control to generate, create, read, scan barcode image in VS .NET applications.
www.OnBarcode.com
Paint PDF417 In None
Using Barcode generator for Word Control to generate, create PDF 417 image in Office Word applications.
www.OnBarcode.com
Fetch Javacompatible UI Interact with UI
Encoding PDF-417 2d Barcode In Visual Basic .NET
Using Barcode drawer for VS .NET Control to generate, create PDF-417 2d barcode image in VS .NET applications.
www.OnBarcode.com
Code 128 Code Set B Printer In Visual Basic .NET
Using Barcode drawer for Visual Studio .NET Control to generate, create ANSI/AIM Code 128 image in VS .NET applications.
www.OnBarcode.com
Now that you re familiar with the interface and its methods, let s see them in action. Listing 11.4 shows fragments of the Game class, written in Java, displaying the first part of how to hook a JavaFX-created user interface control into Java Swing.
Scan Data Matrix ECC200 In Visual C#
Using Barcode reader for VS .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
PDF417 Reader In Visual C#.NET
Using Barcode reader for .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Listing 11.4 Game.java (part 1): adding JavaFX UIs to Java code
import javax.script.ScriptEngineManager; import com.sun.javafx.api.JavaFXScriptEngine; // ... private ControlPanel ctrlPan; // ... ctrlPan = getJavaFX(); ctrlPan.setTitle(state.getCurrentRoomTitle()); JPanel pan = new JPanel(new BorderLayout()); pan.add(mapView,BorderLayout.CENTER); pan.add(ctrlPan.getUI(),BorderLayout.SOUTH); //Part 2 is listing 11.5
Imports required
Control panel reference
Create and set up Hook into Java Swing
First we create a class variable to hold our JavaFX object reference, using the ControlPanel interface we defined as a bridge. Then comes the meaty part: to plug the two UIs together we use the method getJavaFX(), which fetches the ControlPanel (details in listing part 2, so be patient!). We call a method on it to set the initial room name; then we use the ControlPanel.getUI() interface method to pull a Swing-compatible JComponent from the JavaFX Script code and add it into the
Adding FX to Java
southern position of a BorderLayout panel. (The other component, added to the center position, is the main game view, in case you re wondering.) Exactly how the JavaFX object is turned into a Java object is hidden behind the mysterious getJavaFX() method, which will surrender its secrets next.
11.3.3 Fetching the JavaFX Script object from within Java
The code that creates the ControlPanel class will look very familiar. It s just a variation on the JSR 223 scripting engine code you saw earlier in listing 11.2. Listing 11.5 is the second half of our Game class fragments. It shows the method getJavaFX() using the JavaFX Script scripting engine.
Listing 11.5 Game.java (part 2): adding JavaFX user interfaces to Java code
private ControlPanel getJavaFX() { ScriptEngineManager manager = new ScriptEngineManager(); JavaFXScriptEngine jfxScriptEngine = (JavaFXScriptEngine)manager.getEngineByName ("javafx"); try { return (ControlPanel)jfxScriptEngine.eval ( "import jfxia.chapter11.jfx.ControlPanelImpl;\n"+ "return ControlPanelImpl{};" ); } catch(Exception e) { e.printStackTrace(); return null; } }
Since we re going to use the scripting engine only once, we lump all the code to initialize the engine and call the script into one place. The script simply returns a declaratively created JavaFX Script object of type ControlPanelImpl, which (you can t tell from this listing, but won t be shocked to learn) extends our ControlPanel interface. The object returned by the script provides the return value for the getJavaFX() method. Figure 11.5 demonstrates the full relationship. Once the ControlPanelImpl object is created, Java and JavaFX Script commuJava JavaFX Script nicate only via a common interface. JavaFX Script s respect for Java interfaces means ControlPanel.java this is a safe way to link the two. However, getUI() getUI() setTitle() JavaFX Script s lack of support for construcsetTitle() tors means the ControlPanelImpl object ControlPanelImpl.fx Game.java must still be created using JSR 223. Let s look at ControlPanelImpl. Because Figure 11.5 Java s Game class and the it s written in JavaFX Script, the code is preJavaFX Script ControlPanelImpl.fx sented in its entirety (for those suffering class communicate via a Java interface, ControlPanel.java. JavaFX withdrawal symptoms) in listing 11.6.
Copyright © OnBarcode.com . All rights reserved.