Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jplot/SavePanel.java


1   /*
2    * JPLOT  -- Java Plotting Interface for any programme or
3    *           as an independent GUI
4    * 
5    * Copyright (C) 1999 Jan van der Lee
6    *
7    * This program is free software; you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation; either version 2
10   * of the License, or (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  
20   * 02111-1307, USA.
21   *
22   * Send bugs, suggestions or queries to <jplot@cig.ensmp.fr>
23   * The latest releases are found at 
24   * http://www.cig.ensmp.fr/~vanderlee/jplot.html
25   *
26   * Initally developed for use by the Centre d'Informatique Geologique 
27   * Ecole des Mines de Paris, Fontainebleau, France.
28   */
29  
30  package jplot;
31  
32  import java.awt.*;
33  import java.util.*;
34  import javax.swing.*;
35  import java.awt.geom.*;
36  import java.awt.event.*;
37  import javax.swing.border.*;
38  import javax.swing.colorchooser.*;
39  
40  import java.io.*;
41  import com.sun.image.codec.jpeg.*;
42  import java.awt.image.BufferedImage;
43  
44  
45  /**
46   * SavePanel build a panel used to save the graph to some format (JPEG
47   * for the time being, waiting for a decent EPS/PS decoder).
48   *
49   * Extends JPanel and not JDialog since this allows to include, if
50   * needed later, the panel within another widget.
51   * Calling the <code>show</code> method pops up a frame (dialog) which
52   * includes the panel.
53   */
54  public class SavePanel extends GriddedPanel {
55  
56    private JDialog dialog;
57  
58    private JComboBox format;
59    private JTextField saveTo;
60    private JTextField imageWidth;
61    private JTextField imageHeight;
62  
63    private final String[] formats = {"jpeg"};
64  
65    private final Dimension mediumField = new Dimension(160,26);
66    private final Dimension shortField = new Dimension(70,26);
67  
68    private Graph graph;
69  
70    /**
71     * Principal constructor, builds the panel.
72     */
73    public SavePanel (Graph g) {
74      dialog = null;
75      graph = g;
76      //setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
77  
78      // build the widgets:
79      EmptyBorder border = new EmptyBorder(new Insets(0,0,0,10));
80      JLabel label = new JLabel("Format:");
81      label.setBorder(border);
82      format = new JComboBox(formats);
83      format.setPreferredSize(mediumField);
84      format.addActionListener(new ActionListener() {
85        public void actionPerformed(ActionEvent e) {
86    // TODO: add action
87        }
88      });
89      addComponent(label,1,1);
90      addFilledComponent(format,1,2,2,1,GridBagConstraints.HORIZONTAL);
91  
92      label = new JLabel("Save to:");
93      label.setBorder(border);
94      saveTo = new JTextField("out.jpg");
95      saveTo.setPreferredSize(mediumField);
96      addComponent(label,2,1);
97      addFilledComponent(saveTo,2,2,2,1,GridBagConstraints.HORIZONTAL);
98      //addComponent(saveTo,2,2);
99  
100     label = new JLabel("Image size:");
101     label.setBorder(border);
102     JPanel p = new JPanel(new BorderLayout());
103     p.add(Utils.makeLabel("width"),BorderLayout.WEST);
104     imageWidth = new JTextField("570");
105     p.add(imageWidth,BorderLayout.CENTER);
106     p.setPreferredSize(shortField);
107     addComponent(label,3,1);
108     addComponent(p,3,2);
109 
110     p = new JPanel(new BorderLayout());
111     p.add(Utils.makeLabel("height"),BorderLayout.WEST);
112     imageHeight = new JTextField("410");
113     p.add(imageHeight,BorderLayout.CENTER);
114     p.setPreferredSize(shortField);
115     addComponent(p,3,3);
116   }
117 
118   /**
119    * Builds a panel with the scaling options for one of the axes.
120    * @param axis axis for which this panel is built.
121    */
122   /*
123   private JPanel getJpegPanel() {
124     GriddedPanel p = new GriddedPanel();
125     p.setBorder(new EmptyBorder(new Insets(5,5,5,5)));
126 
127     return p;
128   }
129   */
130 
131   /**
132    * Saves the current image to a JPG. Draws the image off-screen
133    * using the specfied width/height.
134    */
135   public void saveJPG() {
136     if (saveTo.getText().equals("")) return;
137     int width = Integer.parseInt(imageWidth.getText());
138     int height = Integer.parseInt(imageHeight.getText());
139     Dimension size = new Dimension(width,height);
140     
141     try {
142       File file = new File(saveTo.getText());
143       FileOutputStream out = new FileOutputStream(file);
144       JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
145 
146       BufferedImage bi = graph.getBufferedImage(size);
147       JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
148       param.setQuality(1.0f,false);
149       encoder.setJPEGEncodeParam(param);
150       encoder.encode(bi);
151     } catch (Exception ex) {
152       ex.printStackTrace();
153     }
154   }
155 
156   /**
157    * Updates the panel with the current graphical parameters.
158    */
159   public void refresh() {
160   }
161 
162   public void setValues() {
163   }
164 
165  /**
166    * Return a modal JDialog.
167    * @param parent frame which is the parent of this dialog.
168    * @param x x-position of the upper left corner
169    * @param y y-position of the upper left corner
170    */
171   public void show(Frame parent, int x, int y) {
172     if (dialog == null) {
173       dialog = new JDialog(parent,"Save graph",false);
174       dialog.addWindowListener(new WindowAdapter() {
175   public void windowClosing(WindowEvent e) {
176     dialog.dispose();
177   }
178       });
179 
180       JPanel p = new JPanel(new FlowLayout());
181       p.setBorder(BorderFactory.createEtchedBorder());
182       JButton b = new JButton("Apply");
183       b.addActionListener(new ActionListener() {
184     public void actionPerformed(ActionEvent e) {
185       saveJPG();
186       dialog.dispose();
187     }
188   });
189       p.add(b);
190       b = new JButton("Dismiss");
191       b.addActionListener(new ActionListener() {
192     public void actionPerformed(ActionEvent e) {
193       dialog.dispose();
194     }
195   });
196       p.add(b);
197       dialog.getContentPane().add(this,BorderLayout.CENTER);
198       dialog.getContentPane().add(p,BorderLayout.SOUTH);
199       dialog.setLocation(x,y);
200       dialog.pack();
201     }
202     refresh();
203     dialog.show();  // blocks until user brings dialog down.
204   }
205 }
206 
207 
208 
209 
210 
211 
212 
213 
214