Question

Not sure why my applet isn't showing. It shows in eclipse but when I compile a jar and run it from windows cmd, it doesn't display. The outputs System.out.println do work throughout the code and come out on the command line. What am I doing wrong, is it compiling executable jar or something with manifest? Do I need some kind of permissions in the jar or windows? Should I use something like proprieties file instead of arguments? First time using jars, applets, jgraph. The command I use is java -jar test.jar c:/test.xml

main.java

public static void main(String[] args) {

        File file = new File(args[0]);

        GraphMain(file).init();

        System.out.println("The file path is " + file);

    }

GraphMain.java

public class GraphMain extends JApplet {


    //Stuff

    File DRUGBANK_DIR;

    public GraphMain(File DrugBankFile)
    {
        DRUGBANK_DIR = DrugBankFile;
    }

    public  void init()
    {

        // a lot stuff



            // create a JGraphT graph
           UndirectedGraph<String, DefaultEdge> g = 
                new ListenableUndirectedGraph<String, DefaultEdge>( DefaultEdge.class );

            // create a visualization using JGraph, via an adapter
            m_jgAdapter = new JGraphModelAdapter<String, DefaultEdge>( g );

            JGraph jgraph = new JGraph( m_jgAdapter );

            adjustDisplaySettings( jgraph );
            getContentPane(  ).add( jgraph );
            resize( DEFAULT_SIZE );



            //stuff

          positionVertices(vertices0, vertices1);
        }

        private void positionVertices(List<String> vertices0, List<String> vertices1)
        {
            int dy0 = DEFAULT_SIZE.height / (vertices0.size() + 5);
            int y0 = dy0;
            for (String v0 : vertices0)
            {
                positionVertexAt(v0, 100, y0);
                y0+=dy0;
            }
            int dy1 = DEFAULT_SIZE.height / (vertices1.size() + 2);
            int y1 = dy1;
            for (String v1 : vertices1)
            {
                positionVertexAt(v1, 700, y1);
                y1+=dy1;
            }
        }



        private void adjustDisplaySettings( JGraph jg ) {
            jg.setPreferredSize( DEFAULT_SIZE );

            Color  c        = DEFAULT_BG_COLOR;
            String colorStr = null;

            try {
                colorStr = getParameter( "bgcolor" );
            }
             catch( Exception e ) {}

            if( colorStr != null ) {
                c = Color.decode( colorStr );
            }

            jg.setBackground( c );
        }


        private void positionVertexAt( Object vertex, int x, int y ) {
            DefaultGraphCell cell = m_jgAdapter.getVertexCell( vertex );
            Map<?, ?>              attr = cell.getAttributes(  );
            Rectangle2D        b    = GraphConstants.getBounds( attr );

            GraphConstants.setBounds( attr, new Rectangle2D.Double( x, y, b.getWidth(), b.getHeight() ) );

            Map<DefaultGraphCell, Map<?, ?>> cellAttr = new HashMap<DefaultGraphCell, Map<?, ?>>(  );
            cellAttr.put( cell, attr );
            m_jgAdapter.edit( cellAttr, null, null, null);
        }


}
Was it helpful?

Solution

When you run in in cmd, it won't display because it's not in an applet environment. It doesn't have anything to display to. In order for it to work, you need to create your own display, aka a JFrame. Try this code:

public static void main(String args[]){
    File file=.....;
    final GraphMain applet=new GraphMain(file);
    applet.init();
    JFrame f=new JFrame("Title goes here");
    f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    f.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e){
            applet.stop();
            applet.destroy();
            System.exit(0);
        }
    });
    f.setLayout(new BorderLayout());
    f.add(applet, BorderLayoyt.CENTER);
    f.setSize(whatever size your applet is);
    applet.start();
    f.setVisible(true);
}

I'm not guaranteeing this code to be bug free (I just wrote it in my browser right now) but you can use it as a starting point.

Also, because you are using File you will need to sign your jar for it to work. See the Oracle tutorial here: http://docs.oracle.com/javase/tutorial/deployment/jar/signindex.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top