BobHanson / java2script

Java2Script provides an Eclipse Java to JavaScript transpiler, with a nearly complete implementation of the Java Virtual Machine with AWT and Swing in JavaScript, with simple, automated parallel creation of both class files and js files. To date, over 600 applets have been converted.
https://chemapps.stolaf.edu/swingjs/examples.htm
Other
24 stars 11 forks source link

js can't load image #273

Closed richcanvas closed 6 months ago

richcanvas commented 6 months ago

hello, i have the following java code and can load and draw image: Graphics2D g2d = (Graphics2D) g; Image image=Toolkit.getDefaultToolkit().getImage(Main.class.getResource("/java2d/images/duke.gif")); g2d.drawImage(image, 9, 9, null);

but the generated js report error: j2sApplet exec testApplet start applet null transpiler was unknown now 5.0.1-v2 for java.lang.Class transpiler was 5.0.1-v2 now 3.3.1-v7 for demo.Main transpiler was 3.3.1-v7 now 5.0.1-v2 for java.util.Hashtable swingjs.api.Interface creating instance of javajs.util.AjaxURLStreamHandlerFactory JSApp get parameter: language = null swingjs.JSUtil initialized;debug=false JSApp get parameter: name = testApplet JSApp get parameter: syncId = 777136185134673 JSApp initialized JSAppletViewer initializing2 JSToolkit initialized JSGraphicsConfiguration initialized swingjs.api.Interface creating instance of swingjs.JSGraphicsEnvironment JSGraphicsEnvironment initialized swingjs.api.Interface creating instance of swingjs.JSScreenDevice JSScreenDevice initialized JSAppletViewer thread run1 mode=0 status=0 JSAppletViewer thread run1 mode=1 status=1 JSAppletViewer runloader JSApp get parameter: main = demo.Main swingjs.api.Interface creating instance of swingjs.JSDummyApplet swingjs.api.Interface creating instance of swingjs.plaf.HTML5LookAndFeel testApplet loaded JSAppletViewer thread run1 mode=1 status=76 testApplet running demo.Main Running main(args) in class demo.Main swingjs.api.Interface creating instance of swingjs.JSGraphicsConfiguration JSGraphicsConfiguration initialized JSGraphics2D pixelRatio is 2.0 JSToolkit.dispatch$I(0): TypeError: Cannot read properties of null (reading '_streamData')

0 function() at Clazz._getStackTrace (http://127.0.0.1:8900/swingjs/swingjs2.js:15972:8)

... at ff (swingjs/j2s/swingjs/JSToolkit.js:207:23) at TypeError.getStackTrace$.ReferenceError.getStackTrace$ (http://127.0.0.1:8900/swingjs/swingjs2.js:21178:106) see Clazz._stack TypeError: Cannot read properties of null (reading '_streamData') at Function.eval [as getAttachedStreamData$java_net_URL$Z] (swingjs/j2s/javajs/util/AjaxURLConnection.js:393:12) at Function.eval [as getURLInputStream$java_net_URL$Z] (swingjs/j2s/swingjs/JSUtil.js:364:17) at clazz.eval [as createImage$java_net_URL] (swingjs/j2s/swingjs/JSToolkit.js:345:14) at clazz.eval [as getImage$java_net_URL] (swingjs/j2s/swingjs/JSToolkit.js:316:13) at Object.eval [as paintComponent$java_awt_Graphics] (swingjs/j2s/demo/Main.js:24:39) at Object.eval (swingjs/j2s/javax/swing/JComponent.js:1450:6) at Object.eval [as paint$java_awt_Graphics] (swingjs/j2s/javax/swing/JComponent.js:190:44) at Object.eval [as 秘paint$java_awt_Graphics] (swingjs/j2s/java/awt/JSComponent.js:290:6) at clazz.eval [as paintChildren$java_awt_Graphics] (swingjs/j2s/javax/swing/JComponent.js:154:4) at clazz.eval [as paint$java_awt_Graphics] (swingjs/j2s/javax/swing/JComponent.js:196:6)

BobHanson commented 6 months ago

I suspect this is the issue that JavaScript needs a clock tick to finalize an image loading. I see this in dist/differences.txt:

image loading

So the trick here is to load them in a static {...} block and then WAIT until after applet initialization to utilize them. If the problem persists, add a SwingUtilities.invokeLater(()->{....}) runnable that starts your GUI.