Printing figure with Java2D
Sylvestre Koumar [Tue, 19 Aug 2008 08:16:18 +0000 (08:16 +0000)]
+ check style corrections

13 files changed:
scilab/modules/graphic_export/src/java/org/scilab/modules/graphic_export/ExportBitmap.java
scilab/modules/gui/src/java/org/scilab/modules/gui/bridge/CallScilabBridge.java
scilab/modules/gui/src/java/org/scilab/modules/gui/bridge/ScilabBridge.java
scilab/modules/gui/src/java/org/scilab/modules/gui/bridge/ScilabPrint.java [new file with mode: 0644]
scilab/modules/gui/src/java/org/scilab/modules/gui/bridge/canvas/GLHybrid.java
scilab/modules/gui/src/java/org/scilab/modules/gui/bridge/canvas/ScrolledSwingScilabCanvas.java
scilab/modules/gui/src/java/org/scilab/modules/gui/bridge/canvas/SwingScilabCanvas.java
scilab/modules/gui/src/java/org/scilab/modules/gui/bridge/filechooser/SwingScilabExportFileChooser.java
scilab/modules/gui/src/java/org/scilab/modules/gui/canvas/Canvas.java
scilab/modules/gui/src/java/org/scilab/modules/gui/canvas/ScilabCanvas.java
scilab/modules/gui/src/java/org/scilab/modules/gui/canvas/ScilabCanvasBridge.java
scilab/modules/gui/src/java/org/scilab/modules/gui/canvas/SimpleCanvas.java
scilab/modules/gui/src/java/org/scilab/modules/gui/filechooser/ScilabFileChooser.java

index 51e18e5..f90dec1 100644 (file)
@@ -14,7 +14,9 @@ package org.scilab.modules.graphic_export;
 
 import java.io.File;
 import java.io.IOException;
+
 import javax.media.opengl.GLException;
+
 import com.sun.opengl.util.Screenshot;
 
 /**
@@ -59,7 +61,7 @@ public class ExportBitmap extends ExportToFile {
                        /** Generate the screen-shot */         
                        //Check if we have the permission to export                     
                        if (checkWritePermission(file) == ExportRenderer.SUCCESS) {
-                               Screenshot.writeToFile(file, getWidth(), getHeight());
+                               Screenshot.writeToFile(file, getWidth(), getHeight());                          
                        } else {
                                return ExportRenderer.INVALID_FILE;
                        }
index 0128172..be3ee83 100644 (file)
@@ -33,17 +33,15 @@ import javax.print.DocPrintJob;
 import javax.print.PrintException;
 import javax.print.SimpleDoc;
 import javax.print.attribute.HashPrintRequestAttributeSet;
-import javax.print.attribute.PrintRequestAttribute;
 import javax.print.attribute.PrintRequestAttributeSet;
 import javax.print.attribute.standard.OrientationRequested;
 import javax.swing.text.BadLocationException;
 import javax.swing.text.StyledDocument;
 
 import org.scilab.modules.console.SciConsole;
-import org.scilab.modules.graphic_export.ExportRenderer;
-import org.scilab.modules.graphic_export.FileExporter;
 import org.scilab.modules.gui.bridge.console.SwingScilabConsole;
 import org.scilab.modules.gui.bridge.tab.SwingScilabTab;
+import org.scilab.modules.gui.canvas.Canvas;
 import org.scilab.modules.gui.checkbox.CheckBox;
 import org.scilab.modules.gui.checkbox.ScilabCheckBox;
 import org.scilab.modules.gui.colorchooser.ColorChooser;
@@ -2298,60 +2296,19 @@ public class CallScilabBridge {
        public static boolean printFigure(int figID, boolean postScript, boolean displayDialog) {
                final int figureID = figID;
                // Get the PrinterJob object
-               PrinterJob printerJob = PrinterJob.getPrinterJob();
-               
-               int exportRendererMode = ExportRenderer.EPS_EXPORT;
-               DocFlavor printDocFlavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;
-               String fileExtension = ".eps";
-               
-               if (!postScript) {
-                       exportRendererMode = ExportRenderer.PNG_EXPORT;
-                       printDocFlavor = DocFlavor.INPUT_STREAM.PNG;
-                       fileExtension = ".png";
-               }
+               PrinterJob printerJob = PrinterJob.getPrinterJob();     
 
                boolean userOK = true;
                if (displayDialog) {
                        userOK = printerJob.printDialog(scilabPageFormat);
                }
-               
-               if (userOK) {
-                       try {
-                               /** Export image to PostScript */
-                               if (((PrintRequestAttribute) scilabPageFormat.get(OrientationRequested.class)) == OrientationRequested.PORTRAIT) {
-                                       FileExporter.fileExport(figureID, 
-                                                       tmpPrinterFile + fileExtension,
-                                                       exportRendererMode, 0);
-                               } else {
-                                       FileExporter.fileExport(figureID, 
-                                                       tmpPrinterFile + fileExtension,
-                                                       exportRendererMode, 1);
-                               }
-                               
-                               /** Read file */
-                               FileInputStream psStream = null; 
-                               try { 
-                                       psStream = new FileInputStream(tmpPrinterFile + fileExtension);
-                               } catch (FileNotFoundException ffne) {
-                                       ffne.printStackTrace();
-                                       return false;
-                               }
-
-                               Doc myDoc = new SimpleDoc(psStream, printDocFlavor, null);
-                               DocPrintJob job = printerJob.getPrintService().createPrintJob();
-
-                               // Remove Orientation option from page setup because already managed in FileExporter
-                               PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(scilabPageFormat);
-                               aset.add(OrientationRequested.PORTRAIT);
-
-                               job.print(myDoc, aset);
-                               return true;
-                       } catch (PrintException e) {
-                               e.printStackTrace();
-                               return false;
-                       }
+
+               if (userOK) {                                   
+                       Canvas canvas;          
+                       canvas = ((ScilabRendererProperties) FigureMapper.getCorrespondingFigure(figureID).getRendererProperties()).getCanvas();
+                   ScilabPrint scilabPrint = new ScilabPrint(canvas.dumpAsBufferedImage());            
                }
-               return false;
+               return false;                                   
        }
 
        /**
@@ -2764,4 +2721,5 @@ public class CallScilabBridge {
                ((ScilabRendererProperties) FigureMapper.getCorrespondingFigure(id).getRendererProperties()).getParentTab().setCurrent();
                
        }
+
 }
index 8eefde0..bacfefd 100644 (file)
@@ -17,10 +17,12 @@ package org.scilab.modules.gui.bridge;
 import java.awt.Color;
 import java.awt.Cursor;
 import java.awt.Font;
+import java.awt.image.BufferedImage;
 
 import javax.media.opengl.GL;
 
 import org.scilab.modules.gui.canvas.Canvas;
+import org.scilab.modules.gui.canvas.ScilabCanvas;
 import org.scilab.modules.gui.canvas.ScilabCanvasBridge;
 import org.scilab.modules.gui.canvas.SimpleCanvas;
 import org.scilab.modules.gui.checkbox.CheckBox;
@@ -1408,6 +1410,15 @@ public class ScilabBridge {
                ScilabCanvasBridge.close(canvas);
        }
        
+       /**
+        * Dump the scilabCanvas to an BufferedImage
+        * @param scilabCanvas ScilabCanvas
+        * @return a BufferedImage
+        */
+       public static BufferedImage dumpAsBufferedImage(ScilabCanvas scilabCanvas) {
+               return ScilabCanvasBridge.dumpAsBufferedImage(scilabCanvas);
+       }
+       
        /*****************/
        /* MenuBar Bridge */
        /*****************/
diff --git a/scilab/modules/gui/src/java/org/scilab/modules/gui/bridge/ScilabPrint.java b/scilab/modules/gui/src/java/org/scilab/modules/gui/bridge/ScilabPrint.java
new file mode 100644 (file)
index 0000000..921576a
--- /dev/null
@@ -0,0 +1,78 @@
+/**\r
+ * \r
+ */\r
+package org.scilab.modules.gui.bridge;\r
+\r
+import java.awt.Graphics;\r
+import java.awt.Graphics2D;\r
+import java.awt.geom.AffineTransform;\r
+import java.awt.image.BufferedImage;\r
+import java.awt.print.PageFormat;\r
+import java.awt.print.Paper;\r
+import java.awt.print.Printable;\r
+import java.awt.print.PrinterException;\r
+import java.awt.print.PrinterJob;\r
+\r
+/**\r
+ * This class allow us to print a figure using Java2D\r
+ * @author Sylvestre Koumar\r
+ *\r
+ */\r
+public class ScilabPrint implements Printable {\r
+\r
+       private final int PRINT_MARGIN = 36; \r
+       \r
+       private BufferedImage canvasDump;\r
+\r
+       /**\r
+        * Default Constructor\r
+        * Calling print job to print the BufferedImage\r
+        * @param canvasDump BufferedImage\r
+        */\r
+       public ScilabPrint(BufferedImage canvasDump) {\r
+               this.canvasDump = canvasDump;\r
+\r
+               PrinterJob pj = PrinterJob.getPrinterJob();\r
+               PageFormat pf = pj.defaultPage();\r
+               \r
+               //setting the page format\r
+               Paper paper = new Paper();\r
+           double margin = PRINT_MARGIN; // half inch\r
+           paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight()\r
+               - margin * 2);\r
+           pf.setPaper(paper);\r
+               \r
+               pj.setPrintable(this, pf);\r
+               if (pj.printDialog()) {\r
+                       try {\r
+                               pj.print(); \r
+                       } catch (PrinterException pe) {\r
+                               System.out.println("Printing Error");\r
+                               System.err.println(pe.toString());\r
+                       }\r
+               }\r
+\r
+       }\r
+\r
+       /**\r
+        * Printing by converting the buffered image to Graphics2D\r
+        * @param g Graphics\r
+        * @param pf PageFormat\r
+        * @param page int\r
+        * @return Printable.PAGE_EXISTS if the printing instruction exist\r
+        * @throws PrinterException indicate that an exceptional condition \r
+        * has occurred in the print system\r
+        */\r
+       public int print(Graphics g, PageFormat pf, int page)\r
+       throws PrinterException {\r
+               if (page >= 1) {\r
+                       return Printable.NO_SUCH_PAGE;\r
+               }       \r
+               \r
+               AffineTransform at = AffineTransform.getTranslateInstance(0, 0);                \r
+               ((Graphics2D) g).drawRenderedImage(canvasDump, at);\r
+               \r
+               return Printable.PAGE_EXISTS;\r
+       }\r
+\r
+}\r
index a9e3773..2b2a939 100644 (file)
@@ -156,8 +156,7 @@ public class GLHybrid extends JPanel implements GLAutoDrawable {
            /** --- MY BEAUTIFUL SCREENSHOT --- */
            tac = System.currentTimeMillis();
            time("My screenshot...", tic, tac);
-       }
-       else {
+       } else {
            tic = System.currentTimeMillis();
            /** --- JOGL SCREENSHOT --- */
            dump = Screenshot.readToBufferedImage(getWidth(), getHeight());
index 1a04822..703b426 100644 (file)
@@ -15,6 +15,7 @@ package org.scilab.modules.gui.bridge.canvas;
 import java.awt.Color;
 import java.awt.Point;
 import java.awt.Rectangle;
+import java.awt.image.BufferedImage;
 import java.lang.reflect.InvocationTargetException;
 
 import javax.media.opengl.GL;
@@ -24,7 +25,6 @@ import javax.swing.SwingUtilities;
 
 import org.scilab.modules.gui.canvas.SimpleCanvas;
 import org.scilab.modules.gui.events.ScilabEventListener;
-import org.scilab.modules.gui.events.ScilabRubberBox;
 import org.scilab.modules.gui.utils.Position;
 import org.scilab.modules.gui.utils.Size;
 
@@ -351,4 +351,12 @@ public class ScrolledSwingScilabCanvas extends JScrollPane implements SimpleCanv
                        e.printStackTrace();
                }
        }
+       
+       /**
+        * Convert a Canvas to an BufferedImage
+        * @return a BufferedImage
+        */
+       public BufferedImage dumpAsBufferedImage() {
+               return canvas.dumpAsBufferedImage();
+       }
 }
index b1b183a..fd2a563 100644 (file)
@@ -17,6 +17,7 @@ package org.scilab.modules.gui.bridge.canvas;
 import java.awt.AWTEvent;
 import java.awt.Color;
 import java.awt.Dimension;
+import java.awt.image.BufferedImage;
 
 import javax.media.opengl.GLCapabilities;
 import javax.media.opengl.GLEventListener;
@@ -31,6 +32,8 @@ import org.scilab.modules.renderer.FigureMapper;
 import org.scilab.modules.renderer.figureDrawing.DrawableFigureGL;
 import org.scilab.modules.renderer.figureDrawing.SciRenderer;
 
+import com.sun.opengl.util.Screenshot;
+
 /**
  * Swing implementation for Scilab Canvas in GUIs This implementation requires
  * JOGL
@@ -275,5 +278,15 @@ public class SwingScilabCanvas extends GLJPanel implements SimpleCanvas {
                this.removeAll();
        }
        
+       /**
+        * Take a screenshot of the figure and put it into a BufferedImage
+        * @return a BufferedImage
+        */
+       public BufferedImage dumpAsBufferedImage() {                    
+               getContext().makeCurrent();             
+               BufferedImage dump = Screenshot.readToBufferedImage(getWidth(), getHeight());           
+               getContext().release();         
 
-}
+               return dump;
+       }       
+}      
index 3a3f2ab..28c03fe 100644 (file)
@@ -175,8 +175,9 @@ public class SwingScilabExportFileChooser extends SwingScilabFileChooser {
        
        
        /**
-        *Return the file extension
-        * @param fileName
+        * Return the file extension
+        * @param fileName Name of the file
+        * @return the extension
         */
        public String getExtension(String fileName) {
                if(fileName != null) {
index 0a8ab46..50e4f60 100644 (file)
@@ -12,6 +12,8 @@
 
 package org.scilab.modules.gui.canvas;
 
+import java.awt.image.BufferedImage;
+
 import javax.media.opengl.GL;
 
 import org.scilab.modules.gui.dockable.Dockable;
@@ -131,4 +133,10 @@ public interface Canvas extends Dockable {
          * Disable the canvas befor closing
          */
        void close();
+
+       /**
+        * Convert to a BufferedImage
+        * @return BufferedImage
+        */
+       BufferedImage dumpAsBufferedImage();
 }
index 371e6e9..9f3a275 100644 (file)
@@ -14,6 +14,8 @@
 
 package org.scilab.modules.gui.canvas;
 
+import java.awt.image.BufferedImage;
+
 import javax.media.opengl.GL;
 
 import org.scilab.modules.gui.bridge.ScilabBridge;
@@ -271,5 +273,13 @@ public class ScilabCanvas extends ScilabDockable implements Canvas {
        public void close() {
                ScilabBridge.close(this);
        }
+
+       /**
+        * Convert to a BufferedImage
+        * @return BufferedImage
+        */
+       public BufferedImage dumpAsBufferedImage() {
+                               return ScilabBridge.dumpAsBufferedImage(this);
+       }
        
 }
index 8b0ae1c..0fa6807 100644 (file)
@@ -12,6 +12,9 @@
 
 package org.scilab.modules.gui.canvas;
 
+import java.awt.Graphics2D;
+import java.awt.image.BufferedImage;
+
 import javax.media.opengl.GL;
 
 import org.scilab.modules.gui.bridge.canvas.ScrolledSwingScilabCanvas;
@@ -256,4 +259,13 @@ public class ScilabCanvasBridge {
        public static void close(Canvas canvas) {
                canvas.getAsSimpleCanvas().close();
        }
+
+       /**
+        * Convert a ScilabCanvas to a BufferedImage
+        * @param scilabCanvas ScilabCanvas
+        * @return BufferedImage
+        */
+       public static BufferedImage dumpAsBufferedImage(ScilabCanvas scilabCanvas) {
+               return scilabCanvas.getAsSimpleCanvas().dumpAsBufferedImage();
+       }
 }
index 42aafa9..96d1f8b 100644 (file)
@@ -12,6 +12,8 @@
 
 package org.scilab.modules.gui.canvas;
 
+import java.awt.image.BufferedImage;
+
 import javax.media.opengl.GL;
 
 import org.scilab.modules.gui.utils.Position;
@@ -167,4 +169,10 @@ public interface SimpleCanvas {
          * Disable the canvas befor closing
          */
        void close();
+
+       /**
+        * Convert to a BufferedImage
+        * @return BufferedImage
+        */
+       BufferedImage dumpAsBufferedImage();
 }
index cb361c5..b2594b8 100644 (file)
@@ -54,6 +54,7 @@ public class ScilabFileChooser extends ScilabUIElement implements FileChooser {
        
        /**
         * Creates a Scilab ExportFileChooser
+        * @param figureId int
         * @return the created export file chooser
         */
        public static FileChooser createExportFileChooser(int figureId) {