2012/10/12

TWaver With JavaFX

We have paid little attention on JavaFX since JavaFX Script was not provided in Oracle. However, now we have found that in fact Oracle has attached much importance on it, for we have discovered by chance that JavaFX has developed to version 2.2 and been binding to JDK7.、
Recently I have seen someone putting up questions in our forum and asked whether there is JavaFX version in TWaver, so I have had a browse on Oracle’s website and found that it is applicable to embed JavaFX into Swing. It is quite simple and convenient to do that so I have written a Demo right away.

Effects

An Analysis on the code
There is a typical Network in the middle of the window: there is a JavaFX container in the top and bottom respectively. The top one includes a Slider, sliding which can change the RenderColor of the selected Node in TWaver. Some texts are involved in the bottom container. The name and the location of a node will be shown on the Text component when selecting and dragging the node. 

In this Demo, Swing is still the application architecture. JavaFX has provided a component named JFXpanel which can be embedded into a Swing container and JavaFX components can be put into the JFXPanel. However, attention need to be paid to the difference in the thread of Swing and JavaFX(the official text says:” JavaFX data should be accessed only on the JavaFX User thread.” and “Swing data should be changed only on the EDT.” ). Therefore, if you want to change the data of JavaFX in Swing, the encapsulation of Platform#runLater is suggested.

The code for the Text component of JavaFX that is changed when dragging a node in this example:

network.getDataBox().addElementPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if("location".equals(evt.getPropertyName())){
                    Element element= (Element) evt.getSource();
                    final String name=element.getName();
                    final double x=element.getX();
                    final double y=element.getY();
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            nameText.setText(name);
                            locationText.setText("X:"+x+"   Y:"+y);
                        }
                    });
                }
            }
        });
Likewise, if you want to change the Swing data in JavaFX, the encapsulation of SwingUtilities#invokeLater is suggested.
The following is the code for the Render Color of the selected node which is changed when dragging the Slider in this example:
slider.valueProperty().addListener(new ChangeListener<Number>() {
            public void changed(ObservableValue<? extends Number> ov,
                                Number old_val,final Number new_val) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        Iterator it=network.getSelectionModel().selection();
                        while (it.hasNext()){
                            Element element= (Element) it.next();
                            element.putRenderColor(new java.awt.Color(new_val.intValue()));
                        }
                    }
                });
            }
        });
 All codes:

public class Test1 {
    static TNetwork network;
    static Text nameText;
    static Text locationText;
    static  Slider slider;
    private static void initAndShowGUI() {
        initNetwork();
        JFrame frame = new JFrame("TWaver With JavaFX");
        frame.getContentPane().setLayout(new BorderLayout());

        frame.setSize(500, 300);
        frame.setVisible(true);
        frame.getContentPane().add(network);

        final JFXPanel controlFXPanel = new JFXPanel();
        frame.add(controlFXPanel,BorderLayout.NORTH);

        final JFXPanel statusFXPanel = new JFXPanel();
        frame.add(statusFXPanel,BorderLayout.SOUTH);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        network.getDataBox().addElementPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if("location".equals(evt.getPropertyName())){
                    Element element= (Element) evt.getSource();
                    final String name=element.getName();
                    final double x=element.getX();
                    final double y=element.getY();
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            nameText.setText(name);
                            locationText.setText("X:"+x+"   Y:"+y);
                        }
                    });
                }
            }
        });
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                initFX(statusFXPanel,controlFXPanel);
            }
        });
    }
    private static void initNetwork(){
        network=new TNetwork();
        twaver.Node node1=new Node();
        node1.setName("node1");
        twaver.Node node2=new Node();
        node2.setLocation(200,100);
        node2.setName("node2");
        Link link=new Link(node1,node2);
        network.getDataBox().addElement(node1);
        network.getDataBox().addElement(node2);
        network.getDataBox().addElement(link);

    }
    private static void initFX(JFXPanel statusFXPanel,JFXPanel controlFXPanel) {
        Scene scene = createStatusScene();
        statusFXPanel.setScene(scene);
        statusFXPanel.validate();

        Scene controlScene=createControlScene();
        controlFXPanel.setScene(controlScene);
        controlFXPanel.validate();

    }
    private static Scene createControlScene(){
        Group  root  =  new  Group();
        Scene  scene  =  new  Scene(root, Color.ALICEBLUE);
        HBox hbox=new HBox();
        slider =new Slider(0,16777215,0);
        slider.setPrefHeight(30);
        slider.setPrefWidth(300);
        slider.valueProperty().addListener(new ChangeListener<Number>() {
            public void changed(ObservableValue<? extends Number> ov,
                                Number old_val, final Number new_val) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        Iterator it = network.getSelectionModel().selection();
                        while (it.hasNext()) {
                            Element element = (Element) it.next();
                            element.putRenderColor(new java.awt.Color(new_val.intValue()));
                        }
                    }
                });
            }
        });
        Label label=new Label("颜色:");
        label.setPrefHeight(30);
        hbox.getChildren().add(label);
        hbox.getChildren().add(slider);
        root.getChildren().add(hbox);
        return scene;
    }
    private static Scene createStatusScene() {
        Group  root  =  new  Group();
        Scene  scene  =  new  Scene(root, Color.ALICEBLUE);
        VBox vbox=new VBox();
        vbox.setPrefWidth(300);
        vbox.setPrefHeight(20);
        HBox hbox=new HBox();
        nameText=new Text();
        hbox.getChildren().add(new Label("  Name:"));
        hbox.getChildren().add(nameText);
        hbox.getChildren().add(new Text("   "));
        locationText=new Text();
        hbox.getChildren().add(new Label("Location:"));
        hbox.getChildren().add(locationText);
        vbox.getChildren().add(hbox);
        root.getChildren().add(vbox);
        return (scene);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                initAndShowGUI();
            }
        });
    }
}

 

No comments:

Post a Comment