Is it possible to make JavaFX web applet?

≯℡__Kan透↙ 提交于 2020-01-11 04:47:12

问题


I like old Java applets. But because I really like the way JFX works, I want write some games using it (or even game making system, who knows?), but I'd like to be able to post them on my website. How would one go about doing this?


回答1:


Yes, you should be able to embed JavaFX in your web page:

http://docs.oracle.com/javase/8/docs/technotes/guides/deploy/deployment_toolkit.html#BABJHEJA

http://docs.oracle.com/javase/8/javase-clienttechnologies.htm




回答2:


Yes, you can embed a JavaFX GUI into the Swing-based JApplet. You can do this by using the JFXPanel - it is essentially an adaptor between Swing and JavaFX panels.

Complete example:
The FXApplet class that sets-up the JavaFX GUI:

public class FXApplet extends JApplet {
    protected Scene scene;
    protected Group root;

    @Override
    public final void init() { // This method is invoked when applet is loaded
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                initSwing();
            }
        });
    }

    private void initSwing() { // This method is invoked on Swing thread
        final JFXPanel fxPanel = new JFXPanel();
        add(fxPanel);

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                initFX(fxPanel);
                initApplet();
            }
        });
    }

    private void initFX(JFXPanel fxPanel) { // This method is invoked on JavaFX thread
        root = new Group();
        scene = new Scene(root);
        fxPanel.setScene(scene);
    }

    public void initApplet() {
        // Add custom initialization code here
    }
}

And a test implementation for it:

public class MyFXApplet extends FXApplet {
    // protected fields scene & root are available

    @Override
    public void initApplet() {
        // this method is called once applet has been loaded & JavaFX has been set-up

        Label label = new Label("Hello World!");
        root.getChildren().add(label);

        Rectangle r = new Rectangle(25,25,250,250);
        r.setFill(Color.BLUE);
        root.getChildren().add(r);
    }
}

Alternatively, you can use the FXApplet gist, which also includes some documentation.



来源:https://stackoverflow.com/questions/8566818/is-it-possible-to-make-javafx-web-applet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!