KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jawe > actions > SaveAsJPG


1 package org.enhydra.jawe.actions;
2
3 import org.enhydra.jawe.*;
4
5 import java.awt.*;
6 import java.awt.event.ActionEvent JavaDoc;
7 import java.awt.image.BufferedImage JavaDoc;
8 import java.io.*;
9 import java.util.*;
10
11 import javax.swing.*;
12
13 import com.sun.image.codec.jpeg.*;
14
15 public class SaveAsJPG extends ActionBase {
16
17    public SaveAsJPG (AbstractEditor editor) {
18       super(editor);
19    }
20
21    public void actionPerformed(ActionEvent JavaDoc e) {
22       try {
23          String JavaDoc file =
24             JaWE.getInstance().saveDialog(
25                ResourceManager.getLanguageDependentString("SaveAsJPGLabel"),1,
26                editor.getGraph().get("Id").toString());
27          if (file!=null && file.length()>0) {
28             saveGraphAsJPG(file,editor.getGraph());
29          }
30       } catch (Exception JavaDoc ex) {
31          String JavaDoc msg=ResourceManager.getLanguageDependentString("ErrorJPGSavingFailed");
32          JaWE.getInstance().message(msg,JOptionPane.WARNING_MESSAGE);
33       }
34    }
35
36    public static void saveGraphAsJPG (String JavaDoc file,AbstractGraph graph) throws Exception JavaDoc {
37       BufferedImage JavaDoc img = null;
38       Object JavaDoc[] cells = graph.getRoots();
39
40       if (cells.length > 0) {
41          Rectangle bounds = graph.getCellBounds(cells).getBounds();//HM, JGraph3.4.1
42
graph.toScreen(bounds);
43
44          // Create a Buffered Image
45
Dimension d = bounds.getSize();
46          img = new BufferedImage JavaDoc(d.width,d.height,BufferedImage.TYPE_INT_RGB);
47          Graphics2D graphics = img.createGraphics();
48          graph.paint(graphics);
49       }
50
51       FileOutputStream fos = new FileOutputStream(file);
52       JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
53       encoder.encode(img);
54       fos.flush();
55       fos.close();
56    }
57
58 }
59
Popular Tags