Search code examples
swingclojureseesaw

How do I set an image's resolution?


I am using Seesaw to interact with Swing.

I have some icons defined thus:

(def label :icon 
           (clojure.java.io/resource "some_image.png"))

I want to render "some_image.png" in a different resolution. If I simply set the bounds, I'll only get part of the image.

How do I achieve this?


Solution

  • you could do it by dropping down into Swing. basically, manipulate the file as a Swing Image. once you have it at the size you want, Seesaw's icon facilities are flexible in terms of what can be passed in (see https://daveray.github.io/seesaw/seesaw.icon-api.html); you can pass the Swing Image into the label function.

    (defn imagetest []
      (let [w (frame :title "Image Test" :width 400 :height 400)
            img (.getScaledInstance 
                  (javax.imageio.ImageIO/read 
                    (io/resource "racecar.gif")) 400 400 1)
            lbl (label :icon img)
            pnl (horizontal-panel :items [lbl])]
        (config! w :content pnl)
        (show! w)))
    

    Note: the 1 that i'm passing as the final arg to .getScaledInstance is for the SCALE_DEFAULT flag; more info here: https://docs.oracle.com/javase/7/docs/api/constant-values.html#java.awt.Image.SCALE_DEFAULT