source: src/main/java/weka/gui/visualize/JPEGWriter.java @ 18

Last change on this file since 18 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 6.1 KB
RevLine 
[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  *    JPEGWriter.java
19  *    Copyright (C) 2005 University of Waikato, Hamilton, New Zealand
20  *
21  */
22
23package weka.gui.visualize;
24
25import java.awt.Color;
26import java.awt.Graphics;
27import java.awt.Graphics2D;
28import java.awt.image.BufferedImage;
29import java.io.File;
30import java.util.Iterator;
31import java.util.Locale;
32
33import javax.imageio.IIOImage;
34import javax.imageio.ImageIO;
35import javax.imageio.ImageWriteParam;
36import javax.imageio.ImageWriter;
37import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
38import javax.imageio.stream.ImageOutputStream;
39import javax.swing.JComponent;
40
41/**
42 * This class takes any JComponent and outputs it to a JPEG-file.
43 * Scaling is by default disabled, since we always take a screenshot.
44 *
45 * @author FracPete (fracpete at waikato dot ac dot nz)
46 * @version $Revision: 5920 $
47 */
48public class JPEGWriter
49  extends JComponentWriter {
50 
51  /** the quality of the image. */
52  protected float m_Quality;
53 
54  /** the background color. */
55  protected Color m_Background;
56 
57  /**
58   * initializes the object.
59   */
60  public JPEGWriter() {
61    super();
62  }
63
64  /**
65   * initializes the object with the given Component.
66   *
67   * @param c         the component to print in the output format
68   */
69  public JPEGWriter(JComponent c) {
70    super(c);
71  }
72
73  /**
74   * initializes the object with the given Component and filename.
75   *
76   * @param c         the component to print in the output format
77   * @param f         the file to store the output in
78   */
79  public JPEGWriter(JComponent c, File f) {
80    super(c, f);
81   
82    m_Quality    = 1.0f;
83    m_Background = Color.WHITE;
84  }
85 
86  /**
87   * further initialization.
88   */
89  public void initialize() {
90    super.initialize();
91   
92    m_Quality    = 1.0f;
93    m_Background = Color.WHITE;
94    setScalingEnabled(false);
95  }
96
97  /**
98   * returns the name of the writer, to display in the FileChooser.
99   * must be overridden in the derived class.
100   *
101   * @return the name of the writer
102   */
103  public String getDescription() {
104    return "JPEG-Image";
105  }
106 
107  /**
108   * returns the extension (incl. ".") of the output format, to use in the
109   * FileChooser.
110   * must be overridden in the derived class.
111   *
112   * @return the file extension
113   */
114  public String getExtension() {
115    return ".jpg";
116  }
117 
118  /**
119   * returns the current background color.
120   *
121   * @return the current background color
122   */
123  public Color getBackground() {
124    return m_Background;
125  }
126 
127  /**
128   * sets the background color to use in creating the JPEG.
129   *
130   * @param c the color to use for background
131   */
132  public void setBackground(Color c) {
133    m_Background = c;
134  }
135 
136  /**
137   * returns the quality the JPEG will be stored in.
138   *
139   * @return the quality
140   */
141  public float getQuality() {
142    return m_Quality;
143  }
144 
145  /**
146   * sets the quality the JPEG is saved in.
147   *
148   * @param q the quality to use
149   */
150  public void setQuality(float q) {
151    m_Quality = q;
152  }
153 
154  /**
155   * generates the actual output.
156   *
157   * @throws Exception  if something goes wrong
158   */
159  public void generateOutput() throws Exception {
160    BufferedImage       bi;
161    Graphics            g;
162    ImageWriter         writer;
163    Iterator            iter;
164    ImageOutputStream   ios;
165    ImageWriteParam     param;
166
167    // render image
168    bi = new BufferedImage(getComponent().getWidth(), getComponent().getHeight(), BufferedImage.TYPE_INT_RGB);
169    g  = bi.getGraphics();
170    g.setPaintMode();
171    g.setColor(getBackground());
172    if (g instanceof Graphics2D)
173      ((Graphics2D) g).scale(getXScale(), getYScale());
174    g.fillRect(0, 0, getComponent().getWidth(), getComponent().getHeight());
175    getComponent().printAll(g);
176   
177    // get jpeg writer
178    writer = null;
179    iter   = ImageIO.getImageWritersByFormatName(getExtension().replace(".", ""));
180    if (iter.hasNext())
181      writer = (ImageWriter) iter.next();
182    else
183      throw new Exception("No writer available for " + getDescription() + "!");
184
185    // prepare output file
186    ios = ImageIO.createImageOutputStream(getFile());
187    writer.setOutput(ios);
188
189    // set the quality
190    param = new JPEGImageWriteParam(Locale.getDefault());
191    param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT) ;
192    param.setCompressionQuality(getQuality());
193
194    // write the image
195    writer.write(null, new IIOImage(bi, null, null), param);
196
197    // cleanup
198    ios.flush();
199    writer.dispose();
200    ios.close();   
201  }
202 
203  /**
204   * for testing only.
205   *
206   * @param args the commandline arguments
207   * @throws Exception if something goes wrong
208   */
209  public static void main(String[] args) throws Exception {
210    System.out.println("building TreeVisualizer...");
211    weka.gui.treevisualizer.TreeBuild builder = new weka.gui.treevisualizer.TreeBuild();
212    weka.gui.treevisualizer.NodePlace arrange = new weka.gui.treevisualizer.PlaceNode2();
213    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 }"));
214    weka.gui.treevisualizer.TreeVisualizer tv = new weka.gui.treevisualizer.TreeVisualizer(null, top, arrange);
215    tv.setSize(800 ,600);
216   
217    String filename = System.getProperty("java.io.tmpdir") + File.separator + "test.jpg";
218    System.out.println("outputting to '" + filename + "'...");
219    toOutput(new JPEGWriter(), tv, new File(filename));
220
221    System.out.println("done!");
222  }
223}
Note: See TracBrowser for help on using the repository browser.