I finally bit the bullet and set up a WordPress blog, having been reluctantly convinced that the ease of posting more than compensates for the disadvantages, which in my opinion are many, but I won’t go into them here, except to the extent that some of them are apparent from what follows.

I figured that I would start by moving a few older posts from my old blog.

Back when I was still teaching Java, I wrote a simple applet demo program that computed and displayed the evolution of one-dimensional cellular automata, using the 8-bit rule scheme described by Stephen Wolfram in A New Kind Of ScienceI described it in a blog post at the time, with a link to the Java source code and the applet running embedded at the end of the post.

Java applets were never a very satisfactory way of embedding functionality into a web page. There were always just too many ways for them to not work in a particular browser running a particular version of Java.

Now, however, it seems Oracle has raised its game — now applets don’t work in any browser. I discovered this when I went back to the original post and found that instead of the applet, what appears is a message that “Your security settings have blocked an untrusted application from running.”

“Run it anyway” is not offered as an option. (Clearly users are far too dumb to decide for themselves what sources they trust.)

As it turns out, there are only two ways to get it to run.

  1. On the client computer, the user can go into the Java Console and add the applet as an exception to the security settings (like any user is going to do that, even if it weren’t more complicated than the average space shuttle launch); or
  2. You can code-sign the applet, which requires getting a certificate from an approved provider (which is tedious and costs money), installing it in a keystore (not simple), modifying your code (the applet has to be in a *.jar file with a manifest), and, presumably, going in and tinkering with any or all of the foregoing every time a new Java version breaks something. (For an applet??? I don’t think I know any developers who hate themselves that much.)

It takes a fairly high degree of aggravation to push me into voluntarily electing javascript as an alternative to anything. Having been spoiled by elegant languages like Python and Mathematica, I’m not a fan — javascript strikes me as an unholy union of the heavy-handed obsessiveness of Java with the obfuscatorily cryptic excesses of Unix shell scripting — but I’ll save that rant for another time. Anyway, as best I can tell, there aren’t any good alternatives for embedding lightweight graphics functionality into a web page (not that applets were ever all that good an option either). I do very little web programming any more, and for a quick and dirty demo of something, it just isn’t worth fooling with add-on graphics libraries and such.

So back to the cellular automaton applet: converting Java applet to javascript isn’t very hard — Java and javascript both use essentially the same ugly syntax (Curly braces? Semicolons?? Seriously?)  for most things so a lot of the code just copies over. Applets draw onto a Graphics object passed as an argument to a “paint” event handler, while javascript can just draw directly onto the HTML5 canvas, but the basic graphics functions are nearly identical. Javascript doesn’t have a “sleep” function, so it’s slightly more complicated to slow the animation down, but not really difficult, just different. Compare the Java code with the javascript code, if interested.

The final glitch was that apparently there isn’t any practical way to embed running javascript into a WordPress post. You can use javascript with WordPress in other ways (widgets, for example), but not in a post. So if you’re using WordPress, it really isn’t a substitute for an applet, except that applets probably don’t work in WordPress posts either. Since I can’t embed my cellular automaton in this post, so instead I’ll just link to a plain HTML page running the script, and show a screen shot here, which WordPress will allow me to do. If you want to see the code, just open the HTML page in a text editor or use the “view code” option in your browser (cntl-U in most browsers). Note that the code is not intended to be, and certainly isn’t, an example of elegantly written javascript, it’s an example of modifying just what I had to to convert it from Java and get it to run.

For the details of the cellular automaton and how it works, please see the older post.

To see the javascript version running in a separate HTML page, click here.


Leave a Reply

Your email address will not be published. Required fields are marked *