1 package org.enhydra.jawe.actions; 2 3 import org.enhydra.jawe.*; 4 5 import java.awt.*; 6 import java.awt.event.ActionEvent ; 7 import java.awt.image.BufferedImage ; 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 e) { 22 try { 23 String 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 ex) { 31 String msg=ResourceManager.getLanguageDependentString("ErrorJPGSavingFailed"); 32 JaWE.getInstance().message(msg,JOptionPane.WARNING_MESSAGE); 33 } 34 } 35 36 public static void saveGraphAsJPG (String file,AbstractGraph graph) throws Exception { 37 BufferedImage img = null; 38 Object [] cells = graph.getRoots(); 39 40 if (cells.length > 0) { 41 Rectangle bounds = graph.getCellBounds(cells).getBounds(); graph.toScreen(bounds); 43 44 Dimension d = bounds.getSize(); 46 img = new BufferedImage (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 |