Java Code Examples for org.apache.xmlgraphics.java2d.ps.EPSDocumentGraphics2D#setupDocument()

The following examples show how to use org.apache.xmlgraphics.java2d.ps.EPSDocumentGraphics2D#setupDocument() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SaveImage.java    From mzmine3 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void run() {

  try {

    if (fileType.equals(FileType.EMF)) {
      OutputStream out2 = new java.io.FileOutputStream(file);
      EMFGraphics2D g2d2 = new EMFGraphics2D(out2, new Dimension(width, height));
      g2d2.startExport();
      chart.draw(g2d2, new Rectangle(width, height));
      g2d2.endExport();
      g2d2.closeStream();
    }

    if (fileType.equals(FileType.EPS)) {
      OutputStream out = new java.io.FileOutputStream(file);
      EPSDocumentGraphics2D g2d = new EPSDocumentGraphics2D(false);
      g2d.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
      g2d.setupDocument(out, width, height);
      chart.draw(g2d, new Rectangle(width, height));
      g2d.finish();
      out.flush();
      out.close();
    }
  } catch (IOException e) {
    MZmineCore.getDesktop().displayErrorMessage("Unable to save image.");
    e.printStackTrace();
  }
}
 
Example 2
Source File: SaveImage.java    From mzmine2 with GNU General Public License v2.0 5 votes vote down vote up
public void run() {

    try {

      if (fileType.equals(FileType.EMF)) {
        OutputStream out2 = new java.io.FileOutputStream(file);
        EMFGraphics2D g2d2 = new EMFGraphics2D(out2, new Dimension(width, height));
        g2d2.startExport();
        chart.draw(g2d2, new Rectangle(width, height));
        g2d2.endExport();
        g2d2.closeStream();
      }

      if (fileType.equals(FileType.EPS)) {
        OutputStream out = new java.io.FileOutputStream(file);
        EPSDocumentGraphics2D g2d = new EPSDocumentGraphics2D(false);
        g2d.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
        g2d.setupDocument(out, width, height);
        chart.draw(g2d, new Rectangle(width, height));
        g2d.finish();
        out.flush();
        out.close();
      }
    } catch (IOException e) {
      MZmineCore.getDesktop().displayErrorMessage(MZmineCore.getDesktop().getMainWindow(),
          "Unable to save image.");
      e.printStackTrace();
    }
  }
 
Example 3
Source File: JFreeChartUtils.java    From old-mzmine3 with GNU General Public License v2.0 4 votes vote down vote up
public static void exportToImageFile(ChartViewer chartNode, File file, ImgFileType fileType) {

    final JFreeChart chart = chartNode.getChart();
    final int width = (int) chartNode.getWidth();
    final int height = (int) chartNode.getHeight();

    try {

      switch (fileType) {

        case JPG:
          ExportUtils.writeAsJPEG(chart, width, height, file);
          break;

        case PNG:
          ExportUtils.writeAsPNG(chart, width, height, file);
          break;

        case SVG:
          setDrawSeriesLineAsPath(chart, true);
          ExportUtils.writeAsSVG(chart, width, height, file);
          setDrawSeriesLineAsPath(chart, false);
          break;

        case PDF:
          setDrawSeriesLineAsPath(chart, true);
          ExportUtils.writeAsPDF(chart, width, height, file);
          setDrawSeriesLineAsPath(chart, false);
          break;

        case EMF:
          FileOutputStream out2 = new FileOutputStream(file);
          setDrawSeriesLineAsPath(chart, true);
          EMFGraphics2D g2d2 = new EMFGraphics2D(out2, new Dimension(width, height));
          g2d2.startExport();
          chart.draw(g2d2, new Rectangle(width, height));
          g2d2.endExport();
          setDrawSeriesLineAsPath(chart, false);
          break;

        case EPS:
          FileOutputStream out = new FileOutputStream(file);
          setDrawSeriesLineAsPath(chart, true);
          EPSDocumentGraphics2D g2d = new EPSDocumentGraphics2D(false);
          g2d.setGraphicContext(new GraphicContext());
          g2d.setupDocument(out, width, height);
          chart.draw(g2d, new Rectangle(width, height));
          g2d.finish();
          setDrawSeriesLineAsPath(chart, false);
          out.close();
          break;

      }

    } catch (IOException e) {
      MZmineGUI.displayMessage("Unable to save image: " + e.getMessage());
      e.printStackTrace();
    }
  }
 
Example 4
Source File: ChartPanel.java    From opensim-gui with Apache License 2.0 4 votes vote down vote up
/**
 * Prints the chart on a single page.
 *
 * @param g  the graphics context.
 * @param pf  the page format to use.
 * @param pageIndex  the index of the page. If not <code>0</code>, nothing 
 *                   gets print.
 *
 * @return The result of printing.
 */
public int print(Graphics g, PageFormat pf, int pageIndex) {

   if (pageIndex != 0) {
      return NO_SUCH_PAGE;
   }
   /** this works but the curve is made of little pieces */
   Graphics2D g2 = (Graphics2D) g;
   double x = pf.getImageableX();
   double y = pf.getImageableY();
   double w = pf.getImageableWidth();
   double h = pf.getImageableHeight();
   this.chart.draw(g2, new Rectangle2D.Double(x, y, w, h), this.anchor,
           null);
   
   if (printToPrinter)  return PAGE_EXISTS;
   // The rest should be moved up to the export eps action listener so it's not done per page'
   // Show export dialog
   JFileChooser fileChooser = new JFileChooser();
   ExtensionFileFilter filter = new ExtensionFileFilter(
           localizationResources.getString("PostScript_Files"), ".eps");
   fileChooser.addChoosableFileFilter(filter);
   String filename="";
   int option = fileChooser.showSaveDialog(this);
   if (option == JFileChooser.APPROVE_OPTION) {
      filename = fileChooser.getSelectedFile().getPath();
      if (isEnforceFileExtensions()) {
         if (!filename.endsWith(".eps") || !filename.endsWith(".ps")) {
            filename = filename + ".eps";
         }
      } else
         return NO_SUCH_PAGE;
   }
    
   try {
      OutputStream out = new java.io.FileOutputStream(new File(filename));
      out = new java.io.BufferedOutputStream(out);

      //Instantiate the EPSDocumentGraphics2D instance
      EPSDocumentGraphics2D g2dps = new EPSDocumentGraphics2D(false);
      g2dps.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
      //Set up the document size
      g2dps.setupDocument(out, (int)w, (int)h+200);

      //Paint a bounding box
      g2dps.drawRect((int)x, (int)y, (int)w, (int)h);
      this.chart.draw(g2dps, new Rectangle2D.Double(x, y, w, h), this.anchor,
              null);


      //A few rectangles rotated and with different color

      //Cleanup
      g2dps.finish();
      out.flush();
      out.close();
   }
   catch(java.io.IOException e){
      return NO_SUCH_PAGE;
   }
   return PAGE_EXISTS;
   
}