Geometry-Based Edge Clustering
 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 
78  public boolean isMeshSelected(){
79  return drawPanel.isMeshSelected();
80  }
81 
87  public void autoMesh(int dx, int dy){
88  canvasPanel.createMesh(dx, dy);
89  }
90 
94  public void bundleEdges(){
95  canvasPanel.bundleEdges();
96  }
97 
101  public void clearCanvas(){
102  canvasPanel.clearAll();
103  }
104 
108  public void clearMesh(){
109  canvasPanel.clearMesh();
110  }
111 
115  public void connectAll(){
116  canvasPanel.connectAll();
117  }
118 
123  public void loadGraph(File file){
124  try {
125  BufferedReader br = new BufferedReader(new FileReader(file));
126  String line = br.readLine();
127  canvasPanel.clearAll();
128  while (line != null) {
129  String[] parts = line.split(";");
130  if(parts[0].equals("node")){
131  canvasPanel.addNode(Integer.parseInt(parts[1]), Integer.parseInt(parts[2]));
132  }
133  else if(parts[0].equals("edge")){
134  canvasPanel.addEdge(Integer.parseInt(parts[1]), Integer.parseInt(parts[2]));
135  }
136  line = br.readLine();
137  }
138  br.close();
139  canvasPanel.paintComponent(canvasPanel.getGraphics());
140  } catch (Exception e) {
141  JOptionPane.showMessageDialog(frame, "Could not load file.");
142  }
143  }
144 
148  public void paintCanvas(){
149  canvasPanel.paintComponent(canvasPanel.getGraphics());
150  }
151 
156  public void saveGraph(File file){
157  try {
158  if (!file.getName().endsWith(".gdf"))
159  file = new File(file.getAbsolutePath() + ".gdf");
160  BufferedWriter bw = new BufferedWriter(new FileWriter(file));
161  for(Node node : canvasPanel.getNodes())
162  bw.write("node;" + node.getXInt() + ";" + node.getYInt() + "\n");
163  for(Edge edge : canvasPanel.getEdges())
164  bw.write("edge;" + canvasPanel.getNodes().indexOf(edge.getStartNode()) + ";"
165  + canvasPanel.getNodes().indexOf(edge.getEndNode()) + "\n");
166  bw.flush();
167  bw.close();
168  } catch (Exception e) {
169  JOptionPane.showMessageDialog(frame, "Could not save file.");
170  }
171  }
172 
176  public void unbundleEdges(){
177  canvasPanel.unbundleEdges();
178  }
179 
183  private void initialize() {
184  mouseMode = MouseMode.CURSOR;
185 
186  frame = new JFrame("Geometry-Based Edge Clustering");
187  frame.setBounds(100, 100, 700, 400);
188  frame.setMinimumSize(new Dimension(700,400));
189  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
190  frame.getContentPane().setLayout(new MigLayout("", "[grow]", "[grow]"));
191 
192  drawPanel = new DrawPanel(this);
193  frame.getContentPane().add(drawPanel, "cell 0 0, aligny top");
194  canvasPanel = new CanvasPanel(this);
195  canvasPanel.setMinimumSize(new Dimension(700-drawPanel.getWidth(), 400));
196  frame.getContentPane().add(canvasPanel, "cell 1 0,alignx left,grow");
197  }
198 
199 }