gpt4 book ai didi

java - 使用 JGraphx 在 xml 文件中保存和加载图形

转载 作者:行者123 更新时间:2023-11-30 06:11:35 25 4
gpt4 key购买 nike

在 JGraphx 中,可以绘制自定义顶点。我定义了自定义对象并将其用作顶点的值。要将图表保存并加载为 xml 文件,自定义对象已注册如下:

mxCodecRegistry.addPackage(the path of the custom object); 
mxCodecRegistry.register(new mxObjectCodec(new Myobject()));

enter image description here保存顶点时,将自定义对象保存为顶点的值。当我加载 xml 文件时,尽管顶点显示正确,但顶点的值返回 null。换句话说,图形显示很好,但是当我想读取对象的属性甚至移动它时,会出现以下错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.mxgraph.swing.handler.mxVertexHandler.createHandles(Unknown Source)
at com.mxgraph.swing.handler.mxCellHandler.refresh(Unknown Source)
at com.mxgraph.swing.handler.mxCellHandler.<init>(Unknown Source)
at com.mxgraph.swing.handler.mxVertexHandler.<init>(Unknown Source)
at com.mxgraph.swing.mxGraphComponent.createHandler(Unknown Source)
at com.mxgraph.swing.handler.mxSelectionCellsHandler.refresh(Unknown Source)
at com.mxgraph.swing.handler.mxSelectionCellsHandler$1.invoke(Unknown Source)
at com.mxgraph.util.mxEventSource.fireEvent(Unknown Source)
at com.mxgraph.util.mxEventSource.fireEvent(Unknown Source)
at com.mxgraph.view.mxGraphSelectionModel$mxSelectionChange.execute(Unknown Source)
at com.mxgraph.view.mxGraphSelectionModel.changeSelection(Unknown Source)
at com.mxgraph.view.mxGraphSelectionModel.setCells(Unknown Source)
at com.mxgraph.view.mxGraphSelectionModel.setCell(Unknown Source)
at com.mxgraph.view.mxGraph.setSelectionCell(Unknown Source)
at com.mxgraph.swing.mxGraphComponent.selectCellForEvent(Unknown Source)
at com.mxgraph.swing.handler.mxGraphHandler.mousePressed(Unknown Source)
at java.awt.AWTEventMulticaster.mousePressed(Unknown Source)
at java.awt.AWTEventMulticaster.mousePressed(Unknown Source)
at java.awt.AWTEventMulticaster.mousePressed(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$500(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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$JavaSecurityAccessImpl.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)

这表明该对象为空。我按照文档注册了我的对象。这是图形用户界面:

package graphicalUserInterface;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.Point;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.mxgraph.canvas.mxGraphics2DCanvas;
import com.mxgraph.io.mxCodecRegistry;
import com.mxgraph.io.mxObjectCodec;
import com.mxgraph.model.mxCell;
import com.mxgraph.shape.mxStencilShape;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxEvent;
import com.mxgraph.util.mxEventObject;
import com.mxgraph.util.mxUtils;
import com.mxgraph.util.mxEventSource.mxIEventListener;
import com.mxgraph.view.mxGraph;
import com.mxgraph.view.mxStylesheet;

import netElements.Valve;

public class GUIBoard extends JPanel {

private mxGraph _graph;
private mxGraphComponent _graphComponent;
private JPanel _panel;
private JFrame _frame;
private ArrayList<Valve> _valves;

public GUIBoard(JFrame frame) {
// TODO Auto-generated constructor stub
_frame=frame;
initialize();
}

private void initialize(){
_panel=new JPanel();
_panel.setLayout(new BoxLayout(_panel, BoxLayout.Y_AXIS));

addToRegistry("Images/valve.shape");
/**
* Define a new object to be saved in the Model
*/
mxCodecRegistry.addPackage(Valve.class.getPackage().toString());
mxCodecRegistry.register(new mxObjectCodec(new Valve()));


_graph=new mxGraph() {

public boolean isCellFoldable(Object cell, boolean collapse)
{
return false;
}

public String convertValueToString(Object cell)
{
Object value = null ;
if (cell instanceof mxCell)
{
value = ((mxCell) cell).getValue();

if (value instanceof Valve)
{
Valve v=(Valve) value;

return ((Valve) value).getElementName();

}
}

return super.convertValueToString(value);
}

public boolean isCellResizable(Object cell)
{
return !getModel().isVertex(cell);
}
};

_graph.addListener(mxEvent.MOVE_CELLS, new mxIEventListener()
{
@Override
public void invoke(Object sender, mxEventObject mxevt) {

Object[] cells = (Object[]) mxevt.getProperty("cells");
Point x = (Point) mxevt.getProperty("location");
Double dx = (Double) mxevt.getProperty("dx");
Double dy = (Double) mxevt.getProperty("dy");
}
});

setDefaultConnector();

_graphComponent=new mxGraphComponent(_graph);
_graphComponent.setPreferredSize(new Dimension(600, 400));
this.add(_graphComponent);

_graph.setVertexLabelsMovable(true);
_graph.setDisconnectOnMove(false);
_graph.setConnectableEdges(false);
_graph.setCellsEditable(false);
_graph.setResetEdgesOnMove(true);

_valves=new ArrayList<Valve>();

_panel.add(new GUIBoardToolBars(this).addFileToolBar());
_frame.add(new GUIBoardToolBars(this).addPowerElements(), BorderLayout.EAST);
_frame.add(_panel, BorderLayout.NORTH);

}

public mxGraph getGraph(){

return _graph;
}

public mxGraphComponent getGraphComponent(){

return _graphComponent;

}

private void setDefaultConnector() {
Map<String, Object> connector = new HashMap<String, Object>();
connector.put(mxConstants.STYLE_ROUNDED, false);
connector.put(mxConstants.STYLE_ORTHOGONAL, true);
connector.put(mxConstants.STYLE_EDGE, "elbowEdgeStyle");
connector.put(mxConstants.STYLE_SHAPE, mxConstants.SHAPE_CONNECTOR);
connector.put(mxConstants.STYLE_ENDARROW, mxConstants.NONE);


mxStylesheet connectorStyle = new mxStylesheet();
connectorStyle.setDefaultEdgeStyle(connector);
_graph.setStylesheet(connectorStyle);
}

public void addValves(Valve valve){

_valves.add(valve);
}

public ArrayList<Valve> getValves(){

return _valves;
}

public void addToRegistry(String element){
String nodeXml, objectName;
mxStencilShape newShape;
try {
nodeXml = mxUtils.readFile(element);
newShape = new mxStencilShape(nodeXml);
objectName = newShape.getName();
mxGraphics2DCanvas.putShape(objectName, newShape);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public GUIBoard(LayoutManager arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}

public GUIBoard(boolean arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}

public GUIBoard(LayoutManager arg0, boolean arg1) {
super(arg0, arg1);
// TODO Auto-generated constructor stub
}

}

GUI 操作是:

package graphicalUserInterface;

import java.awt.MouseInfo;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.swing.AbstractAction;

import org.w3c.dom.Document;

import com.mxgraph.io.mxCodec;
import com.mxgraph.io.mxCodecRegistry;
import com.mxgraph.io.mxObjectCodec;
import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxGeometry;
import com.mxgraph.util.mxUtils;
import com.mxgraph.util.mxXmlUtils;

import netElements.Valve;

public class GUIBoardActions {

public GUIBoardActions() {
// TODO Auto-generated constructor stub
}

public static class NewNetwork extends AbstractAction {

private GUIBoard _newNDB;
public NewNetwork(GUIBoard ndb) {
_newNDB=ndb;
}

@Override
public void actionPerformed(ActionEvent e) {

}

}

public static class OpenNetwork extends AbstractAction {

/**
*
*/
private static final long serialVersionUID = 8166859573134106478L;
private GUIBoard _openNDB;
private Document document;
mxCodec codec ;
public OpenNetwork(GUIBoard ndb) {
_openNDB=ndb;
mxCodecRegistry.addPackage(Valve.class.getPackage().toString());
mxCodecRegistry.register(new mxObjectCodec(new Valve()));

}

@Override
public void actionPerformed(ActionEvent aevt) {
mxCodecRegistry.addPackage(Valve.class.getPackage().toString());
mxCodecRegistry.register(new mxObjectCodec(new Valve()));

try {
document = mxXmlUtils.parseXml(mxUtils.readFile("C:\\test graph/file.xml"));
codec = new mxCodec(document);
codec.decode(document.getDocumentElement(), _openNDB.getGraph().getModel());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

_openNDB.getGraph().getModel().beginUpdate();
Object[] cells;
try
{
cells= _openNDB.getGraph().getChildCells(_openNDB.getGraph().getDefaultParent(), true, true);

}
finally
{
_openNDB.getGraph().getModel().endUpdate();
}

_openNDB.getGraph().addCells(cells);
for (Object c : cells) {
mxCell cell = (mxCell) c;
if (cell.isVertex()) {
if (cell.getValue() instanceof Valve) {
Valve valve=(Valve) cell.getValue();
_openNDB.addValves(valve);

}
}
}

}

}


public static class SaveNetwork extends AbstractAction {

/**
*
*/
private static final long serialVersionUID = 4757526430199667311L;
private GUIBoard _saveNDB;

public SaveNetwork(GUIBoard ndb) {
_saveNDB=ndb;
}

@Override
public void actionPerformed(ActionEvent aevt) {
mxCodec codec = new mxCodec();
try {

String xml2 = mxUtils.getPrettyXml(codec.encode(_saveNDB.getGraph().getModel()));

mxUtils.writeFile(xml2, "C:\\test graph/file.xml");


} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

public static class AddNewValve extends AbstractAction {

/**
*
*/
private static final long serialVersionUID = 4187443683391653845L;
private GUIBoard _valveNDB;
private MouseMotionListener _boardMotionListener;
private MouseListener _boardListener;

public AddNewValve(GUIBoard ndb){
_valveNDB=ndb;

}

@Override
public void actionPerformed(ActionEvent aevt) {

Valve v = new Valve();
v.setElementNumber(_valveNDB.getValves().size()+1);
v.setElementName("V"+v.getElementNumber());
v.setElementStyleName("VALVE"+v.getElementNumber());
v.setElementStyle(_valveNDB.getGraph());

_valveNDB.addValves(v);

mxCell vCell;
_valveNDB.getGraph().getModel().beginUpdate();
try
{
vCell=(mxCell) _valveNDB.getGraph().insertVertex(_valveNDB.getGraph().getDefaultParent(), null, v, MouseInfo.getPointerInfo().getLocation().x,MouseInfo.getPointerInfo().getLocation().y, 80, 30,v.getElementStyleString());

}
finally
{
_valveNDB.getGraph().getModel().endUpdate();
}

vCell.setAttribute("Name", v.getElementName());
vCell.setConnectable(false);


_valveNDB.getGraphComponent().getGraphControl().addMouseMotionListener(_boardMotionListener=new MouseMotionAdapter() {


@Override
public void mouseDragged(MouseEvent mevt) {
// TODO Auto-generated method stub

}

@Override
public void mouseMoved(MouseEvent mevt) {
vCell.setGeometry(new mxGeometry(mevt.getX(),mevt.getY(),80,30));
_valveNDB.getGraph().refresh();

}

});

_valveNDB.getGraphComponent().getGraphControl().addMouseListener(_boardListener=new MouseListener() {

@Override
public void mouseReleased(MouseEvent mevt) {
// TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent mevt) {
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent mevt) {
// TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent mevt) {
// TODO Auto-generated method stub

}

@Override
public void mouseClicked(MouseEvent mevt) {
_valveNDB.getGraphComponent().getGraphControl().removeMouseListener(_boardListener);
_valveNDB.getGraphComponent().getGraphControl().removeMouseMotionListener(_boardMotionListener);

}
});

}
}


}

这是工具栏代码:

package graphicalUserInterface;

import java.awt.Color;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JToolBar;
import javax.swing.UIManager;

import graphicalUserInterface.GUIBoardActions.*;

public class GUIBoardToolBars extends JToolBar {

/**
*
*/
private static final long serialVersionUID = 7241903583392004921L;


GUIBoard _drawingBoard;
//JToolBar _fileToolBar;

public GUIBoardToolBars(GUIBoard ndb) {
_drawingBoard=ndb;
//_fileToolBar=new JToolBar();
}

public JToolBar addFileToolBar(){
JToolBar fileToolBar=new JToolBar();
fileToolBar.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3), getBorder()));
fileToolBar.setFloatable(false);
fileToolBar.setAlignmentX(0);
UIManager.put("ToolTip.background", Color.TRANSLUCENT);


fileToolBar.add(addNewComponent("New", new NewNetwork(_drawingBoard), new ImageIcon("Icon/new.gif"))).setToolTipText("New");

fileToolBar.add(addNewComponent("Open", new OpenNetwork(_drawingBoard), new ImageIcon("Icon/open.gif"))).setToolTipText("Open");

fileToolBar.add(addNewComponent("Save", new SaveNetwork(_drawingBoard), new ImageIcon("Icon/save.gif"))).setToolTipText("Save");
//JButton newFileToolBar=new JButton();
//newFileToolBar.setActionCommand("newFileToolBar");
//newFileToolBar.setIcon(new ImageIcon("Image/new.gif"));
//newFileToolBar.setToolTipText("New");
//fileToolBar.add(newFileToolBar);
return fileToolBar;
}

public JToolBar addPowerElements(){
JToolBar powerElements=new JToolBar(VERTICAL);
powerElements.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(3,3,3,3), getBorder()));
powerElements.setFloatable(false);


powerElements.add(addNewComponent("Valve", new AddNewValve(_drawingBoard), new ImageIcon("Icon/valve1.png"))).setToolTipText("Valve");

return powerElements;
}

@SuppressWarnings("serial")
public Action addNewComponent(String name, Action componentAction, ImageIcon icon){

AbstractAction newComponent=new AbstractAction(name,(icon!=null) ? icon : null){

public void actionPerformed(ActionEvent e)
{
componentAction.actionPerformed(new ActionEvent(_drawingBoard.getGraphComponent(),e.getID(),e.getActionCommand()));
}
};
icon.setDescription(name);
return newComponent;

}

public GUIBoardToolBars(int arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}

public GUIBoardToolBars(String arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}

public GUIBoardToolBars(String arg0, int arg1) {
super(arg0, arg1);
// TODO Auto-generated constructor stub
}

}

最后是自定义对象类:

package netElements;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import com.mxgraph.model.mxCell;
import com.mxgraph.util.mxConstants;
import com.mxgraph.view.mxGraph;
import com.mxgraph.view.mxStylesheet;

public class Valve implements BasicPropertiesOfElement, Serializable {

/**
*
*/
private static final long serialVersionUID = 6425822395370778513L;
private mxCell _valveCell;
private transient mxStylesheet _valveStyleSheet;
protected int _valveNumber;
private double _valveX, _valveY, _valveRotationalAngle=0;
private String _valveName, _valveStyleName, _valveStyleString="";

public Valve() {

}

public void setElementStyleName(String styleName) {

_valveStyleName=styleName;
}

public String getElementStyleName() {

return _valveStyleName;
}

public void setElementStyle(mxGraph g) {

Map<String, Object> transProperties;

_valveStyleSheet = g.getStylesheet();
transProperties = new HashMap<String, Object>();
transProperties.put(mxConstants.STYLE_SHAPE, "valve");
transProperties.put(mxConstants.STYLE_STROKECOLOR, "black");
transProperties.put(mxConstants.STYLE_ROTATION, _valveRotationalAngle);
transProperties.put(mxConstants.STYLE_VERTICAL_LABEL_POSITION, mxConstants.ALIGN_TOP);
_valveStyleSheet.putCellStyle(_valveStyleName, transProperties);

this.setElementStyleString(transProperties);

}

public mxStylesheet getElementStyle() {

return this._valveStyleSheet;
}

public void setElementStyleString(Map<String, Object> transProperties) {

transProperties.entrySet().stream().forEach(entry ->
_valveStyleString+=entry.getKey()+"="+entry.getValue().toString()+";");
}

public String getElementStyleString() {

return _valveStyleString;

}



@Override
public void setElementRotationalAngle(double angle) {
// TODO Auto-generated method stub
_valveRotationalAngle=angle;

}



@Override
public double getElementRotationalAngle() {
// TODO Auto-generated method stub
return _valveRotationalAngle;
}


@Override
public void setElementNumber(int elementNumber) {
// TODO Auto-generated method stub
_valveNumber=elementNumber;

}



@Override
public int getElementNumber() {
// TODO Auto-generated method stub
return _valveNumber;
}



@Override
public void setElementName(String elementName) {
// TODO Auto-generated method stub
_valveName=elementName;

}



@Override
public String getElementName() {
// TODO Auto-generated method stub
return _valveName;
}

}

我检查了xml文件,发现自定义对象(这里名为Valve)的属性没有显示。基于示例here ,自定义对象应该在 codedc 中注册,其余的将由 JGraph 处理。我不知道该怎么做或应该重写哪个方法来保存自定义对象的属性以及单元格。任何帮助将不胜感激。

最佳答案

我找到了解决方案。对象类(此处为 Valve)中的对象属性应与 getter/setter 方法名称相同。例如,名称为“_valveName”的变量(属性)应与其 getter/setter 方法匹配。这里该变量的 getter/setter 方法是:
公共(public)无效setElementName(字符串元素名称)公共(public)字符串 getElementName()这与变量名不同。因此,通过更改变量名称或 getter/setter 方法名称即可解决问题。这是修改后的自定义对象类:

package netElements;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import com.mxgraph.model.mxCell;
import com.mxgraph.util.mxConstants;
import com.mxgraph.view.mxGraph;
import com.mxgraph.view.mxStylesheet;

public class Valve implements Serializable {

/**
*
*/
private static final long serialVersionUID = 6425822395370778513L;
protected int valveNumber;
protected String valveName;
private transient mxStylesheet valveStyleSheet;
private mxCell _valveCell;
private double _valveX, _valveY, valveRotationalAngle=0;
protected String valveStyleName, valveStyleString="";

public Valve()
{
}

public void setValveNumber(int elementNumber) {
// TODO Auto-generated method stub
valveNumber=elementNumber;

}

public int getValveNumber() {
// TODO Auto-generated method stub
return valveNumber;
}

public void setValveStyleName(String styleName) {

valveStyleName=styleName;
}

public String getValveStyleName() {

return valveStyleName;
}

public void setValveStyle(mxGraph g) {

Map<String, Object> transProperties;

valveStyleSheet = g.getStylesheet();
transProperties = new HashMap<String, Object>();
transProperties.put(mxConstants.STYLE_SHAPE, "valve");
transProperties.put(mxConstants.STYLE_STROKECOLOR, "black");
transProperties.put(mxConstants.STYLE_ROTATION, valveRotationalAngle);
transProperties.put(mxConstants.STYLE_VERTICAL_LABEL_POSITION, mxConstants.ALIGN_TOP);
valveStyleSheet.putCellStyle(valveStyleName, transProperties);

this.setValveStyleString(transProperties);

}

public mxStylesheet getValveStyle() {

return this.valveStyleSheet;
}

public void setValveStyleString(Map<String, Object> transProperties) {

transProperties.entrySet().stream().forEach(entry ->
valveStyleString+=entry.getKey()+"="+entry.getValue().toString()+";");
}

public String getValveStyleString() {

return valveStyleString;

}

public void setValveRotationalAngle(double angle) {
// TODO Auto-generated method stub
valveRotationalAngle=angle;

}

public double getValveRotationalAngle() {
// TODO Auto-generated method stub
return valveRotationalAngle;
}


public void setValveName(String elementName) {
// TODO Auto-generated method stub
valveName=elementName;

}

public String getValveName() {
// TODO Auto-generated method stub
return this.valveName;
}

}

整个示例可以在 here 找到.

关于java - 使用 JGraphx 在 xml 文件中保存和加载图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50127916/

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