-
Notifications
You must be signed in to change notification settings - Fork 0
/
BarChart.java
299 lines (265 loc) · 7.81 KB
/
BarChart.java
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* BarChart Code
*/
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.*;
import javax.swing.JFrame;
import java.awt.Color;
public class BarChart implements ImageObserver
{
public static final int XMAX = 400;
public static final int XSTART = 60;
public static final int [] YMAX = { 220, 320, 420, 520 };
// hard code an array of 4 colors
private Color [] colors = { Color.BLUE, new Color( 150, 150, 0 ),
Color.DARK_GRAY, Color.MAGENTA };
private int [][] data;
private int barSize;
private int xStart;
private int activity = 5;
private int count = 0;
private int studentResult;
private int exactFrequencyCount;
private int exactMinimum;
private int key;
private boolean checkNewValues;
public BarChart( )
{ }
public BarChart( int [][] dArray )
{
data = new int [dArray.length][dArray[0].length];
for ( int i = 0; i < dArray.length; i++ )
{
for ( int j = 0; j < dArray[i].length; j++ )
{
data[i][j] = dArray[i][j];
}
}
barSize = ( XMAX - 20 ) / data[0].length;
studentResult = -1;
exactFrequencyCount = -1;
exactMinimum = -1;
key = -1;
checkNewValues = true;
}
public void setArray( int [][] dArray )
{
for ( int i = 0; i < dArray.length; i++ )
{
for ( int j = 0; j < dArray[i].length; j++ )
{
data[i][j] = dArray[i][j];
}
}
}
public void setStudentResult( int newStudentResult )
{
studentResult = newStudentResult;
}
public void setKey( int newKey )
{
key = newKey;
}
public int getExactFrequencyCount( )
{
return exactFrequencyCount;
}
public int getExactMinimum( )
{
return exactMinimum;
}
public boolean getCheckNewValues( )
{
return checkNewValues;
}
public void setActivity( int a )
{
activity = a;
}
public int getActivity( )
{
return activity;
}
public void updateBarChart( int key, int index1, int index2, Graphics g )
{
draw( g );
switch( activity )
{
case( 0 ): // create new array values
// drawNewArray( index1, index2, g );
break;
case( 1 ): // print out the array
drawArray( index1, index2, g );
break;
case( 2 ): // set all array values of a row to a certain value
drawNewValue( index1, index2, g );
break;
case( 3 ): // find the minimum in a row
drawMinimum( index1, index2, g );
break;
case( 4 ): // find the frequency of a value
drawFrequency( index1, index2, key, g );
break;
}
}
public void draw( Graphics g )
{
// Draws the original array in 4 colors
for ( int i = 0; i < data.length; i++ )
{
xStart = XSTART;
g.setColor( colors[i] );
g.drawString( "Row " + i, xStart - 50, YMAX[i] );
for ( int j = 0; j < data[i].length; j++ )
{
g.fillRect( xStart, YMAX[i]-data[i][j], barSize-5, data[i][j] );
g.drawString( "" + data[i][j], xStart, YMAX[i] + 15 );
xStart += barSize;
}
}
}
public void drawArray( int index1, int index2, Graphics g )
{
// called to "print out the array"
// index1 must be less than 4
g.setColor( Color.red );
for ( int i = 0; i < index1; i++ )
{
xStart = XSTART;
for ( int j = 0; j < data[i].length; j++ )
{
g.fillRect( xStart, YMAX[i]-data[i][j], barSize-5, data[i][j] );
g.drawString( "" + data[i][j], xStart, YMAX[i] + 15 );
xStart += barSize;
}
}
xStart = XSTART;
for ( int j = 0; j <= index2; j++ )
{
g.fillRect( xStart, YMAX[index1]-data[index1][j], barSize-5, data[index1][j] );
g.drawString( "" + data[index1][j], xStart, YMAX[index1] + 15 );
xStart += barSize;
}
}
public void drawNewValue( int index1, int index2, Graphics g )
{
g.setColor( Color.blue );
String s = "";
checkCurrentNewValues( index1, index2 );
if ( checkNewValues )
s = "correctly";
else
s = "incorrectly";
g.drawString( "Setting new array elements of row " + index1 + " to " + key, 25, 110 );
g.drawString( "Up to index " + index2 + " , the new values are set " + s, 25, 130 );
// set all values of a row to a certain value
g.setColor( Color.red );
xStart = XSTART;
// g.drawString( "" + data[index1][index2], xStart, YMAX[index1] + 15 );
for ( int j = 0; j <= index2; j++ )
{
g.drawString( "" + data[index1][index2], xStart, YMAX[index1] + 15 );
g.fillRect( xStart, YMAX[index1]-data[index1][j], barSize-5, data[index1][j] );
xStart += barSize;
}
}
public void drawMinimum( int index1, int index2, Graphics g )
{
int b = findSubMinimum( index1, index2 );
if ( index2 != -1 )
{
g.setColor( Color.BLUE );
g.drawString( "Your current minimum in column " + index2 + ": " + studentResult, 25, 110 );
exactMinimum = b;
g.drawString( "Correct current minimum in column " + index2 + ": " + exactMinimum, 25, 130 );
g.setColor( Color.RED );
xStart = XSTART + index2 * barSize;
g.drawRect( xStart, YMAX[index1] - data[index1][index2], barSize-5, data[index1][index2] );
g.drawString( "" + data[index1][index2], xStart, YMAX[index1] + 15 );
for ( int i = 0; i <= index1; i++ )
{
if ( data[i][index2] == b )
{
g.setColor( Color.RED );
g.fillRect( xStart, YMAX[i] - data[i][index2], barSize-5, data[i][index2] );
break;
}
}
}
}
public int findSubMinimum( int index1, int index2 )
{
int minimum = data[0][index2];
for ( int i = 1; i <= index1; i++ )
{
if ( minimum > data[i][index2] )
minimum = data[i][index2];
}
return minimum;
}
public void drawFrequency( int index1, int index2, int value, Graphics g )
{
// called to count frequency of a value
// index1 must be less than 4
// index2 must be less than 20
g.setColor( Color.BLUE );
g.drawString( "Your current frequency count: " + studentResult, 25, 110 );
exactFrequencyCount = findExactFrequencyCount( index1, index2, value );
g.drawString( "Correct current frequency count: " + exactFrequencyCount, 25, 130 );
g.setColor( Color.RED );
for ( int i = 0; i < index1; i++ )
{
xStart = XSTART;
for ( int j = 0; j < data[i].length; j++ )
{
if ( data[i][j] == value )
g.fillRect( xStart, YMAX[i]-data[i][j], barSize-5, data[i][j] );
xStart += barSize;
}
}
// last row (incomplete)
xStart = XSTART + barSize * index2;
g.drawRect( xStart, YMAX[index1]-value, barSize-5, value );
g.drawString( "" + data[index1][index2], xStart, YMAX[index1] + 15 );
xStart = XSTART;
for ( int j = 0; j <= index2; j++ )
{
if ( data[index1][j] == value )
g.fillRect( xStart, YMAX[index1]-data[index1][j], barSize-5, data[index1][j] );
xStart += barSize;
}
}
public int findExactFrequencyCount( int rowIndex, int colIndex, int searchValue )
{
int currentCount = 0;
for ( int i = 0; i < rowIndex; i++ )
{
for( int j = 0; j < data[i].length; j++ )
{
if ( data[i][j] == searchValue )
currentCount++;
}
}
// check current row now
for (int j = 0; j <= colIndex; j++ )
{
if ( data[rowIndex][j] == searchValue )
currentCount++;
}
return currentCount;
}
public void checkCurrentNewValues( int index1, int index2 )
{
checkNewValues = true;
for ( int i = 0; i <= index2; i++ )
{
if ( key != data[index1][i] )
checkNewValues = false;
}
}
public boolean imageUpdate( Image i, int infoflags, int x, int y, int width, int height )
{
return true;
}
}