Menu

Save graph

Help
Ibrahim
2007-02-09
2013-04-25
  • Ibrahim

    Ibrahim - 2007-02-09

    Hi

    Can I save the resulted graph in PNG or JEPG format in a file, if yes please tell me how.

    thanks

    ibrahim

     
    • Jean-Daniel Fekete

      You can look at the createImage(File file, float scale) in infovis.panel.MainFrameDecorator

      Here is the code
      Component comp = visualization.getComponent();
              while (comp != null && ! (comp instanceof JScrollPane)) {
                  comp = comp.getParent();
              }
              if (comp == null) {
                  logger.error("Visualization not in a scroll pane");
                  return;
              }
              JScrollPane scroll = (JScrollPane) comp;
              Dimension savedSize = scroll.getSize();
              Rectangle savedBounds = scroll.getBounds();
              Rectangle bounds = new Rectangle(savedBounds);
              Dimension d = scroll.getPreferredSize();
              scroll.setSize(d);
              scroll.validate();
             
              bounds.x = 0;
              bounds.y = 0;
              bounds.width = (int) (d.width * scale);
              bounds.height = (int) (d.height * scale);
              BufferedImage image = null;
              try {
                  image = new BufferedImage(
                          bounds.width,
                          bounds.height,
                          BufferedImage.TYPE_INT_RGB);
              }
              catch(OutOfMemoryError e) {
                  logger.error("Out of memory creating image", e);
                  JOptionPane.showMessageDialog(
                          null,
                          "Error",
                          "Out of memory creating image",
                          JOptionPane.ERROR_MESSAGE);
                  //parent.add(comp);
                  scroll.setSize(savedSize);
                  scroll.validate();
                  return;
              }
              Graphics2D g2d = (Graphics2D) image.getGraphics();
      //        g2d.setColor(comp.getBackground());
      //        g2d.fill(bounds);
      //        visualization.paint(g2d, bounds);
              g2d.scale(scale, scale);
              comp.paint(g2d);

              try {
                  ImageIO.write(image, "png", file);
              } catch (IOException e) {
                  logger.error("Cannot write png file " + file, e);
              }
              image.flush();
              image = null;
              scroll.setSize(savedSize);
              scroll.validate();

       

Log in to post a comment.