0

I have a jframe with a jgraph init, and I have added a jmenu bar. I am trying this code to add a jmenu item, so that when it is clicked it will export the jgraph as image in the selected location.

I get the following errors:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: image == null!
    at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(Unknown Source)
    at javax.imageio.ImageIO.getWriter(Unknown Source)
    at javax.imageio.ImageIO.write(Unknown Source)
    at GUIquery$2.actionPerformed(GUIquery.java:440)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.AbstractButton.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

And the code:

final static mxGraph graph = new mxGraph() ;
final static mxGraphComponent graphComponent = new mxGraphComponent(graph);

public class GUI extends JFrame  { 
....
public static JMenuBar createMenuBar() { 

JMenuBar menuBar;
                JMenu menu, submenu;
                JMenuItem menuItem;

                //dimiurgia tu menubar

                menuBar = new JMenuBar();

                //1o menu
                menu = new JMenu("Menu");
                menu.setMnemonic(KeyEvent.VK_A);
                menu.getAccessibleContext().setAccessibleDescription("Info");
                menuBar.add(menu);

                // menu items

                menuItem = new JMenuItem("Export as Image",KeyEvent.VK_T);
       menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
  menuItem.getAccessibleContext().setAccessibleDescription("Saves Diagram as Image (.PNG)");
                menuItem.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent event) {


                        try {

                            String filename ="Image";

                            exportImage(filename, graph, graphComponent);


                        }catch (IOException e) {
                            System.out.println("Image could not be read");
                            System.exit(1);
                        }

                    }
                });

                menu.add(menuItem);
                menu.addSeparator();

                //submenu

                submenu = new JMenu("Preferences");
                submenu.setMnemonic(KeyEvent.VK_S);

                 menuItem = new JMenuItem("Option A");
                 menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.ALT_MASK));
                 submenu.add(menuItem);

                menuItem = new JMenuItem("Option B");
                menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_3, ActionEvent.ALT_MASK));
                submenu.add(menuItem);

                menu.add(submenu);

                // 2o menu

                menu = new JMenu("View");
                menu.setMnemonic(KeyEvent.VK_N);
                menu.getAccessibleContext().setAccessibleDescription( "View");
                menuBar.add(menu);

                menuItem = new JMenuItem("Info",KeyEvent.VK_A);
                menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_4, ActionEvent.ALT_MASK));
                menuItem.getAccessibleContext().setAccessibleDescription("Info");

                menu.add(menuItem);

                menuItem = new JMenuItem("Flow",KeyEvent.VK_B);
                menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_5, ActionEvent.ALT_MASK));
                menuItem.getAccessibleContext().setAccessibleDescription("Flow");

                menu.add(menuItem);


          menuBar.add(menu);
          return menuBar;

            }


            protected static void exportImage(String filename, mxGraph graph, mxGraphComponent graphComponent) throws IOException{
                BufferedImage image = mxCellRenderer.createBufferedImage(graph, null, 1, Color.WHITE, graphComponent.isAntiAlias(), null,graphComponent.getCanvas());

                // Creates the URL-encoded XML data
                mxCodec codec = new mxCodec();
                String xml = URLEncoder.encode(mxXmlUtils.getXml(codec.encode(graph.getModel())), "UTF-8");

                mxPngEncodeParam param = mxPngEncodeParam.getDefaultEncodeParam(image);
                param.setCompressedText(new String[] { "graph", xml });

                // Saves as a PNG file
                FileOutputStream outputStream = new FileOutputStream(new File(filename));

                try
                {
                    mxPngImageEncoder encoder = new mxPngImageEncoder(outputStream,param);

                    if (image != null)
                    {
                        encoder.encode(image);
                    }
                    else
                    {
                        System.out.println("No Image");
                    }
                }
                finally
                {
                    outputStream.close();
                }
            }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user2598911
  • 379
  • 2
  • 6
  • 22

1 Answers1

2

This may not be the best answear you expect, but it looks like one needs to pay for jgraph to get it. Until some jgraph library onwer answears, maybe it'll give you a hint.

Exception says "java.lang.IllegalArgumentException: image == null!" and I would blaim one of null parameters or malfunction here:

    BufferedImage image = mxCellRenderer.createBufferedImage(graph, null, 1, Color.WHITE,
    graphComponent.isAntiAlias(), null,graphComponent.getCanvas());

You can verify by putting null check like this:

    mxPngEncodeParam param = null;

    if(image != null) {
       param = mxPngEncodeParam.getDefaultEncodeParam(image);
    } else {
       System.out.println("No Image in pngparam creation");
       System.exit(1);
    }

or simply putting some breakpoints in debug mode.

HTH

Bolesław Denk
  • 553
  • 7
  • 15
  • Now it wont show anything. It simply wont save the graph but without showing any errors. @Bolesław Denk – user2598911 Sep 19 '13 at 22:17
  • No system console output during pngparam creation? If no exception is risen, then it sounds like this somewhat caught the exception and finished program. Are you shure that BufferedImage image isn't null? – Bolesław Denk Sep 19 '13 at 22:28
  • Nope its not null. When I execute it, it will just run but wont terminate, giving no results back. @Bolesław Denk – user2598911 Sep 19 '13 at 22:34
  • I think i havent defined where to save the picture..? – user2598911 Sep 19 '13 at 22:38
  • There are common cases when File defined like this, is created in your program location or project folder in workspace, but ofcource for the sake of safety you can add full path anytime. @user2598911 – Bolesław Denk Sep 19 '13 at 22:50
  • Yes it was saved in the projects folder. You are right. But not sure why, but the vertices labels are not shown! why? @Bolesław Denk – user2598911 Sep 19 '13 at 23:11
  • I think that's more specific about mxGraph, which I don't know yet. If I get the evaluation copy from producer, I'll try to play around with it. @user2598911 – Bolesław Denk Sep 20 '13 at 16:21