Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rendering several times from the same REPL process #10

Open
mrzor opened this issue May 16, 2016 · 4 comments
Open

Rendering several times from the same REPL process #10

mrzor opened this issue May 16, 2016 · 4 comments

Comments

@mrzor
Copy link

mrzor commented May 16, 2016

Greetings!

I've been experimenting with tikkba exemples. I've stripped the first one down to just a text label.

I would like to be able to close the Swing window and start a new one by calling (-main) again from the REPL - arguably there are nicer ways to achieve reloading, but I was expecting this one to work.

What happens is that the first time (-main) is called from the REPL, the render succeeds (text appears in frame). After closing the frame, calling (-main) again results in an empty frame. Restarting the REPL allows for another render.

There are no exceptions as far as I can tell, and nothing to hint me off on stdout/stderr. How could I debug such an issue?

$ java -version
openjdk version "1.8.0_92"
OpenJDK Runtime Environment (build 1.8.0_92-b14)
OpenJDK 64-Bit Server VM (build 25.92-b14, mixed mode)

I'm on an up-to-date Archlinux system, with latest proprietary nvidia drivers.

Here is my test case:

(ns playground.tikkba
  (:require [tikkba.swing :as ts]
            [tikkba.dom :as td]
            [analemma.svg :as as]
            [analemma.charts :as ac]
            [analemma.xml :as ax])
  (:import (javax.swing JFrame SwingUtilities)
           (org.apache.batik.swing JSVGCanvas)))

(defn hw-svg []
  (as/svg
    (-> (as/line 2 10 50 10)
        (ax/add-attrs :stroke "black" :stroke-width 4))
    (-> (as/text "Hello world")
        (ax/add-attrs :x 2 :y 30)
        (as/style :fill "#000066"
                  :font-family "Verdana"
                  :font-size "28px"
                  :alignment-baseline :middle))))

(defn create-frame
  [^JSVGCanvas canvas]
  (let [frame (JFrame.)]
    (.setTitle frame "Tikkba!")
    (.add frame canvas)
    (.setSize frame 800 200)
    ;(.setDefaultCloseOperation frame JFrame/EXIT_ON_CLOSE)
    (SwingUtilities/invokeAndWait
      (fn [] (.setVisible frame true)))))

(defn -main
  []
  ;; Converts the SVG representation to a XML Document
  ;; and displays the SVG in a JFrame
  (let [doc (td/svg-doc (hw-svg))
        canvas (ts/jsvgcanvas)]
    (ts/set-document canvas doc)
    (create-frame canvas)))

Cheers!

@mrzor
Copy link
Author

mrzor commented May 16, 2016

Well, sure enough, there was a way to achieve my initial goal of being able to reload without restarting everything.

Here's my take on it:

(ns savages.art.gen-debug
  (:require [tikkba.swing :as ts]
            [tikkba.dom :as td]
            [analemma.svg :as as]
            [analemma.charts :as ac]
            [analemma.xml :as ax])
  (:import (javax.swing JFrame SwingUtilities)
           (org.apache.batik.swing JSVGCanvas)))

; Hack to keep the same canvas across REPL reloads
(def canvas)
(when-not (bound? #'canvas)
  (def ^JSVGCanvas canvas (ts/jsvgcanvas)))

(defn hw-svg []
  (as/svg
    (-> (as/line 2 10 50 10)
        (ax/add-attrs :stroke "black" :stroke-width 4))
    (-> (as/text "Hello world ZORRR")
        (ax/add-attrs :x 2 :y 30)
        (as/style :fill "#000066"
                  :font-family "Verdana"
                  :font-size "28px"
                  :alignment-baseline :middle))))

(defn create-frame []
  (let [frame (JFrame.)]
    (.setTitle frame "Tikkba!")
    (.add frame canvas)
    (.setSize frame 800 200)
    ;(.setDefaultCloseOperation frame JFrame/EXIT_ON_CLOSE)
    (SwingUtilities/invokeAndWait
      (fn []
        (.setVisible frame true)))))

(defn reload! []
  (let [doc (td/svg-doc (hw-svg))]
    (ts/set-document canvas doc)
    (.repaint canvas)))

(defn -main
  []
  ;; Converts the SVG representation to a XML Document
  ;; and displays the SVG in a JFrame
  (reload!)
  (create-frame))

I could boil that down and make it README-worthy if you're interested.

I still believe that there is a glitch somewhere that prevents creating many JFrame/canvases in the same process.
Is it from batik? swing? Both of those are tentacular deps I'm not too keen on exploring in gory details.

Regarding closing this issue, I'm not sure if tikkba.swing could provide a JFrame-level restart-able window.

@pallix
Copy link
Owner

pallix commented May 19, 2016

I cannot say where the problem is from. The best is to write a sample of your problem in Java and asks on the Batik mailing list but probably you do not want to do that.

Maybe you can add a development.md file explaning your solution?

As for providing a helper function, if necessary, it could go in tikkba.utils.swing, what do you think?

@mrzor
Copy link
Author

mrzor commented May 19, 2016

Actually attempting to reproduce this using only Java would clearly help pinpoint whether Batik or Tikkba is the origin. I can't commit to doing such a thing any time soon, however.

If there is a Java/Batik workaround for the issue, making it available from tikkba.utils.swing sounds perfectly reasonable to me.

Writing up a development.md could be done, but being very new to tikkba and being still in early development with it, I'm not sure I can phrase up a comprehensive thing on the subject.
How about adding an exemple and a section to the README pointing to it ? (down to the bottom, titled 'Working from the REPL')

@pallix
Copy link
Owner

pallix commented May 19, 2016

Yes, addition an example and a link is very fine. I misunderstood you, I though you wanted to have the whole code in the README..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants