[4] | 1 | /* |
---|
| 2 | * This program is free software; you can redistribute it and/or modify |
---|
| 3 | * it under the terms of the GNU General Public License as published by |
---|
| 4 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 5 | * (at your option) any later version. |
---|
| 6 | * |
---|
| 7 | * This program is distributed in the hope that it will be useful, |
---|
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 10 | * GNU General Public License for more details. |
---|
| 11 | * |
---|
| 12 | * You should have received a copy of the GNU General Public License |
---|
| 13 | * along with this program; if not, write to the Free Software |
---|
| 14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | /* |
---|
| 18 | * PostscriptWriter.java |
---|
| 19 | * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand |
---|
| 20 | * |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | package weka.gui.visualize; |
---|
| 24 | |
---|
| 25 | import java.io.BufferedOutputStream; |
---|
| 26 | import java.io.File; |
---|
| 27 | import java.io.FileOutputStream; |
---|
| 28 | |
---|
| 29 | import javax.swing.JComponent; |
---|
| 30 | |
---|
| 31 | /** |
---|
| 32 | * This class takes any Component and outputs it to a Postscript file.<p> |
---|
| 33 | * <b>Note:</b><br> |
---|
| 34 | * This writer does not work with Components that rely on clipping, like e.g. |
---|
| 35 | * scroll lists. Here the complete list is printed, instead of only in the |
---|
| 36 | * borders of the scroll list (may overlap other components!). This is due to |
---|
| 37 | * the way, clipping is handled in Postscript. There was no easy way around |
---|
| 38 | * this issue. :-( |
---|
| 39 | * |
---|
| 40 | * @author FracPete (fracpete at waikato dot ac dot nz) |
---|
| 41 | * @version $Revision: 1.3 $ |
---|
| 42 | * @see PostscriptGraphics |
---|
| 43 | */ |
---|
| 44 | public class PostscriptWriter |
---|
| 45 | extends JComponentWriter { |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | * initializes the object |
---|
| 49 | */ |
---|
| 50 | public PostscriptWriter() { |
---|
| 51 | super(null); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | /** |
---|
| 55 | * initializes the object with the given Component |
---|
| 56 | * |
---|
| 57 | * @param c the component to print in the output format |
---|
| 58 | */ |
---|
| 59 | public PostscriptWriter(JComponent c) { |
---|
| 60 | super(c); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | * initializes the object with the given Component and filename |
---|
| 65 | * |
---|
| 66 | * @param c the component to print in the output format |
---|
| 67 | * @param f the file to store the output in |
---|
| 68 | */ |
---|
| 69 | public PostscriptWriter(JComponent c, File f) { |
---|
| 70 | super(c, f); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | * returns the name of the writer, to display in the FileChooser. |
---|
| 75 | * must be overridden in the derived class. |
---|
| 76 | */ |
---|
| 77 | public String getDescription() { |
---|
| 78 | return "Postscript-File"; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | /** |
---|
| 82 | * returns the extension (incl. ".") of the output format, to use in the |
---|
| 83 | * FileChooser. |
---|
| 84 | * must be overridden in the derived class. |
---|
| 85 | */ |
---|
| 86 | public String getExtension() { |
---|
| 87 | return ".eps"; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | /** |
---|
| 91 | * generates the actual output |
---|
| 92 | * |
---|
| 93 | * @throws Exception if something goes wrong |
---|
| 94 | */ |
---|
| 95 | public void generateOutput() throws Exception { |
---|
| 96 | BufferedOutputStream ostrm; |
---|
| 97 | PostscriptGraphics psg; |
---|
| 98 | |
---|
| 99 | ostrm = null; |
---|
| 100 | |
---|
| 101 | try { |
---|
| 102 | ostrm = new BufferedOutputStream(new FileOutputStream(getFile())); |
---|
| 103 | psg = new PostscriptGraphics(getComponent().getHeight(), getComponent().getWidth(), ostrm); |
---|
| 104 | psg.setFont(getComponent().getFont()); |
---|
| 105 | psg.scale(getXScale(), getYScale()); |
---|
| 106 | getComponent().printAll(psg); |
---|
| 107 | psg.finished(); |
---|
| 108 | } |
---|
| 109 | catch (Exception e) { |
---|
| 110 | System.err.println(e); |
---|
| 111 | } |
---|
| 112 | finally { |
---|
| 113 | if (ostrm != null) { |
---|
| 114 | try { |
---|
| 115 | ostrm.close(); |
---|
| 116 | } catch (Exception e) { |
---|
| 117 | // Nothing to really do for error on close |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | /** |
---|
| 124 | * for testing only |
---|
| 125 | */ |
---|
| 126 | public static void main(String[] args) throws Exception { |
---|
| 127 | System.out.println("building TreeVisualizer..."); |
---|
| 128 | weka.gui.treevisualizer.TreeBuild builder = new weka.gui.treevisualizer.TreeBuild(); |
---|
| 129 | weka.gui.treevisualizer.NodePlace arrange = new weka.gui.treevisualizer.PlaceNode2(); |
---|
| 130 | weka.gui.treevisualizer.Node top = builder.create(new java.io.StringReader("digraph atree { top [label=\"the top\"] a [label=\"the first node\"] b [label=\"the second nodes\"] c [label=\"comes off of first\"] top->a top->b b->c }")); |
---|
| 131 | weka.gui.treevisualizer.TreeVisualizer tv = new weka.gui.treevisualizer.TreeVisualizer(null, top, arrange); |
---|
| 132 | tv.setSize(800 ,600); |
---|
| 133 | |
---|
| 134 | String filename = System.getProperty("java.io.tmpdir") + "test.eps"; |
---|
| 135 | System.out.println("outputting to '" + filename + "'..."); |
---|
| 136 | toOutput(new PostscriptWriter(), tv, new File(filename)); |
---|
| 137 | |
---|
| 138 | System.out.println("done!"); |
---|
| 139 | } |
---|
| 140 | } |
---|