gpt4 book ai didi

java - JMenu 项目将 jgraph 导出为图像

转载 作者:行者123 更新时间:2023-12-02 06:43:20 27 4
gpt4 key购买 nike

我有一个带有 jgraph init 的 jframe,并且添加了一个 jmenu 栏。我正在尝试使用此代码添加一个 jmenu 项,这样当单击它时,它将在选定位置将 jgraph 导出为图像。

我收到以下错误:

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)

代码:

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();
}
}

最佳答案

这可能不是您期望的最佳答案,但看起来需要付费购买 jgraph 才能获得它。直到一些 jgraph 库 onwer 回答,也许它会给你一个提示。

异常显示“java.lang.IllegalArgumentException: image == null!”我会在这里归咎于空参数之一或故障:

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

您可以通过像这样进行空检查来验证:

    mxPngEncodeParam param = null;

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

或者只是在 Debug模式下放置一些断点。

HTH

关于java - JMenu 项目将 jgraph 导出为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18904546/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com