Skip to content

Commit

Permalink
Bit of condensing
Browse files Browse the repository at this point in the history
  • Loading branch information
fffej committed Dec 31, 2008
1 parent acf74b8 commit e3ba8aa
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions sortapp.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,18 @@
(take (count lst) (iterate bubble lst)))

(def maxval 100)

(def initial-list (take 100 (repeatedly (fn [] (rand-int maxval)))))

(def position (atom 0))

(def canvas (proxy [JPanel ActionListener] []
(paintComponent [g]
(proxy-super paintComponent g)
(.setColor g Color/RED)
(let [width (.getWidth this) height (.getHeight this) barHeight (/ height (inc (count initial-list))) barWidthPerVal (/ width maxval)
(let [width (.getWidth this) height (.getHeight this) bar-height (/ height (inc (count initial-list))) val-width (/ width maxval)
bubble-sorted (bubble-sort initial-list)]
(doseq [val (into (sorted-map) (zipmap (range 0 (count initial-list)) (nth bubble-sorted @position)))]
(let [y (int (* (first val) barHeight)) barWidth (int (* (second val) barWidthPerVal))]
(.fillRect g 0 y barWidth barHeight)))))
(let [y (int (* (first val) bar-height)) barWidth (int (* (second val) val-width))]
(.fillRect g 0 y barWidth bar-height)))))
(actionPerformed [e] (swap! position inc) (compare-and-set! position (count initial-list) 0) (.repaint this))))

(let [x (Timer. 50 canvas)]
Expand All @@ -35,15 +33,11 @@
(defn is-running [] (.isRunning x)))

(defn sortapp []
(let [frame (JFrame. "Sort Visualizer")
algorithm-chooser (JComboBox.)
run-button (JButton. "Go")]
(let [frame (JFrame. "Sort Visualizer")run-button (JButton. "Go")]
(.addActionListener run-button
(proxy [ActionListener] []
(actionPerformed [evt]
(if (is-running)
(stop-timer)
(start-timer)))))
(if (is-running) (stop-timer) (start-timer)))))
(doto frame
(.setLayout (BorderLayout. 3 3))
(.add canvas (BorderLayout/CENTER))
Expand Down

0 comments on commit e3ba8aa

Please sign in to comment.