LSD, Скажи пожалуста чего в этом коде не так... апплет запускаеться, но ничего не выводит...
| Код | import java.awt.Component; import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Iterator; import java.util.Set; import java.util.TreeSet;
import javax.swing.BorderFactory; import javax.swing.BoxLayout; import javax.swing.JApplet; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingConstants; import javax.swing.JComboBox;
import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath; import edu.uci.ics.jung.graph.Edge; import edu.uci.ics.jung.graph.Graph; import edu.uci.ics.jung.graph.Vertex; import edu.uci.ics.jung.graph.decorators.StringLabeller; import edu.uci.ics.jung.graph.decorators.StringLabeller.UniqueLabelException; import edu.uci.ics.jung.graph.impl.DirectedSparseGraph; import edu.uci.ics.jung.random.generators.EppsteinPowerLawGenerator; import edu.uci.ics.jung.visualization.FRLayout; import edu.uci.ics.jung.visualization.PluggableRenderer; import edu.uci.ics.jung.visualization.ShapePickSupport; import edu.uci.ics.jung.visualization.VisualizationViewer; import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse; import edu.uci.ics.jung.visualization.control.ModalGraphMouse;
public class kurs extends JApplet implements ActionListener { /** * */ private static final long serialVersionUID = 1L; protected static Graph g; protected static Vertex[] v; protected static Edge[] e; protected static StringLabeller slc; protected static VisualizationViewer vv; protected static PluggableRenderer pr; static Vertex mFrom; static Vertex mTo; protected static int dist; protected static String list; public static Graph Ggraph() { g = new DirectedSparseGraph(); v = new Vertex[26]; e = new Edge[50];
g = (Graph) new EppsteinPowerLawGenerator(26, 50, 50).generateGraph();
StringLabeller sl = StringLabeller.getLabeller(g); int c = 0; for (Iterator iter = g.getVertices().iterator(); iter.hasNext();) { Vertex v = (Vertex) iter.next(); try { sl.setLabel(v, "" + c);
} catch (UniqueLabelException e) { } c++; } return g; } private static JPanel setUpControls() { JPanel jp = new JPanel();
jp.setBackground(Color.WHITE); jp.setLayout(new BoxLayout(jp, BoxLayout.PAGE_AXIS)); jp.setBorder(BorderFactory.createLineBorder(Color.black, 3));
JPanel jp2 = new JPanel(); jp2.add(new JLabel("Введите количество итераций:", SwingConstants.LEFT)); jp2.add(getSelectionBox(true)); jp2.setBackground(Color.white); jp.add(jp2); return jp2; } private static Component getSelectionBox(final boolean from) { StringLabeller sl = StringLabeller.getLabeller(g); Set<String> s = new TreeSet<String>();
for (Iterator iter = g.getVertices().iterator(); iter.hasNext();) { s.add(sl.getLabel((Vertex) iter.next())); } final JComboBox choices = new JComboBox(s.toArray()); choices.setSelectedIndex(-1); choices.setBackground(Color.WHITE); choices.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { StringLabeller sl = StringLabeller.getLabeller(g); Vertex v = sl.getVertex((String) choices.getSelectedItem()); if (from) { mFrom = v; } else { mTo = v; } drawShortest(); }
});
return choices; }
protected static void drawShortest() { if (mFrom == null || mTo == null) { return; } DijkstraShortestPath dsp = new DijkstraShortestPath(g); try { Number n = dsp.getDistance(mFrom, mTo); dist = n.intValue(); } catch (IllegalArgumentException iae) { } try { list = dsp.getPath(mFrom, mTo).toString(); } catch (IllegalArgumentException iae) { } }
@SuppressWarnings("deprecation") public static void main(String[] args) { Ggraph();
FRLayout l = new FRLayout(g); PluggableRenderer r = new PluggableRenderer();
vv = new VisualizationViewer(l, r); vv.setPickSupport(new ShapePickSupport()); vv.setBackground(Color.WHITE); vv.setForeground(Color.darkGray); ModalGraphMouse gm = new DefaultModalGraphMouse(); vv.setGraphMouse(gm);
JFrame jf = new JFrame(); jf.getContentPane().add(vv, BorderLayout.CENTER); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.getContentPane().add(setUpControls(), BorderLayout.SOUTH);
jf.pack(); jf.show(); } public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub }
}
|
|