For the sake of anyone who finds this thread in a search at some point, here
is the actual answer to my original question:

1.  It doesn't matter whether you extend Applet or JApplet, at least for my
purposes so far, it seems.  JApplet is swing, and solves some of the
bugginess found in awt (such as buffered image flicker for example).

2. For Java applets there seems to be two different and incompatible methods
of listening to the mouse.  One is using the mouseDown handler, and other
than throwing random null pointer exceptions, there is no corresponding
handler for keyboard events that actually works from that API as far as I
can tell.

Therefore you need to implement java.awt.event.KeyListener and
java.awt.event.MouseListener in your gen-class.

Gen-class should have this at least (syntax as embedded in ns macro call):
(:gen-class
 :extends javax.swing.JApplet
 :implements [java.awt.event.KeyListener java.awt.event.MouseListener])

You need to import from java.awt.event: KeyListener KeyEvent MouseListener
and MouseEvent.

3.  The prefered event handlers to implement are those from KeyListener et
al.  Do not write handlers for mouseDown, etc (also those alternate handlers
won't work if you have implemented the above in your gen-class).

(defn -keyReleased [#^JApplet this #^KeyEvent e] )
(defn -keyTyped [#^JApplet this #^KeyEvent e] )
(defn -keyPressed [#^JApplet this #^KeyEvent e]
   (and (condp = (. e getKeyCode)
   KeyEvent/VK_LEFT (go-left) KeyEvent/VK_RIGHT (go-right)
   KeyEvent/VK_UP (go-up) KeyEvent/VK_DOWN (go-down) false)
   (.repaint this)))

(defn -mouseEntered [#^JApplet this #^MouseEvent e] )
(defn -mouseExited [#^JApplet this #^MouseEvent e] )
(defn -mousePressed [#^JApplet this #^MouseEvent e] )
(defn -mouseReleased [#^JApplet this #^MouseEvent e] )
(defn -mouseClicked [#^JApplet this #^MouseEvent e]
   ; do something with (.getX e) (.getY e)
   (.repaint this))

Repaint is a method of JApplet (or Applet if you use that).

4.  Implement JApplet (or Applet)'s init handler (not to be confused with
gen-class's init/post-init handlers, which I'm not entirely clear on just
yet).

(defn -init [#^JApplet this]
   (.addKeyListener this this)
   (.addMouseListener this this)
   (.setFocusable this true)

Very important: add the listeners, and setFocusable as shown above.

5.  Implement paint to render any graphics based on your applet's updated
state.
Like so:
(defn -paint [#^JApplet this #^Graphics g]
  (draw-images this g))


Rob

On Mon, Jun 14, 2010 at 3:52 AM, Meikel Brandmeyer <m...@kotka.de> wrote:

> Hi,
>
> On Jun 14, 8:55 am, rob levy <r.p.l...@gmail.com> wrote:
>
> > (ns ...
> >     ...
> >     (:gen-class
> >      :extends javax.swing.JApplet
> >   ???   :implements java.awt.event.KeyListener))
>
> :implements [java.awt.event.KeyListener]
>
> Note: vector.
>
> > (defn -init [#^JApplet applet]
> >   ??? addKeyListener )
>
> You probably want :post-init.
>
> > (defn -keyPressed [#^JApplet applet #^KeyEvent event]
> >   (let [key (. event getKeyCode)]
> >     (cond (= key (. KeyEvent VK_LEFT))  (dosync (ref-set message "left"))
> >           (= key (. KeyEvent VK_RIGHT)) (dosync (ref-set message
> "right"))
> >           (= key (. KeyEvent VK_UP))    (dosync (ref-set message "up"))
> >           (= key (. KeyEvent VK_DOWN))  (dosync (ref-set message
> "down"))))
>
> You can simplify this with condp.
>
> (condp = key
>  KeyEvent/VK_LEFT ....
>  KeyEvent/VK_RIGHT ....
>  KeyEvent/VK_UP ....
>  KeyEvent/VK_DOWN ....)
>
> Concerning the question how to make the applet respond: Dunno what the
> best way is. I would probably put the KeyListener into a proxy, which
> drives the necessary changes (changing the refs in your example). In
> the GUI code I would add listeners to the refs, which update the UI on
> change.
>
> Sincerely
> Meikel
>
> PS: Shameless self-promotion:
> For proxy:
> http://kotka.de/blog/2010/03/proxy_gen-class_little_brother.html
> For gen-class:
> http://kotka.de/blog/2010/02/gen-class_how_it_works_and_how_to_use_it.html
> For GUI driving:
> http://kotka.de/blog/2010/05/Decoupling_Logic_and_GUI.html
>
> Hope some of this helps.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com<clojure%2bunsubscr...@googlegroups.com>
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to