EdgeView.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package view;
  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.Point;
  8. import java.util.ArrayList;
  9. import javax.swing.JPanel;
  10. import bk.LayoutType;
  11. import graph.LayeredGraphEdge;
  12. /**
  13. * A drawable representation of an edge.
  14. * @author kolja
  15. *
  16. */
  17. public class EdgeView extends JPanel {
  18. private static final long serialVersionUID = 1L;
  19. private LayeredGraphEdge model;
  20. private LayoutType layout;
  21. public EdgeView( LayeredGraphEdge model, LayoutType lt ) {
  22. this.model = model;
  23. layout = lt;
  24. }
  25. @Override
  26. public Point getLocation()
  27. {
  28. ArrayList<Point> bps = model.getLinePoints( layout );
  29. double minX = bps.get( 0 ).getX();
  30. double minY = bps.get( 0 ).getY();
  31. for( Point p : bps )
  32. {
  33. minX = Math.min( minX, p.getX() );
  34. minY = Math.min( minY, p.getY() );
  35. }
  36. minX -= 5;
  37. minY -= 5;
  38. return new Point( (int)minX, (int)minY );
  39. }
  40. @Override
  41. public int getWidth()
  42. {
  43. ArrayList<Point> bps = model.getLinePoints( layout );
  44. double max = bps.get( 0 ).getX();
  45. for( Point p : bps )
  46. max = Math.max( max, p.getX() );
  47. return (int)max + 10;
  48. }
  49. @Override
  50. public int getHeight()
  51. {
  52. ArrayList<Point> bps = model.getLinePoints( layout );
  53. double max = bps.get( 0 ).getY();
  54. for( Point p : bps )
  55. max = Math.max( max, p.getY() );
  56. return (int)max + 10;
  57. }
  58. @Override
  59. public Dimension getPreferredSize()
  60. {
  61. return new Dimension( getWidth(), getHeight() );
  62. }
  63. @Override
  64. public void paint( Graphics g )
  65. {
  66. paintComponent( g );/*
  67. for( Component c : getComponents() )
  68. c.paint( g.create(
  69. c.getX() + 10,
  70. c.getY() + 10,
  71. Math.min( getWidth() - 10, c.getWidth() + c.getX() + 10 ),
  72. Math.min( getHeight() - 10, c.getHeight() + c.getY() + 10 ) ) );
  73. */}
  74. @Override
  75. public void paintComponent( Graphics g )
  76. {
  77. ((Graphics2D)g).setStroke(new BasicStroke(1));
  78. //System.out.println( "Clipping: x:" + g.getClip().getBounds().getX() + " y:" + g.getClip().getBounds().getY() + " w:" + g.getClip().getBounds().getWidth() + " h:" + g.getClip().getBounds().getHeight() );
  79. g.setColor( Color.LIGHT_GRAY );
  80. if( model.isConflicted( layout ) )
  81. g.setColor( Color.RED );
  82. ArrayList<Point> bps = model.getLinePoints( layout );
  83. for( int i = 1; i < bps.size(); i++ )
  84. {
  85. // System.out.println( "Draw a Line from (" + (int)bps.get( i - 1 ).getX() + "," + (int)bps.get( i - 1 ).getY() + ") to (" + (int)bps.get( i ).getX() + "," + (int)bps.get( i ).getY() + ")" );
  86. g.drawLine( (int)bps.get( i - 1 ).getX() - getLocation().x, (int)bps.get( i - 1 ).getY() - getLocation().y, (int)bps.get( i ).getX() - getLocation().x, (int)bps.get( i ).getY() - getLocation().y );
  87. }
  88. ((Graphics2D)g).fill( RenderHelper.createArrowShape( new Point( bps.get( bps.size() - 2 ).x - getLocation().x, bps.get( bps.size() - 2 ).y - getLocation().y ), new Point( bps.get( bps.size() - 1 ).x - getLocation().x, bps.get( bps.size() - 1 ).y - getLocation().y ) ) );
  89. }
  90. }