-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphVisualiser.java
More file actions
214 lines (199 loc) · 7.23 KB
/
GraphVisualiser.java
File metadata and controls
214 lines (199 loc) · 7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import java.awt.*;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* Creates a basic visual representation of the graph and the connectivity of nodes
* and their importance (via colour).
* @author Samuel Heath 21725083, Bryan Trac 21704976
*/
public class GraphVisualiser {
private Graph g; // Graph we are visualising
private JFrame frame;
private CanvasPane canvas;
private Graphics2D graphic;
private Image canvasImage;
/**
*
* @param g The graph we want to visually represent.
* @param title The name of the window that opens.
* @param width Width of the canvas.
* @param height Height of the canvas.
* @param bgColour Background colour of the canvas.
*/
public GraphVisualiser(Graph g, String title, int width, int height, Color bgColour) {
this.g = g;
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas = new CanvasPane();
frame.setContentPane(canvas);
frame.setTitle(title);
canvas.setPreferredSize(new Dimension(width,height));
frame.pack();
Dimension size = canvas.getSize();
canvasImage = canvas.createImage(size.width,size.height);
graphic = (Graphics2D) canvasImage.getGraphics();
graphic.setColor(bgColour);
graphic.fillRect(0,0,size.width,size.height);
graphic.setColor(Color.black);
frame.setVisible(true);
frame.setVisible(true);
// frame.show();
frame.setVisible(true);
draw();
repaint();
}
/**
*
*/
class CanvasPane extends JPanel {
public void paint(Graphics g) {
g.drawImage(canvasImage,0,0,null);
}
}
/**
* Draws the graph, and uses random numbers to put the nodes on the canvas.
*/
public void draw() {
//Draw key
int h = canvas.getHeight();
int w = canvas.getWidth();
drawLine(2,2,2,152,Color.BLACK);
drawLine(2,152,75,152,Color.BLACK);
drawLine(2,2,75,2,Color.BLACK);
drawLine(75,2,75,152,Color.BLACK);
drawRectangle(5,5,15,15, Color.GREEN);
drawString("0-9 %", 25,15, Color.BLACK);
drawRectangle(5,20,15,30, Color.ORANGE);
drawString("10-19 %",25,30, Color.BLACK);
drawRectangle(5,35,15,45, Color.PINK);
drawString("20-29 %", 25,45, Color.BLACK);
drawRectangle(5,50,15,60, Color.MAGENTA);
drawString("30-39 %", 25,60, Color.BLACK);
drawRectangle(5,65,15,75, Color.RED);
drawString("40-49 %", 25,75, Color.BLACK);
drawRectangle(5,80,15,90, Color.LIGHT_GRAY);
drawString("50-59 %", 25,90, Color.BLACK);
drawRectangle(5,95,15,105, Color.GRAY);
drawString("60-69 %", 25,105, Color.BLACK);
drawRectangle(5,110,15,120,Color.DARK_GRAY);
drawString("70-79 %", 25, 120, Color.BLACK);
drawRectangle(5,125,15,135,Color.BLUE);
drawString("80-89 %",25,135, Color.BLACK);
drawRectangle(5,140,15,150,Color.BLACK);
drawString("90-99 %",25,150, Color.BLACK);
drawString("Incident Nodes (%)", 2,165, Color.BLACK);
//End
Random rx = new Random(); //random for the x component.
Random ry = new Random(); //random for the y component.
int x,y;
int[][] nodePos = new int[g.getNumberOfVertices()][3];
for (int i = 0; i < g.getEdgeMatrix()[0].length; i ++) {
x = rx.nextInt(canvas.getWidth() - 80) + 80;
y = ry.nextInt(canvas.getHeight() - 60) + 50;
int count = 0;
for (int j = 0; j < g.getNumberOfVertices(); j++) {
if (g.getEdgeMatrix()[i][j] == 1) {
count++;
}
}
int percentIndex = (int) Math.floor(((double)count/g.getNumberOfVertices())*10);
nodePos[i] = new int[] {x,y, percentIndex};
}
for (int i = 0; i < g.getNumberOfVertices(); i++) {
for (int j = 0; j < g.getNumberOfVertices(); j++) {
if(g.getEdgeMatrix()[i][j] == 1) {
switch (nodePos[i][2]){
case 0: this.setForegroundColour(Color.GREEN);
break;
case 1: this.setForegroundColour(Color.ORANGE);
break;
case 2: this.setForegroundColour(Color.PINK);
break;
case 3: this.setForegroundColour(Color.MAGENTA);
break;
case 4: this.setForegroundColour(Color.RED);
break;
case 5: this.setForegroundColour(Color.LIGHT_GRAY);
break;
case 6: this.setForegroundColour(Color.GRAY);
break;
case 7: this.setForegroundColour(Color.DARK_GRAY);
break;
case 8: this.setForegroundColour(Color.BLUE);
break;
case 9: this.setForegroundColour(Color.BLACK);
break;
default: this.setForegroundColour(Color.YELLOW);
break;
}
drawLine(nodePos[i][0], nodePos[i][1], nodePos[j][0], nodePos[j][1], this.getForegroundColour());
}
graphic.fillOval(nodePos[i][0]-7, nodePos[i][1]-7, 14, 14);
}
}
}
/**
* @param x1 X position start.
* @param y1 y position start.
* @param x2 X position finish.
* @param y2 Y position finish.
* @param c The colour to draw the line in.
*/
public void drawLine(int x1, int y1, int x2, int y2, Color c) {
setForegroundColour(c);
graphic.drawLine(x1,y1,x2,y2);
canvas.repaint();
}
/**
* Draws a rectangle on the SimpleCanvas between two points.
* Assumes that x1 <= x2 && y1 <= y2
*/
public void drawRectangle(int x1, int y1, int x2, int y2, Color c) {
setForegroundColour(c);
for (int i = x1; i < x2; i++)
graphic.drawLine(i, y1, i, y2 - 1);
canvas.repaint();
}
/**
* Changes the colour for subsequent drawing on this SimpleCanvas
*/
public void setForegroundColour(Color newColour) {
graphic.setColor(newColour);
}
/**
* Returns the color used for drawing.
*/
public Color getForegroundColour() {
return graphic.getColor();
}
/**
* he font to be used.
*/
public void setFont(Font newFont) {
graphic.setFont(newFont);
}
/**
* Returns the font being used.
*/
public Font getFont() {
return graphic.getFont();
}
/**
* @param text
* @param x
* @param y
* @param c
*/
public void drawString(String text, int x, int y, Color c) {
setForegroundColour(c);
graphic.drawString(text, x, y);
canvas.repaint();
}
/**
* Repaints the canvas.
*/
public void repaint() {
canvas.repaint();
}
}