2 Replies Latest reply: Mar 14, 2013 12:47 PM by axvelazq RSS

    importing Graph to Image (oracle dss graph Graph)

    axvelazq
      I am trying to implement a Copy Image of a Graph(oracle.dss .graph.Graph) , My code is working but there is still an issue:
      * When copy and pasting the image, I get a black background due to the transparent background of the Graph. I tried to change the color of the background using the following:
      this._graph.setBackground(Colo.gray);
      But still not working. Any Idea of how to copy the Graph on the clipboard so that I don't get black background on the image pasted ?
      private oracle.dss.graph.Graph _graph;
      ...
      
                      public void actionPerformed(ActionEvent e) {
                          try{
                             ByteArrayOutputStream out = new ByteArrayOutputStream();
                             _graph.exportGraphToStream(out);
                             BufferedImage image  = ImageIO.read( new ByteArrayInputStream(out.toByteArray()));
                            CopyImagetoClipBoard ci = new CopyImagetoClipBoard(image);
                          }catch(Exception ex) {
                              theLogger.log(Level.SEVERE,ex.getMessage(),ex);
                          }
                      }
                  });
      ...
      
          class CopyImagetoClipBoard implements ClipboardOwner {
                  public CopyImagetoClipBoard(Image i) {
                      try {
      
                          TransferableImage trans = new TransferableImage( i );
                          Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
                          c.setContents( trans, this );
                      }
                      catch ( Exception x ) {
                          x.printStackTrace();
                          System.exit( 1 );
                      }
                  }
      ...
      Note that this should be in Memory, since It is only a "Copy Image" feature

      Edited by: user13679988 on Mar 13, 2013 3:04 PM
        • 1. Re: importing Graph to Image (oracle dss graph Graph)
          UdayaShankaraGandhi
          There may one of the below reasons that you are getting the background as black.
          either your graph image is not transparent and it is having background color as black or the background to which are placing your graph is itself having background color as black.

          If your graph image is having black background, to made it transparent you can use the below method
          public static Image makeColorTransparent(final BufferedImage im, final Color color)   
             {   
                final ImageFilter filter = new RGBImageFilter()   
                {   
                   // the color we are looking for (white)... Alpha bits are set to opaque   
                   public int markerRGB = color.getRGB() | 0xFFFFFFFF;   
            
                   public final int filterRGB(final int x, final int y, final int rgb)   
                   {   
                      if ((rgb | 0xFF000000) == markerRGB)   
                      {   
                         // Mark the alpha bits as zero - transparent   
                         return 0x00FFFFFF & rgb;   
                      }   
                      else  
                      {   
                         // nothing to do   
                         return rgb;   
                      }   
                   }   
                };   
            
                final ImageProducer ip = new FilteredImageSource(im.getSource(), filter);   
                return Toolkit.getDefaultToolkit().createImage(ip);   
             }   
          }  
          and call the above method by passing your BufferedImage and Color.Black like
          makeColorTransparent(bufferedImage, Color.BLACK);
          • 2. Re: importing Graph to Image (oracle dss graph Graph)
            axvelazq
            I'm almost sure the Graph background has transparent background.
            I have 2 available operations that can be done on the graph ( which are the ones that I'm trying to implement )
            1) Save Image As. which It will import the graph to a PNG Image, this one is working fine:
                    this.saveImageAs.addActionListener(new ActionListener() {
            
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                try{
                                JFileChooser fc = new JFileChooser(_graph.getTitle().getText());
                                //fc.addChoosableFileFilter(new ImageFilter());
                                    
                                int result =  fc.showDialog(Ide.getMainWindow(), "Save");
                                System.out.println();
                                _graph.setImageSize(new Dimension(1024,600));
                                _graph.exportToPNGWithException(new FileOutputStream(fc.getSelectedFile()));
                                 }catch(Exception ex){
                                        theLogger.log(Level.SEVERE,ex.getMessage(),ex);
                                    }
                            }
                        });
            If I open the image that was store after this, I see that the transparent background, so It looks good for me. so this is not a problem at all.

            2) the Second one ( Which is actually not working at all ) Is the Copy Image, which It is suppouse to copy the image to the clipboard so that the use can be able paste it on a word document, wordPad, or any other editor. Just as you can do on any image on a Webbrowser.
                    this.copyImage.addActionListener(new ActionListener(){
            
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                try{
                                ByteArrayOutputStream out = new ByteArrayOutputStream();
                                _graph.setImageSize(new Dimension(1024,600));
                                _graph.exportGraphToStream(out);
                                BufferedImage image  = ImageIO.read( new ByteArrayInputStream(out.toByteArray()));
                                 CopyImagetoClipBoard ci = new CopyImagetoClipBoard(image);
                                }catch(Exception ex) {
                                    theLogger.log(Level.SEVERE,ex.getMessage(),ex);
                                }
                            }
                        });
            As a result, when pasting the copied image to paint or to word or anywhere, I always get a black background, what looks horrible btw. this is a big issue.

            I try to use your method makeColorTransparent. but It caused my application to get frozen =/ not sure why.
            as follow:
              CopyImagetoClipBoard ci = new CopyImagetoClipBoard(makeColorTransparent(image,Color.BLACK));
            The reason why I believe the problem is not on the exportToStream but on the ImageIO.read is because exportToStream uses the exportToPNG that actually works fine to export the image ( since I used it on the Save Image As option )
                /**
                 * @internal
                 * Called when the graph must be rendered to the provided OutputStream
                 *
                 * @param out OutputStream object to use in exporting the graph
                 * @return boolean True if the export completed successfully, False otherwise
                 */
                public boolean exportGraphToStream(OutputStream out)
                {
                  this.exportToPNG(out);
                  return true;
            Take a look how It looks like to copy and paste on paint:
            [Graph after Copy and Paste on Paint|http://postimage.org/image/m9szt96vr/]
            http://s15.postimage.org/ybodneg4b/graph_After.png

            Now take a look how It looks like when exporting to Image png:
            [Graph Imported as PNG|http://postimage.org/image/4g3dgfic9/]
            http://s12.postimage.org/r4skfzzq5/Export_Img.png

            Edited by: user13679988 on Mar 14, 2013 10:46 AM