Class: GraphCanvas
- Inherits:
-
Object
- Object
- GraphCanvas
- Defined in:
- lib/graph_canvas.rb
Instance Attribute Summary collapse
-
#graph ⇒ Object
Returns the value of attribute graph.
-
#iterative ⇒ Object
Returns the value of attribute iterative.
-
#layout ⇒ Object
Returns the value of attribute layout.
Instance Method Summary collapse
- #height ⇒ Object
-
#initialize ⇒ GraphCanvas
constructor
A new instance of GraphCanvas.
- #paint(g) ⇒ Object
- #paint_arrowhead(g, edge) ⇒ Object
- #paint_edge(g, edge) ⇒ Object
- #paint_edges(g) ⇒ Object
- #paint_graph(g) ⇒ Object
- #paint_info(g) ⇒ Object
- #paint_node(g, node, location) ⇒ Object
- #paint_nodes(g) ⇒ Object
- #paint_ref_edges(g, node) ⇒ Object
- #update_layout(layout) ⇒ Object
- #width ⇒ Object
Constructor Details
#initialize ⇒ GraphCanvas
Returns a new instance of GraphCanvas.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/graph_canvas.rb', line 26 def initialize super @iterative = true @frame = 0 @queue = ConcurrentLinkedQueue.new @d = 25 @poly = Polygon.new @poly.addPoint(0, 0) @poly.addPoint(6, 14) @poly.addPoint(0, 11) @poly.addPoint(-6, 14) @poly.addPoint(0, 0) setBackground(Color::black) end |
Instance Attribute Details
#graph ⇒ Object
Returns the value of attribute graph.
23 24 25 |
# File 'lib/graph_canvas.rb', line 23 def graph @graph end |
#iterative ⇒ Object
Returns the value of attribute iterative.
24 25 26 |
# File 'lib/graph_canvas.rb', line 24 def iterative @iterative end |
#layout ⇒ Object
Returns the value of attribute layout.
23 24 25 |
# File 'lib/graph_canvas.rb', line 23 def layout @layout end |
Instance Method Details
#height ⇒ Object
183 184 185 |
# File 'lib/graph_canvas.rb', line 183 def height return self.getHeight end |
#paint(g) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/graph_canvas.rb', line 54 def paint(g) if @queue.size > 0 @painting = true @queued_layout = @queue.poll paint_graph(g) java.lang.Thread.sleep(20) repaint elsif @layout @queued_layout = @layout paint_graph(g) @painting = false end end |
#paint_arrowhead(g, edge) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/graph_canvas.rb', line 129 def paint_arrowhead(g, edge) a = @queued_layout[edge.source] b = @queued_layout[edge.target] g.setStroke(BasicStroke.new(2)) line = Line2D::Double.new(a.x, a.y, b.x, b.y) g.draw(line) teta = Math::atan2((a.x - b.x), a.y - b.y) dist = Math.sqrt((a.x - b.x)**2 + (a.y - b.y)**2) frac = @d/2.0 / dist px = b.x - frac * (b.x - a.x) py = b.y - frac * (b.y - a.y) at = g.getTransform g.translate(px, py) g.rotate(-teta) if (@queued_layout.graph.has_edge_data_key?(:path) and @queued_layout.graph.edge_data(:path)[edge]) color = g.getColor stroke = g.getStroke g.setColor(Color::ORANGE) g.setStroke(BasicStroke.new(6)) g.draw(@poly) g.setStroke(stroke) g.setColor(color) end g.draw(@poly) g.fill(@poly) g.setTransform(at) end |
#paint_edge(g, edge) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/graph_canvas.rb', line 108 def paint_edge(g, edge) s = @queued_layout[edge.source] t = @queued_layout[edge.target] line = Line2D::Double.new(s.x, s.y, t.x, t.y) if (@queued_layout.graph.has_edge_data_key?(:path) and @queued_layout.graph.edge_data(:path)[edge]) g.setColor(Color::ORANGE) g.setStroke(BasicStroke.new(6)) g.draw(line) end if (@queued_layout.graph.edge_data(:is_clicked?) and @queued_layout.graph.edge_data(:is_clicked?)[edge]) g.setColor(Color::blue) else g.setColor(Color::white) end if (edge.directed?) paint_arrowhead(g, edge) end g.setStroke(BasicStroke.new(2)) g.draw(line) end |
#paint_edges(g) ⇒ Object
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/graph_canvas.rb', line 161 def paint_edges(g) g.setColor(Color::white) @queued_layout.graph.each_edge do |edge| if edge.source == edge.target paint_ref_edge(g, edge) else paint_edge(g, edge) end end end |
#paint_graph(g) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/graph_canvas.rb', line 68 def paint_graph(g) if (@queued_layout.graph) paint_info(g) paint_edges(g) paint_nodes(g) end end |
#paint_info(g) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/graph_canvas.rb', line 76 def paint_info(g) rh = RenderingHints.new(RenderingHints.new(RenderingHints::KEY_ANTIALIASING, RenderingHints::VALUE_ANTIALIAS_ON)) g.setRenderingHints(rh); g.setStroke(BasicStroke.new(2)) finished = "" finished = "finished" if @queued_layout.finished? g.setColor(Color::white) g.drawString("Iteration: #{@queued_layout.iterations}", 10, 20) g.drawString("Layout: #{@queued_layout.class.name} #{finished}", 10, self.height - 20) end |
#paint_node(g, node, location) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/graph_canvas.rb', line 88 def paint_node(g, node, location) shape = Ellipse2D::Double.new(location.x-@d/2.0, location.y-@d/2.0, @d, @d) g.setColor(Color::black) g.fill(shape) if @queued_layout.graph.node_data(:is_clicked?) and @queued_layout.graph.node_data(:is_clicked?)[node] g.setColor(Color::blue) elsif @queued_layout.graph.node_data(:path) and @queued_layout.graph.node_data(:path)[node] g.setColor(Color::red) else g.setColor(Color::white) end g.draw(shape) end |
#paint_nodes(g) ⇒ Object
102 103 104 105 106 |
# File 'lib/graph_canvas.rb', line 102 def paint_nodes(g) @queued_layout.each_node_location do |node, location| paint_node(g, node, location) end end |
#paint_ref_edges(g, node) ⇒ Object
172 173 174 175 176 177 |
# File 'lib/graph_canvas.rb', line 172 def paint_ref_edges(g, node) loc = @layout[node] shape = Ellipse2D::Double.new(loc.x, loc.y, 40, 40) g.setColor(Color::white) g.draw(shape) end |
#update_layout(layout) ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/graph_canvas.rb', line 43 def update_layout(layout) if @iterative if @layout != layout @layout = layout @queue.clear end @queue.add(layout.deep_copy) repaint unless @painting end end |
#width ⇒ Object
179 180 181 |
# File 'lib/graph_canvas.rb', line 179 def width return self.getWidth end |