Created
December 9, 2014 18:33
-
-
Save capitancook/3f361142ccf2e65e7d1b to your computer and use it in GitHub Desktop.
2d rendering graphics library for the kbcad example - http://highorderdysfunctions.blogspot.com/2014/12/frame-language-knowledge-based-computer_9.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns clj-fl-examples.renderlib | |
(:require [clj-fl.core :refer :all]) | |
(import [java.awt Color] | |
[javax.imageio ImageIO] | |
[javax.swing JPanel JFrame])) | |
(def frame (atom nil)) | |
(def gfx (atom nil)) | |
(def t (atom nil)) ;real world to screen window coordinate map | |
(defn startgraphics | |
"start a graphics window of wx by xy pixels (400 by 400 if none is specified. | |
The real world that is mapped in the graphic window is approx 12 meterd by 12 meters." | |
([] (startgraphics 400 400)) | |
([wx wy] | |
(reset! frame | |
(doto (JFrame.) | |
(.setVisible true) | |
(.setSize wx wy) | |
(.setBackground Color/WHITE))) | |
(reset! gfx | |
(doto | |
(.getGraphics @frame) | |
(.setStroke (java.awt.BasicStroke. 1.0)) | |
(.setColor (java.awt.Color. 0 0 255)))) | |
(reset! t {:tx (int(/ wx 20)) :ty (int (/ wx 20)) | |
:dx (int (/ wx 8)) :dy (int (/ wy 8)) | |
:wx wx :wy wy}))) | |
(defn transform [p] | |
"Viewing Transformation - perform the mapping of real world coordinates of the point p | |
to the screen window coordinates. here the real world is a rectangle on a plane of 12 by 12 meters" | |
(list (+ (@t :dx) (* (first p) (@t :tx))) (+ (@t :dy) (* (second p) (@t :ty))))) | |
(defn transformsegment [s] | |
"Viewing Transformation - perform the viewing transformation of the two end points of the segment s." | |
(list (transform (first s)) (transform (second s)))) | |
(defn setlinecolor [c] | |
(.setColor @gfx (java.awt.Color. (first c) (second c) (third c)))) | |
(defn setlinewidth [w] | |
(.setStroke @gfx (java.awt.BasicStroke. w))) | |
(defn drawline [s] | |
"Draw the line segment s" | |
(let [st (transformsegment s)] | |
(.drawLine @gfx (first(first st)) (second(first st)) (first(second st)) (second(second st))))) | |
(defn drawcolorline [c w s] | |
"Draw the line segment s of color c and width w" | |
(setlinecolor c) | |
(setlinewidth w) | |
(drawline s)) | |
(defn drawstring [s p] | |
"draw the string s starting from point p" | |
(let [pt (transform p)] | |
(.drawString @gfx s (first pt) (second pt)))) | |
(defn drawcolorstring [c s p] | |
"draw the string s starting from point p using the color c" | |
(setlinecolor c) | |
(drawstring s p)) | |
(defn drawrectangle [x1 y1 x2 y2] | |
(.drawRectangle @gfx x1 y1 x2 y2)) | |
(defn drawfilledrectangle [x1 y1 x2 y2] | |
(.fillRect @gfx x1 y1 x2 y2)) | |
(defn drawcircle [x y r] | |
(.drawOval @gfx (- x r) (- y r ) (* 2 r) (* 2 r))) | |
(defn drawfilledcircle [x y r] | |
(.fillOval @gfx (- x r) (- y r ) (* 2 r) (* 2 r))) | |
(defn cleargraphics [] | |
"Clears the Graphics2D context" | |
(setlinecolor '(255 255 255)) | |
(.clearRect @gfx 0 0 (:wx @t) (:wy @t))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment