interactive swing

This demo is based on the capabilites of kin as at 2003-08-20. Features may well change.

(use-package 'java-awt)
(use-package 'javax-swing)
These statements tell the current (kin-user) package to use symbols from the java packages 'java.awt' and 'javax.swing'.
You may either use the lispified names, or the dot notation for package names.
(use-class 'color)
(use-class 'border-layout)
(use-class 'kin-awt:awt-functions)
These statements tell the the current package to use the global symbols and functions (public static fields and public static methods) from the classes Color, BorderLayout, and kin.awt.AwtFunctions. (kin) looks for unqualified classes in all used packages. As you have imported java-awt and javax.swing, then this uses the classes java.awt.Color, java.awt.BorderLayout, and kin.awt.AwtFunctions. kin-awt:awt-functions contains methods to generate proxies for interfaces in java.awt.event that allow the events to be handled by lambda functions.
You may either use the lispified names, or the dot notation for class names; however, the separator between the package and the symbol name must be the lisp package seperator, ':'.
(setf jf (make-instance 'j-frame))
(setf (visible jf) t)
(kin) maps mixed case Java names to all lowercase lispified names. This creates a javax.swing.JFrame, setting the variable 'jf' to reference it.
The frame is make visible by setting its visible accessor to the generalized boolean value 't'. (kin) maps 'set' 'get' and 'is' methods to accessor functions.
 
(make-instance 'rectangle 10 20 300 200)
(setf (bounds jf) *)
(move jf 120 120)
(setf (title jf) "(kewl indigo newts)")
(setf (layout (content-pane jf)) (make-instance 'border-layout))
These expressions set various properties of the frame.
(add (content-pane jf) (setf panel (make-instance 'j-panel)) *center*)
(setf (opaque panel) t)
(setf (background panel) yellow)
(setf (foreground (setf rainbow (add panel (make-instance 'j-button "rainbow")))) red)
(setf (visible rainbow) t)
Because we earlier used the java.awt.Color and java.awt.BorderLayout classes, we don't have to qualify 'red', 'yellow' or '*center*. Java names all in upper case are mapped to lowercase names in (kin) with stars.

You may have to touch the frame or resize it to force it to do a layout.

 
(setf (background rainbow) (background (parent rainbow)))
(setf (background panel) pink)
These just illustrate some of (kin)'s use of Java properties.
(defmacro inherit (object property) `(setf (,property ,object) (,property (parent ,object))))
This is a macro, defined using lisp's backquote notation. Given a reference to an object, and the name of a property, this macro generates code which sets the object's property to the property with the same name belonging to the parent. You simply can't do that in Java in one line of code.
(inherit rainbow background)
(setf (background panel) yellow)
(inherit rainbow background)
(setf (background panel) black)
(inherit rainbow background)
This illustrates the use of the inherit macro.
(let ((random (make-instance 'java-util:random)))
 (defun random-selection-from (list) 
  (lambda () (nth (next-int random (length list)) list))))
This is a closure, which contains a function which creates a function to return a random element from a list.
(setf (symbol-function 'random-color)
  (random-selection-from (list red yellow pink blue orange (darker magenta) green)))
This code sets the function for the symbol 'random-color to choose a color at random from the list.
(add-action-listener rainbow
 (setf alr 
  (action-listener
   (lambda (event)
    (setf (background panel) (random-color))
    (inherit rainbow background)))))
This code uses the action-listener function from kin-awt:awt-functions to create an action listener for the rainbow button. If you now press the rainbow button, the background will change to a random color.
(add panel
  (let ((label (make-instance 'j-label "wibble"))
          (mouse-listener (mouse-listener nil nil nil nil nil)))
   (add-mouse-listener label mouse-listener)
   (setf (enter-function mouse-listener)
     (lambda (event) (setf (background label) blue)))
   (setf (exit-function mouse-listener)
     (lambda (event) (inherit label background)))
   (setf (opaque label) t)
   (setf (visible label) t)
   (inherit label background)
   label))
This code adds a label to the panel, with a mouse-listener which switches the background color of the label.

To exit kin, use the exit function from java.lang.System, which is imported into the kin-user package by default:

(exit 0)

Pete Kirkham

2003-08-20.

SourceForge.net Logo