更改顶点样式时出错

时间:2013-10-01 12:56:50

标签: java jgrapht jgraphx jgraph

我正在尝试使用以下代码更改顶点v4的样式。虽然改变了风格但我得到了错误。我试图做其他动作,比如setVisible(false),它可以工作。所以它必须是我正在使用的方法。我应该改变什么?

           public class graphgen extends JFrame {

                JFrame frame ;
                static JGraph jgraph ;

                final static mxGraph graph = new mxGraph() {


                final static mxGraphComponent graphComponent = new mxGraphComponent(graph);

                Object cell ;
                Object v1, v2, v3, v4, v5, v6, v7, v8, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15, w16, w17, w18, z1, z2, z3, z4, y1, y2, y3, y4, y5, y6 ;

                private static final int OPACITY_PALE = 20;
                private static final int OPACITY_HL = 100;


                public graphgen() {



                    gen();

                }

                public void gen(){

                Hashtable<String, Object> oldstyle = new Hashtable<String, Object>();
                oldstyle.put(mxConstants.STYLE_OPACITY, OPACITY_PALE);
                ....

                Hashtable<String, Object> newstyle = new Hashtable<String, Object>();
                newstyle.put(mxConstants.STYLE_OPACITY, OPACITY_HL);
                ...


stylesheet.putCellStyle( oldstyle, oldstyle);
    stylesheet.putCellStyle( newstyle, newstyle);

Object parent = graph.getDefaultParent();

                graph.getModel().beginUpdate();
                    try
                    {....

    Object v4 = graph.insertVertex(parent, null, "label", 380, 80, 80,
                    30, oldstyle);
                ...

                if (GC.contains("aaa")) {

                            graph.getView().getState(v4).setStyle(newstyle);

                            graphComponent.refresh();
                            graph.repaint();
                    }

                ....}
                    finally
                    {
                        graph.getModel().endUpdate();
                    }



            getContentPane().add(graphComponent);
            add(graphComponent);

            ...
        }

            public static void main(String[] args)
                {
                        graphgen frame = new graphgen();

                    frame.pack();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setResizable(false);
                    frame.setSize(1600, 1200);
                    frame.setVisible(true);


                }
            }

我收到以下错误:

    Exception in thread "main" java.lang.NullPointerException
    at graphgen.gen(graphgen.java:482) // line with raph.getView().getState(v4).changeStyle(newstyle);

1 个答案:

答案 0 :(得分:1)

使用:

mxGraph.setCellStyle(String style, Object[] cells)

代替。