March 03, 2004

java screen dump util

techie alert

I generate a lot of screenshots--and, well, windows "print screen" tends to look pretty darn ugly. The other night, I started thinking, "what if I could dump to EPS?"

Turns out someone beat me to it with this rather useful (GPL'd) package. So with that spurring me on, I've gone ahead and built a wrapper around it that allows simple dumps to both EPS (using this package) and to PNG (using javax.io). I haven't gotten around to JPG or GIF, but it would be pretty much the same code as the PNG (leave a comment if you want it.)

If nothing else, this keeps me from having to google around to remember how to export to an image.

Download the JAR here
Javadoc here

The basic usage is fairly simple:

Dump dumper = new EPSDump(); 
dumper.dumpComponent(new File("foo"), myPanel); 

Enjoy.

UPDATE: I just realized that it doesn't handle transparency. There's probably a couple of Swing gotchas in there... on the other hand, the results really do look pretty--at least! Those of us with PCs can at last have Java screenshots that look almost as good as Mac screenshots.

Here's one from a little code I've produced in JUNG. The pink circles are users of a network software who partiticapted in a study; the lines are "friend of" connections; the light blue circles are individuals; the blue strips are sets of individuals that have the same connectivity.

eps

** UPDATE **
I guess it would be useful to tack this to, say, a keyboard shortcut, like "alt-P".

In your component or window intialization, call

addPrintAction( myDumpablePanel );

and somewhere in there have some code like

addPrintAction( JComponent jp ) {
   Action printAction = new PrintAction(jp);
   KeyStroke k = KeyStroke.getKeyStroke("alt P");
   jp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(k, "printscreen");
   jp.getActionMap().put("printscreen", printAction);
}

And then the actual dump:

static class PrintAction extends AbstractAction {
   private JComponent component;
   public PrintAction(JComponent jp) {
      this.component = jp;
    }
   public void actionPerformed(ActionEvent e) {
      EPSDump dumper = new EPSDump();
      dumper.textVector = false;
      try {
         System.out.println("Dumping!");
         dumper.dumpComponent(new File("foo.eps"), component);
       } catch (IOException e1) {
         System.out.println("Dump failed");
       }
    }

good luck...

March 3, 2004 07:34 PM | TrackBack | in Other
Comments
Post a comment









Remember personal info?