We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpIntegration › Starting a PApplet in a JFrame
Page Index Toggle Pages: 1
Starting a PApplet in a JFrame (Read 1936 times)
Starting a PApplet in a JFrame
Mar 7th, 2007, 6:43am
 
I've been using the attached style for creating an application using PApplets .. its basically the core of the main() in PApplet and follows the PApplet javadocs reasonably well.

I'm using this to work with several of the JVM languages: JRuby, Groovy, Rhino/Javascript, and Jython.  See http://backspaces.net/ for an example of use of this with Jython.

Its worked fine so far except for JRuby where I get an exception with init() where it gets the AppletContext.

So I'm wondering if there is another style that I'd be better off using?  I realize this is a weird use of Processing, but thought I'd check before I get into a long dialog with the JRuby folks.

Owen

JFrame frame = new JFrame("Embedded PApplet");
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PApplet embed = new MyPAppletSubclass();
frame.add(embed);
embed.init();
while (embed.defaultSize&&!embed.finished)
 try {Thread.sleep(5);} catch (Exception e) {}
frame.pack();
frame.setVisible(true);

Re: Starting a PApplet in a JFrame
Reply #1 - Mar 7th, 2007, 3:22pm
 
where PApplet gets the AppletContext? or jruby? if p5, what error is thrown? it should throw a NullPointerException but then move on from there..

you basically need to set up a fake AppletContext in which to run the applet. the AppletContext is usually provided by the applet viewer, and handles file paths and getting URLs and whatnot.
Re: Starting a PApplet in a JFrame
Reply #2 - Mar 7th, 2007, 5:00pm
 
Hi Ben.

The problem apparently is within the JRuby java support, nothing at all to do with Processing.  I was just fishing for a workaround, and making sure I understood the Processing side of things before getting back to the JRuby community.  I'm looking at the Processing code and see no problems .. just an exception that should be caught and then we press on.  Here are the gory details:

For completeness, here's a version with jython that works just fine:
 http://backspaces.net/hacks/30/processing-with-jython

While here's a version with groovy and rhino:
 http://backspaces.net/files/test.groovy
 http://backspaces.net/files/RandBoxes.js

Again, both work fine.

Finally, here's the jruby version that doesn't work yet:
 http://backspaces.net/files/RandBoxes.rb

The error is:
Applet.java:181:in `java.applet.Applet.getAppletContext': java.lang.NullPointerException: null (NativeException)
       from null:-1:in `processing.core.PApplet$Proxy0.__super$getAppletContext'
       from NativeMethodAccessorImpl.java:-2:in `sun.reflect.NativeMethodAccessorImpl.invoke0'
       from NativeMethodAccessorImpl.java:39:in `sun.reflect.NativeMethodAccessorImpl.invoke'
       from DelegatingMethodAccessorImpl.java:25:in `sun.reflect.DelegatingMethodAccessorImpl.invoke'
       from Method.java:585:in `java.lang.reflect.Method.invoke'
       from JavaMethod.java:204:in `org.jruby.javasupport.JavaMethod.invokeWithExceptionHandling'
..... and so on
Re: Starting a PApplet in a JFrame
Reply #3 - Mar 8th, 2007, 5:13am
 
I put together a response to the JRuby folks about the problem.  See:
http://www.nabble.com/Re%3A-Java-subclasses-in-Ruby-p9368272.html

The whole thread is:
http://www.nabble.com/Java-subclasses-in-Ruby-tf3344729.html#a9368272

I don't see any way Processing could have easily done otherwise: class Applet should be more reasonable about their use of the "stub" class.

Anyway, we'll see if the JRuby folks can avoid having the exception cause them to fail.

Owen
Re: Starting a PApplet in a JFrame
Reply #4 - Mar 9th, 2007, 5:08pm
 
yeah, so it's a matter of expectation.. normally if the appletcontext is null, a NPE is thrown inside init() and we can move on. or we're not following something properly about how RuntimeExceptions are passed up (i.e. if they fail at a certain distance).

it appears that the NPE is instead failing inside the jruby code (in the stub.getAppletContext() line b/c stub is null) but that isn't propogating up to p5 as an NPE--it's halting inside jruby.

fyi, since an NPE is a RuntimeException, the getAppletContext() method need not declare it in the 'throws' declaration.
Re: Starting a PApplet in a JFrame
Reply #5 - Mar 13th, 2007, 4:17pm
 
fry wrote on Mar 9th, 2007, 5:08pm:
yeah, so it's a matter of expectation.. normally if the appletcontext is null, a NPE is thrown inside init() and we can move on. or we're not following something properly about how RuntimeExceptions are passed up (i.e. if they fail at a certain distance).

it appears that the NPE is instead failing inside the jruby code (in the stub.getAppletContext() line b/c stub is null) but that isn't propogating up to p5 as an NPE--it's halting inside jruby.

fyi, since an NPE is a RuntimeException, the getAppletContext() method need not declare it in the 'throws' declaration.


Yup, that's it.  I've rebuilt a version of Processing's core package for the JRuby folks along with a test harness.  Not sure if they'll get time to look at it.  Here's the thread:
http://www.nabble.com/Java-subclasses-in-Ruby-tf3344729.html

Just as a FYI: I'm doing all this with the hopes that one of the "agile" JVM languages will be able to work modestly well with Processing.  We've done Rhino, Groovy, Jython and JRuby.  I personally find Ruby the most compelling language, but the performance of all of these are quite poor, alas.

One nice side effect is that you can run Processing in an interpreter for quick experiments.  I believe all but JRuby, because of the NPE, would support that .. I've done it a few times (trying to figure out how the camera works) but I forget which system it was!  I'm sure I can figure it out if its of interest.

So for all the "syntactic salt" of Java, its still so much better in terms of speed and lack of ambiguities, that I'm starting to like it again.  I'm tempted to try Antlr or Javacc to build a translator from a Kindler, Gentler Java to standard salty Java.
Page Index Toggle Pages: 1