MainView.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package View;
  2. import java.awt.GridLayout;
  3. import java.awt.event.KeyEvent;
  4. import java.awt.event.KeyListener;
  5. import javax.swing.JFrame;
  6. import Algorithms.Animated.Action;
  7. import Algorithms.Animated.AnimationController;
  8. import Algorithms.Animated.BK.BKNodePlacement;
  9. import Algorithms.Animated.BK.ExtremalLayoutCalc.LayoutType;
  10. import IO.Writer;
  11. import Model.LayeredGraphEdge;
  12. import Model.LayeredGraphNode;
  13. /**
  14. * The main window of the application.
  15. * @author kolja
  16. *
  17. */
  18. public class MainView {
  19. /**
  20. * The 'frame' of the main window.
  21. */
  22. public static JFrame frame;
  23. AnimationController controller;
  24. /**
  25. * Initialize the window and its contents.
  26. * @param graph the graph that is displayed in this window.
  27. */
  28. public MainView( LayeredGraphNode graph )
  29. {
  30. controller = new AnimationController();
  31. controller.setTimeBetween( 100 );
  32. frame = new JFrame();
  33. frame.setSize( Math.min( (int)graph.getWidth( LayoutType.TOP_BOTTOM_LEFT ) * 2 + 200, 1700 ), Math.min( (int)graph.getHeight( LayoutType.TOP_BOTTOM_LEFT ) * 2 + 200, 900 ) );
  34. frame.setLocation( 100, 100 );
  35. frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  36. frame.setVisible( true );
  37. frame.addKeyListener( new KeyListener() {
  38. @Override
  39. public void keyTyped(KeyEvent e) {
  40. // TODO Auto-generated method stub
  41. }
  42. @Override
  43. public void keyPressed(KeyEvent e) {
  44. if( e.getKeyCode() == KeyEvent.VK_S )
  45. {
  46. Writer w = new Writer( "save.graph" );
  47. w.writeOutputGraph( graph );
  48. }
  49. if( e.getKeyCode() == KeyEvent.VK_LEFT )
  50. controller.setNextAction( Action.BACKWARD );
  51. if( e.getKeyCode() == KeyEvent.VK_RIGHT )
  52. controller.setNextAction( Action.FORWARD );
  53. if( e.getKeyCode() == KeyEvent.VK_P )
  54. controller.setContinuous( !controller.isContinuous() );
  55. frame.validate();
  56. }
  57. @Override
  58. public void keyReleased(KeyEvent e) {
  59. // TODO Auto-generated method stub
  60. }
  61. });
  62. frame.setLayout(new GridLayout( 2, 2, 10, 10 ));
  63. frame.add(createNodeView( graph, LayoutType.TOP_BOTTOM_LEFT ));
  64. frame.add(createNodeView( graph, LayoutType.TOP_BOTTOM_RIGHT ));
  65. frame.add(createNodeView( graph, LayoutType.BOTTOM_TOP_LEFT ));
  66. frame.add(createNodeView( graph, LayoutType.BOTTOM_TOP_RIGHT ));
  67. frame.validate();
  68. frame.repaint();
  69. new BKNodePlacement( controller, graph );
  70. }
  71. private NodeView createNodeView( LayeredGraphNode gNode, LayoutType lt )
  72. {
  73. NodeView graphView = new NodeView( gNode, lt );
  74. graphView.setLayout( null );
  75. graphView.setOpaque( true );
  76. for( LayeredGraphNode n : gNode.getContainedNodes() )
  77. {
  78. NodeView nv = createNodeView( n, lt );
  79. nv.setBounds( nv.getX(), nv.getY(), nv.getWidth(), nv.getHeight() );
  80. graphView.add( nv );
  81. }
  82. for( LayeredGraphEdge e : gNode.getContainedEdges() )
  83. {
  84. EdgeView ev = new EdgeView( e, lt );
  85. ev.setOpaque( true );
  86. graphView.add( ev );
  87. }
  88. return graphView;
  89. }
  90. }