source: branches/MetisMQI/src/main/java/weka/gui/visualize/BMPWriter.java @ 29

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

Taggata versione per la demo e aggiunto branch.

File size: 4.4 KB
Line 
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 * BMPWriter.java
19 * Copyright (C) 2007 University of Waikato, Hamilton, New Zealand
20 */
21
22package weka.gui.visualize;
23
24import java.awt.Color;
25import java.awt.Graphics;
26import java.awt.Graphics2D;
27import java.awt.image.BufferedImage;
28import java.io.File;
29
30import javax.imageio.ImageIO;
31import javax.swing.JComponent;
32
33/**
34 * This class takes any JComponent and outputs it to a BMP-file.
35 * Scaling is by default disabled, since we always take a screenshot.
36 *
37 * @author  fracpete (fracpete at waikato dot ac dot nz)
38 * @version $Revision: 5920 $
39 */
40public class BMPWriter
41  extends JComponentWriter {
42
43  /** the background color. */
44  protected Color m_Background;
45 
46  /**
47   * initializes the object.
48   */
49  public BMPWriter() {
50    super();
51  }
52
53  /**
54   * initializes the object with the given Component.
55   *
56   * @param c           the component to print in the output format
57   */
58  public BMPWriter(JComponent c) {
59    super(c);
60  }
61
62  /**
63   * initializes the object with the given Component and filename.
64   *
65   * @param c           the component to print in the output format
66   * @param f           the file to store the output in
67   */
68  public BMPWriter(JComponent c, File f) {
69    super(c, f);
70  }
71 
72  /**
73   * further initialization.
74   */
75  public void initialize() {
76    super.initialize();
77   
78    setScalingEnabled(false);
79  }
80
81  /**
82   * returns the name of the writer, to display in the FileChooser.
83   * must be overridden in the derived class.
84   *
85   * @return            the name of the writer
86   */
87  public String getDescription() {
88    return "BMP-Image";
89  }
90 
91  /**
92   * returns the extension (incl. ".") of the output format, to use in the
93   * FileChooser.
94   *
95   * @return            the file extension
96   */
97  public String getExtension() {
98    return ".bmp";
99  }
100 
101  /**
102   * returns the current background color.
103   *
104   * @return            the current background color
105   */
106  public Color getBackground() {
107    return m_Background;
108  }
109 
110  /**
111   * sets the background color to use in creating the BMP.
112   *
113   * @param c           the color to use for background
114   */
115  public void setBackground(Color c) {
116    m_Background = c;
117  }
118 
119  /**
120   * generates the actual output.
121   *
122   * @throws Exception  if something goes wrong
123   */
124  public void generateOutput() throws Exception {
125    BufferedImage       bi;
126    Graphics            g;
127
128    bi = new BufferedImage(getComponent().getWidth(), getComponent().getHeight(), BufferedImage.TYPE_INT_RGB);
129    g  = bi.getGraphics();
130    g.setPaintMode();
131    g.setColor(getBackground());
132    if (g instanceof Graphics2D)
133      ((Graphics2D) g).scale(getXScale(), getYScale());
134    g.fillRect(0, 0, getComponent().getWidth(), getComponent().getHeight());
135    getComponent().printAll(g);
136    ImageIO.write(bi, "bmp", getFile());
137  }
138 
139  /**
140   * for testing only.
141   *
142   * @param args        the commandline arguments
143   * @throws Exception  if something goes wrong
144   */
145  public static void main(String[] args) throws Exception {
146    System.out.println("building TreeVisualizer...");
147    weka.gui.treevisualizer.TreeBuild builder = new weka.gui.treevisualizer.TreeBuild();
148    weka.gui.treevisualizer.NodePlace arrange = new weka.gui.treevisualizer.PlaceNode2();
149    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 }"));
150    weka.gui.treevisualizer.TreeVisualizer tv = new weka.gui.treevisualizer.TreeVisualizer(null, top, arrange);
151    tv.setSize(800 ,600);
152   
153    String filename = System.getProperty("java.io.tmpdir") + File.separator + "test.bmp";
154    System.out.println("outputting to '" + filename + "'...");
155    toOutput(new BMPWriter(), tv, new File(filename));
156
157    System.out.println("done!");
158  }
159}
Note: See TracBrowser for help on using the repository browser.