Force-Directed Edge Bundling
 All Classes Functions
MainWindow.java
1 import java.awt.Dimension;
2 import java.awt.EventQueue;
3 
4 import javax.swing.JFrame;
5 import javax.swing.JOptionPane;
6 
7 import java.io.BufferedReader;
8 import java.io.BufferedWriter;
9 import java.io.File;
10 import java.io.FileReader;
11 import java.io.FileWriter;
12 
13 import net.miginfocom.swing.MigLayout;
14 
20 public class MainWindow {
21 
22  private CanvasPanel canvasPanel;
23  private DrawPanel drawPanel;
24  private JFrame frame;
25  private MouseMode mouseMode;
26 
30  public static void main(String[] args) {
31  EventQueue.invokeLater(new Runnable() {
32  public void run() {
33  try {
34  MainWindow window = new MainWindow();
35  window.frame.setVisible(true);
36  } catch (Exception e) {
37  e.printStackTrace();
38  }
39  }
40  });
41  }
42 
46  public MainWindow() {
47  initialize();
48  }
49 
54  public JFrame getFrame(){
55  return frame;
56  }
57 
63  return mouseMode;
64  }
65 
70  public void setMouseMode(MouseMode mouseMode){
71  this.mouseMode = mouseMode;
72  }
73 
79  public void bundleEdges(int stiffness, boolean animate){
80  canvasPanel.bundleEdges(stiffness, animate);
81  }
82 
86  public void clearCanvas(){
87  canvasPanel.clearAll();
88  }
89 
93  public void connectAll(){
94  canvasPanel.connectAll();
95  }
96 
101  public void loadGraph(File file){
102  try {
103  BufferedReader br = new BufferedReader(new FileReader(file));
104  String line = br.readLine();
105  canvasPanel.clearAll();
106  while (line != null) {
107  String[] parts = line.split(";");
108  if(parts[0].equals("node")){
109  canvasPanel.addNode(Integer.parseInt(parts[1]), Integer.parseInt(parts[2]));
110  }
111  else if(parts[0].equals("edge")){
112  canvasPanel.addEdge(Integer.parseInt(parts[1]), Integer.parseInt(parts[2]));
113  }
114  line = br.readLine();
115  }
116  br.close();
117  canvasPanel.paint(canvasPanel.getGraphics());
118  } catch (Exception e) {
119  JOptionPane.showMessageDialog(frame, "Could not load file.");
120  }
121  }
122 
127  public void saveGraph(File file){
128  try {
129  if (!file.getName().endsWith(".gdf"))
130  file = new File(file.getAbsolutePath() + ".gdf");
131  BufferedWriter bw = new BufferedWriter(new FileWriter(file));
132  for(Node node : canvasPanel.getNodes())
133  bw.write("node;" + node.getXInt() + ";" + node.getYInt() + "\n");
134  for(Edge edge : canvasPanel.getEdges())
135  bw.write("edge;" + canvasPanel.getNodes().indexOf(edge.getStartNode()) + ";"
136  + canvasPanel.getNodes().indexOf(edge.getEndNode()) + "\n");
137  bw.flush();
138  bw.close();
139  } catch (Exception e) {
140  JOptionPane.showMessageDialog(frame, "Could not save file.");
141  }
142  }
143 
147  public void unbundleEdges(){
148  canvasPanel.unbundleEdges();
149  }
150 
155  public void updateProgress(int progress){
156  drawPanel.updateProgress(progress);
157  }
158 
162  private void initialize() {
163  mouseMode = MouseMode.CURSOR;
164 
165  frame = new JFrame("Force-Directed Edge Bundling");
166  frame.setBounds(100, 100, 700, 400);
167  frame.setMinimumSize(new Dimension(700,400));
168  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
169  frame.getContentPane().setLayout(new MigLayout("", "[grow]", "[grow]"));
170 
171  drawPanel = new DrawPanel(this);
172  frame.getContentPane().add(drawPanel, "cell 0 0, aligny top");
173  canvasPanel = new CanvasPanel(this);
174  canvasPanel.setMinimumSize(new Dimension(700-drawPanel.getWidth(), 400));
175  frame.getContentPane().add(canvasPanel, "cell 1 0,alignx left,grow");
176  }
177 
178 }