-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGraphScreen.java
More file actions
185 lines (153 loc) · 7.81 KB
/
GraphScreen.java
File metadata and controls
185 lines (153 loc) · 7.81 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
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class GraphScreen {
// Reference to Main Program
Main main;
// Reference to Graphics2D used by Main
Graphics2D graphics;
// Debugging: Speed up option
// boolean speedUp= false;
boolean speedUp= true;
// Constants defining the color of the graph
private static final Color BARCOLOR= Color.GREEN;
private static final Color SWAPCOLOR= Color.RED;
private static final Color COMPARECOLOR= Color.YELLOW;
private static final Color INSERTCOLOR= Color.ORANGE;
private static final Color READCOLOR= Color.BLUE;
private static final Color EMPTYCOLOR= Color.BLACK;
// Constants defining the color and size of the UI
private static final Color HEADERCOLOR= Color.BLACK;
private static final Color TEXTCOLOR= Color.WHITE;
// Constant defining creation and dimensions of the restart button
private static Rectangle2D restart;
// The array to be visualized
private int[] array;
// The size of the array to be visualized
private int SIZE;
// Largest element of the array for scaling rectangles
private int LARGEST;
// The array of positions to be highlighted
private int[] pointers= new int[0];
// Statistics to be displayed
private long[] data;
// Render mode
private Constants.Mode mode;
// Whether of not to wait for click to continue next step
private boolean wait;
// graphScreen constructor to create a new frame to render
GraphScreen(Main main, int size, boolean wait) {
this.main= main;
this.SIZE= size;
this.LARGEST= size;
this.wait= wait;
}
// Sets the size of the array
public void setSize(int size) {
SIZE= size;
}
// Sets the largest element of the array
public void setLargest(int largest) {
LARGEST= largest;
}
// Function called to render each frame of the graph
public void render() {
this.graphics= (Graphics2D) main.panel.getGraphics();
// Fill in the rectangles (bars) for the graph
for (int i= 0; i < SIZE; i++) {
// Fill the bar
graphics.setColor(BARCOLOR);
graphics.fillRect(i*Constants.SCREEN_SIZES.WIDTH/SIZE, Constants.SCREEN_SIZES.HEIGHT - (Constants.SCREEN_SIZES.BARHEIGHT*array[i]/LARGEST), (Constants.SCREEN_SIZES.WIDTH/SIZE)+1, Constants.SCREEN_SIZES.HEADER + (Constants.SCREEN_SIZES.BARHEIGHT*array[i]/LARGEST));
// Fill space above the bar
graphics.setColor(EMPTYCOLOR);
graphics.fillRect(i*Constants.SCREEN_SIZES.WIDTH/SIZE, Constants.SCREEN_SIZES.HEADER, (Constants.SCREEN_SIZES.WIDTH/SIZE)+1, Constants.SCREEN_SIZES.BARHEIGHT - (Constants.SCREEN_SIZES.BARHEIGHT*array[i]/LARGEST));
}
if (mode != Constants.Mode.DEFAULT && mode != Constants.Mode.FINISH) {
if (mode == Constants.Mode.COMPARE) { // Comparing
// System.out.println("Comparing");
graphics.setColor(COMPARECOLOR);
} else if (mode == Constants.Mode.SWAP) { // Swapping
// System.out.println("Swapping");
graphics.setColor(SWAPCOLOR);
} else if (mode == Constants.Mode.INSERT) { // Inserting
// System.out.println("Inserting");
graphics.setColor(INSERTCOLOR);
} else if (mode == Constants.Mode.READ) { // Reading
// System.out.println("Reading");
graphics.setColor(READCOLOR);
}
// If sound enabled, split execution to play a sound while updating the highlighted positions
if (main.soundOn) {
for (int i= 0; i < pointers.length; i++) {
graphics.fillRect(pointers[i]*Constants.SCREEN_SIZES.WIDTH/SIZE, Constants.SCREEN_SIZES.HEIGHT - (Constants.SCREEN_SIZES.BARHEIGHT*array[pointers[i]]/SIZE), (Constants.SCREEN_SIZES.WIDTH/SIZE)+1, Constants.SCREEN_SIZES.HEADER + (Constants.SCREEN_SIZES.BARHEIGHT*array[pointers[i]]/SIZE));
Sound.makeSound(pointers[i], SIZE);
}
} else {
for (int i= 0; i < pointers.length; i++)
graphics.fillRect(pointers[i]*Constants.SCREEN_SIZES.WIDTH/SIZE, Constants.SCREEN_SIZES.HEIGHT - (Constants.SCREEN_SIZES.BARHEIGHT*array[pointers[i]]/SIZE), (Constants.SCREEN_SIZES.WIDTH/SIZE)+1, Constants.SCREEN_SIZES.HEADER + (Constants.SCREEN_SIZES.BARHEIGHT*array[pointers[i]]/SIZE));
}
}
// Fill Header Information
displayInformation();
//
if (wait && mode != Constants.Mode.DEFAULT && mode != Constants.Mode.FINISH) {
try {
System.in.read();
System.in.read();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void displayInformation() {
// Fill header
graphics.setColor(HEADERCOLOR);
graphics.fillRect(0, 0, Constants.SCREEN_SIZES.WIDTH, Constants.SCREEN_SIZES.HEADER);
// Display title
graphics.setColor(TEXTCOLOR);
graphics.setFont(main.titleFont);
graphics.drawString((String) main.dropDown.getSelectedItem() + " (Run " + data[Constants.DATA_INDICES.NUM_SIMULATIONS] + ")", 0, 25);
// Display information
graphics.setFont(main.myFont);
// Splits execution based on finish status
if (mode != Constants.Mode.FINISH) {
// Unfinished, so display current information
graphics.drawString(
"Comparisons: " + String.format("%.2f", (double) data[Constants.DATA_INDICES.NUM_COMPARISONS]) + " " +
"Swaps: " + String.format("%.2f", (double) data[Constants.DATA_INDICES.NUM_SWAPS]) + " " +
"Insertions: " + String.format("%.2f", (double) data[Constants.DATA_INDICES.NUM_INSERTIONS]), 0, 50);
} else {
// Finished, so display information and render the restart button
graphics.drawString(
"Avg. Comparisons: " + String.format("%.2f", (double) data[Constants.DATA_INDICES.NUM_COMPARISONS] / data[Constants.DATA_INDICES.NUM_SIMULATIONS]) + " " +
"Avg. Swaps: " + String.format("%.2f", (double) data[Constants.DATA_INDICES.NUM_SWAPS] / data[Constants.DATA_INDICES.NUM_SIMULATIONS]) + " " +
"Avg. Insertions: " + String.format("%.2f", (double) data[Constants.DATA_INDICES.NUM_INSERTIONS] / data[Constants.DATA_INDICES.NUM_SIMULATIONS]), 0, 50);
// Y position of the restart button
int restartYPosition= 57;
if (data[Constants.DATA_INDICES.NUM_TIME] > 0) {
restartYPosition= 100 - 18;
graphics.drawString("Avg. Time (nanoseconds): " + String.format("%,d", data[Constants.DATA_INDICES.NUM_TIME] / data[Constants.DATA_INDICES.NUM_SIMULATIONS]), 0, 75);
}
// Creates the restart button
graphics.drawRect(0, restartYPosition, 225, 25);
graphics.drawString("New Simulation", 0, restartYPosition + 18);
restart= new Rectangle2D.Double(0, restartYPosition, 225, 25);
}
}
// Function is called when user clicks on the screen
public void onClick(int x, int y) {
if (mode != Constants.Mode.WAITING)
render();
// If the restart button is clicked, then go back to main menu screen
if (mode == Constants.Mode.FINISH && restart.contains((double) x, (double) y)) {
mode= Constants.Mode.WAITING;
main.start();
}
}
public void updateRender(int[] array, int[] pointers, long[] data, Constants.Mode mode) {
this.array= array;
this.pointers= pointers;
this.data= data;
this.mode= mode;
render();
}
}