Category Archives: Clojure

Clojure GUI Demo of Black-Scholes

Launch Clojure GUI demo of Black Scholes

If you’ve not heard of Black Scholes don’t fear. The equation exists in the world of Financial Services, in particular Options trading. It’s purpose is to calculate a price for a set of options given a model of a financial market containing derivative instruments.

clojure-black-scholes screenshot

 

Paul Legatohas written up a fantastic post on writing a GUI Demo for Scholes Black in Clojure.

You can start it via web start here. You can view the code here.

Clojure GUI Demo of Tetris (via Web Start)

Launch Clojure GUI demo of Tetris

Tetris is an old favourite, that I first played on a friend’s Nintendo Entertainment system. This version come to us from Alex Yakushev via Clojure.

Screenshot of the Clojure GUI demo of tetris

You can play it via the following Web Start link. You can see the code on github here.

The game was originally written by Alexey Pajitnov when working for the Soviet Government. He named it after the Greek word tetra, meaning four, and his favourite game tennis.

The great thing about Alex Y’s writing style is that he puts heaps of effort into sequentially explaining what he is trying to say. You can see more of this when he talks about sockets in Clojure

Clojure GUI demo of Pong

Launch Clojure GUI Demo of Pong

This is a Clojure demo of pong available via Java Web Start. You can see it by clicking the link here.
Clojure GUI Pong Game screenshot

You can see the source code here.

This is a fork from Juan Manuel Gimeno on Github who in turn forked it from Caesar Canassa’s repository on bitbucket.

His blog post launching it is here. (It’s a fun story – couple of mates and some beers on a Saturday night get hacking, upload a demo the following day.) Top work boys!

My intention was to add Java Web start to enable it to be demoed to a wider audience and promote the clojure community.

Applets in Clojure and Counterclockwise

Has anybody looked at Applets since 1997 and Java 1.1? Why would you want to do this?

There are a couple of reasons:

  1. Clojure rocks. You want to show people online what your Clojure code is doing, without having them read the code, or interact with another servlet through a browser.
  2. The reality is that the tooling and libraries for the JVM are superior to Flash and Javascript, and will probably remain so for some time.

Isn’t Clojurescript Cooler?

Yes – but at this point in time is less ‘production ready’ than the main Clojure release.

But haven’t people done this online already?

They have. But there is no good lifecycle for testing it in Counterclockwise (or your REPL of choice).  There is no way to change the code and easily debug it,  without going through a full code->compile->deploy cycle.

In Eclipse for Java Applets, you can just extend the Applet, and then right-click and select ‘run as Applet’. In Clojure (as these examples show) you can also extend the Applet. But there is no easy way to ‘Run as Applet’. Simply executing the Clojure code doesn’t start it either.

So what does the basic Applet look like?

SimpleSwingApplet.clj
(ns demo.applet.SimpleSwingApplet
      (:import
        (javax.swing JApplet JPanel JLabel JFrame))
      (:gen-class
        :post-init post-init
        :extends javax.swing.JApplet))

(defn -post-init [this]
  (def jpanel (JPanel.))
  (.add jpanel (JLabel. "This is my first applet"))
  (.setContentPane this jpanel))

Now that’s what the other two blogs had. Great – but if you run that on your Clojure REPL, you’ll get a big fat nothing. So this is where we start adding value.

Here is what we’ll add to make it run in Counterclockwise (or your REPL of choice).

SimpleSwingAppletRunner.clj
(ns game.applet.SimpleSwingAppletRunner
  (:import
        (javax.swing JApplet JPanel JLabel JFrame)))

;------
(compile 'game.applet.SimpleSwingApplet)

(defn -main [s]
  (let [applet (new demo.applet.SimpleSwingApplet)]
    (doto (JFrame. "MyApplet")
      (.add (.getContentPane applet))
      (.pack)
      (.setLocationByPlatform true)
      (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
      (.setVisible true))     
    ))

(-main "s")

Basically we instantiate the Applet and then get the contentPane which we pass to a newly created a Frame.