
Animation is integral to many parts of GUIs, including both the fancy effects when things change used in window managers, and of course games.   The core of any animation system is a scheme for periodically changing the display while still remaining responsive to the user.   This task demonstrates this.



Task

Create a window containing the string "Hello World! " (the trailing space is significant).

Make the text appear to be rotating right by periodically removing one letter from the end of the string and attaching it to the front.

When the user clicks on the (windowed) text, it should reverse its direction.




(import '[javax.swing JFrame JLabel])
(import '[java.awt.event MouseAdapter])
 
(def text "Hello World! ")
(def text-ct (count text))
(def rotations
  (vec
    (take text-ct
      (map #(apply str %)
        (partition text-ct 1 (cycle text))))))
 
(def pos (atom 0))  ;position in rotations vector being displayed
(def dir (atom 1))  ;direction of next position (-1 or 1)
 
(def label (JLabel. text))
 
(.addMouseListener label
  (proxy [MouseAdapter] []
    (mouseClicked [evt] (swap! dir -))))
 
(defn animator []
  (while true
    (Thread/sleep 100)
    (swap! pos #(-> % (+ @dir) (mod text-ct)))
    (.setText label (rotations @pos))))
 
(doto (JFrame.)
  (.add label)
  (.pack)
  (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
  (.setVisible true))
 
(future-call animator)  ;simple way to run animator on a separate thread