Code for Bioinformatics short workshop, University of Dundee, 31st May 2019
- To give a flavour of cross-compilation of Java Swing to Javascript in action
- Eclipse for Java, version 4.8 or later
- git installed (or Eclipse git plugin)
- Java 8 or later
To run as Java:
- Clone this repository
(https://github.com/gmungoc/SwingJSDemo.git)
- Import into Eclipse (Import from Git) as a Java project
- Run class
HelloWorld.java
as a Java application and explore its (very simple) functionality
To translate ('transpile') from Swing to Javascript:
- Right-click on
build-site.xml
and 'Run as Ant Build' - this unzipsswingjs/SwingJS-site.zip
to/site
- Refresh the workspace - you should now see a new folder
site
- Copy file
swingjs\net.sf.j2s.core.jar
to thedropins
subfolder of your Eclipse installation - Restart Eclipse to pick up the 'Compiler Participant' plugin
- if this doesn't work first time, you may need to restart Eclipse with the
-clean
flag (once only)
- if this doesn't work first time, you may need to restart Eclipse with the
- Select
Project | Clean...
to recompile the code- notice file
.j2s
- this is needed to enable JavaToScript 'transpilation'
- notice file
- Refresh project: you should now see a file
site/swing_demo_HelloWorld.html
- the transpiler generates an html launch page for each Java class with a
main
method
- the transpiler generates an html launch page for each Java class with a
- Open this file in a browser (e.g. browse to file, double-click, or right-click and Open With | System Editor)
- ABC - Anything But Chrome (as Chrome by default doesn't allow local file access)
- You should now see the code running as Javascript
- Try making a change to the Java code; it should recompile (as Java) and transpile (to HelloWorld.js)
- Reload the web page to see the changes
- Add a main method to
HelloWorld2
, consisting ofnew HelloWorld2().display()
, and run it- tip: type ma then Ctrl-space for the main method template
- experiment with dragging the mouse, with or without the Shift key pressed
- A second
.html
file should have been generated, forHelloWorld2
. Try this in the browser. This demonstrates drawing on a SwingJS Graphics context. - Transpilation is not just for GUI applications!
- run PrimeNumberFinder (as Java)
- run PrimeNumberFinder (as html/Javascript)
- to fix the error (pass in an argument): add
args: "100",
to theInfo
block of the html
SwingJS is currently maintained at https://github.com/BobHanson/java2script, a fork of https://github.com/java2script/java2script.