@@ -7,23 +7,24 @@ public class BarGraphWindow extends JFrame {
7
7
8
8
public static final int Y_OFFSET = 50 ;
9
9
public static final int Y_BOTTOM_OFFSET = 60 ;
10
- public static final double Y_SPACE = 0.05 ;
10
+ public static final int X_SPACE_ABS = 2 ;
11
11
12
- public static final int REDRAW_DELAY = 250 ;
12
+ public static final int REDRAW_DELAY = 50 ;
13
13
14
14
public static final Color DEFAULT_COLOR = new Color (93 , 173 , 226 );
15
15
public static final Color RANGE_COLOR = new Color (243 , 156 , 18 );
16
16
public static final Color PIVOT_COLOR = new Color (211 , 84 , 0 );
17
17
public static final Color BORDER_COLOR = new Color (21 , 67 , 96 );
18
18
19
- public static boolean DRAW_BORDER = true ;
19
+ public static boolean DRAW_BORDER = false ;
20
+ public static boolean DRAW_NUMBER = false ;
20
21
21
22
public int relWidth , relSpace , relHeight ;
22
23
public int [] barXLeft , barXRight , barValues ;
23
24
public Color [] barColor ;
24
25
25
26
public BarGraphWindow () {
26
- this .setSize (800 , 600 );
27
+ this .setSize (1800 , 1024 );
27
28
this .setVisible (true );
28
29
this .setDefaultCloseOperation (WindowConstants .EXIT_ON_CLOSE );
29
30
@@ -37,9 +38,8 @@ public int findMax(int[] values){
37
38
}
38
39
39
40
public void initBarGraph (int [] values ){
40
- this .relWidth = (int )(this .getWidth () / (values .length * (1 + Y_SPACE * 2 )));
41
- this .relSpace = (int )(relWidth * Y_SPACE );
42
- this .relHeight = (int )((this .getHeight () - Y_OFFSET - Y_BOTTOM_OFFSET ) / findMax (values ) * (1 - Y_SPACE ));
41
+ this .relWidth = (int )((this .getWidth () - values .length ) / values .length );
42
+ this .relHeight = (int )((this .getHeight () - Y_OFFSET - Y_BOTTOM_OFFSET ) / findMax (values ));
43
43
44
44
this .barValues = values .clone ();
45
45
this .barXLeft = new int [values .length ];
@@ -52,20 +52,67 @@ public void initBarGraph(int[] values){
52
52
barXRight [i ] = barXLeft [i ] + relWidth ;
53
53
barColor [i ] = BarGraphWindow .DEFAULT_COLOR ;
54
54
55
- prevX += relWidth + relSpace ;
55
+ prevX += relWidth + X_SPACE_ABS ;
56
56
}
57
57
}
58
58
59
59
public void updateBarGraph (int [] values ){
60
- this .relHeight = (int )((this .getHeight () - Y_OFFSET - Y_BOTTOM_OFFSET ) / findMax (values ) * (1 - Y_SPACE ));
61
- this .barValues = values .clone ();
62
- int prevX = relSpace ;
63
- for (int i = 0 ; i < values .length ; i ++){
64
- barXLeft [i ] = prevX ;
65
- barXRight [i ] = barXLeft [i ] + relWidth ;
60
+ int newHeight = (this .getHeight () - Y_OFFSET - Y_BOTTOM_OFFSET ) / findMax (values );
61
+
62
+ if (newHeight == this .relHeight ){
63
+ for (int i = 0 ; i < values .length ; i ++){
64
+ if (this .barValues [i ] != values [i ]){
65
+ this .barValues [i ] = values [i ];
66
+ redrawBar (i );
67
+ }
68
+ }
69
+ } else {
70
+ this .relHeight = newHeight ;
66
71
67
- prevX += relWidth + relSpace ;
72
+ this .barValues = values .clone ();
73
+ int prevX = relSpace ;
74
+ for (int i = 0 ; i < values .length ; i ++){
75
+ barXLeft [i ] = prevX ;
76
+ barXRight [i ] = barXLeft [i ] + relWidth ;
77
+
78
+ prevX += relWidth + relSpace ;
79
+ }
68
80
}
81
+
82
+ try {
83
+ Thread .sleep (REDRAW_DELAY );
84
+ } catch (Exception ex ){}
85
+ }
86
+
87
+ public synchronized void clearBar (int index ){
88
+ Graphics g = this .getGraphics ();
89
+ g .clearRect (barXLeft [index ], 0 , relWidth , this .getHeight ());
90
+ }
91
+
92
+ public synchronized void drawBar (int index ){
93
+ Graphics2D g = (Graphics2D ) this .getGraphics ();
94
+ int leftCornerX = barXLeft [index ];
95
+ int leftCornerY = this .getHeight () - Y_BOTTOM_OFFSET - relHeight * barValues [index ];
96
+ int barHeight = relHeight * barValues [index ];
97
+
98
+ g .setColor (barColor [index ]);
99
+ g .fillRect (leftCornerX , leftCornerY + Y_OFFSET , relWidth , barHeight );
100
+
101
+ if (DRAW_BORDER ){
102
+ g .setColor (BarGraphWindow .BORDER_COLOR );
103
+ g .drawRect (leftCornerX , leftCornerY + Y_OFFSET , relWidth , barHeight );
104
+ }
105
+
106
+ if (DRAW_NUMBER ) {
107
+ g .setColor (Color .BLACK );
108
+ g .setFont (new Font ("Consolas" , Font .BOLD , 14 ));
109
+ g .drawString (Integer .toString (barValues [index ]), leftCornerX + 10 , leftCornerY );
110
+ }
111
+ }
112
+
113
+ public void redrawBar (int index ){
114
+ clearBar (index );
115
+ drawBar (index );
69
116
}
70
117
71
118
public synchronized void drawBarGraph (){
@@ -87,9 +134,11 @@ public synchronized void drawBarGraph(){
87
134
g .drawRect (leftCornerX , leftCornerY + Y_OFFSET , relWidth , barHeight );
88
135
}
89
136
90
- g .setColor (Color .BLACK );
91
- g .setFont (new Font ("Consolas" , Font .BOLD , 14 ));
92
- g .drawString (Integer .toString (barValues [i ]), leftCornerX + 10 , leftCornerY );
137
+ if (DRAW_NUMBER ) {
138
+ g .setColor (Color .BLACK );
139
+ g .setFont (new Font ("Consolas" , Font .BOLD , 14 ));
140
+ g .drawString (Integer .toString (barValues [i ]), leftCornerX + 10 , leftCornerY );
141
+ }
93
142
}
94
143
95
144
try {
0 commit comments