Updating the edge weight displayed without being "cluncky"

148 views Asked by At

I am using Jgrapht to build a simulation environment based on a directed weighted graph. I now want to display the simulation using Jgraph.

While I have figured out how to display the graph and update it as the simulation runs, I have noticed that when the edge weights update, the entire window flickers. Presently I an calling frame.paintComponents() to update the window.

I believe that I can get it to only update the individual edges that are changing, but I am not familiar with java.awt.

public static void main(String [] args) throws InterruptedException
{

    Simulation newSim = new Simulation(31, .01);//extends ListenableDirectedWeightedGraph

    SimulationViewer applet = new SimulationViewer(newSim);//extends JApplet
    applet.init();

    JFrame frame = new JFrame();
    frame.getContentPane().add(applet);
    frame.setTitle("Simulation Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);


    //run simulation
    for (;newSim.getStep() < newSim.getMAX_STEP(); newSim.incrementStep()) {
        BreadthFirstIterator<SimulationBlock, Signal> iter = 
                         new BreadthFirstIterator<SimulationBlock, Signal>(newSim, newSim.head);

        while (iter.hasNext()) {
            iter.next().execute(newSim);
            //update graphics
            frame.paintComponents(applet.getGraphics());//looks clunky
            Thread.sleep(500);
        }

    }

}
1

There are 1 answers

0
Destruktor On BEST ANSWER

Looks like I needed to call the update method for applet. Replaced:

frame.paintComponents(applet.getGraphics());//looks clunky

With:

applet.update(applet.getGraphics());

This looks a little funky and maybe I need to refactor this code so that is within the SimulationViewer class.