Note that there are some explanatory texts on larger screens.

plurals
  1. POImage Processing, extending JPanel and Simulating Classes in Clojure
    text
    copied!<p>there! I'm building an image-processing application in swing/clojure, and right now I need to develop an image panel in which I can click and compute data. Thanks to <a href="https://stackoverflow.com/questions/1516287/using-swing-i-want-to-draw-a-couple-of-points">coobird</a>, I now have a good idea on how to do it in Java, but I still don't get many issues on its integration with Clojure.</p> <p>Let's take a look at how coobird suggested me doing. First, we should extend a class in Java. In clojure, we do this with the <code>proxy</code> macro, so we'd have something like this:</p> <pre> (def painting-panel (proxy [JPanel] [])) </pre> <p>The next step is to create the class constructor and set some variables. </p> <ol> <li><p>I can define functions after the second argument of proxy, but how can I create the constructor? Is painting-panel the name of this class (therefore the name of the function I should create)?</p></li> <li><p>How can I deal with class variables? Should I define them with a <code>let</code>, like I did?</p></li> <li><p>Are <code>this</code> and <code>super</code> available for me to use, like I did below?</p></li> </ol> <pre> (def painting-panel (let [background-image (Image.) point-clicked (Point.)] (proxy [JPanel] [] (paintComponent [g] (do ((.paintComponent super) g) (doto g (.drawImage background-image 0 0 nil) (.fillRect (.x point-clicked) (.y point-clicked) 1 1)))) (painting-panel []; constructor? ((.addMouseListener this) (proxy [MouseAdapter] [] (mouseClicked [e] (do (def point-clicked (.getPoint e)) (.repaint this))))))))) </pre> <p>Suggestions and code corrections are also welcome!</p> <p>Thank you!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload