author | jgish |
Thu, 18 Apr 2013 16:33:11 -0400 | |
changeset 17159 | bb566a21b661 |
parent 10292 | ed7db6a12c2a |
permissions | -rw-r--r-- |
2 | 1 |
/* |
8972
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
|
5 |
* modification, are permitted provided that the following conditions |
|
6 |
* are met: |
|
7 |
* |
|
8 |
* - Redistributions of source code must retain the above copyright |
|
9 |
* notice, this list of conditions and the following disclaimer. |
|
10 |
* |
|
11 |
* - Redistributions in binary form must reproduce the above copyright |
|
12 |
* notice, this list of conditions and the following disclaimer in the |
|
13 |
* documentation and/or other materials provided with the distribution. |
|
14 |
* |
|
5506 | 15 |
* - Neither the name of Oracle nor the names of its |
2 | 16 |
* contributors may be used to endorse or promote products derived |
17 |
* from this software without specific prior written permission. |
|
18 |
* |
|
19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
|
20 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
|
21 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
22 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
23 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
24 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
25 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
26 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
27 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
28 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
29 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
30 |
*/ |
|
31 |
||
10292
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8972
diff
changeset
|
32 |
/* |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8972
diff
changeset
|
33 |
* This source code is provided to illustrate the usage of a given feature |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8972
diff
changeset
|
34 |
* or technique and has been deliberately simplified. Additional steps |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8972
diff
changeset
|
35 |
* required for a production-quality application, such as security checks, |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8972
diff
changeset
|
36 |
* input validation and proper error handling, might not be present in |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8972
diff
changeset
|
37 |
* this sample code. |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8972
diff
changeset
|
38 |
*/ |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8972
diff
changeset
|
39 |
|
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8972
diff
changeset
|
40 |
|
2 | 41 |
|
42 |
import java.awt.*; |
|
43 |
||
8972
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
44 |
|
2 | 45 |
/** |
46 |
* A simple bar chart demo |
|
47 |
* @author Sami Shaio |
|
48 |
* @modified 06/21/00 Daniel Peek : refactored, comments |
|
49 |
*/ |
|
8972
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
50 |
@SuppressWarnings("serial") |
2 | 51 |
public class BarChart extends java.applet.Applet { |
8972
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
52 |
|
2 | 53 |
private static final int VERTICAL = 0; |
54 |
private static final int HORIZONTAL = 1; |
|
55 |
private static final int SOLID = 0; |
|
56 |
private static final int STRIPED = 1; |
|
57 |
private int orientation; |
|
58 |
private String title; |
|
59 |
private Font font; |
|
60 |
private FontMetrics metrics; |
|
61 |
private int columns; |
|
62 |
private int values[]; |
|
63 |
private Color colors[]; |
|
64 |
private String labels[]; |
|
65 |
private int styles[]; |
|
66 |
private int scale = 10; |
|
67 |
private int maxLabelWidth = 0; |
|
68 |
private int barSpacing = 10; |
|
69 |
private int maxValue = 0; |
|
70 |
||
8972
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
71 |
@Override |
2 | 72 |
public void init() { |
73 |
||
74 |
getSettings(); |
|
75 |
||
76 |
values = new int[columns]; |
|
77 |
labels = new String[columns]; |
|
78 |
styles = new int[columns]; |
|
79 |
colors = new Color[columns]; |
|
80 |
||
8972
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
81 |
for (int i = 0; i < columns; i++) { |
2 | 82 |
parseValue(i); |
83 |
parseLabel(i); |
|
84 |
parseStyle(i); |
|
85 |
parseColor(i); |
|
86 |
} |
|
87 |
} |
|
88 |
||
89 |
private void getSettings() { |
|
90 |
font = new java.awt.Font("Monospaced", Font.BOLD, 12); |
|
91 |
metrics = getFontMetrics(font); |
|
92 |
||
93 |
title = getParameter("title"); |
|
94 |
if (title == null) { |
|
95 |
title = "Chart"; |
|
96 |
} |
|
97 |
||
98 |
String temp = getParameter("columns"); |
|
99 |
if (temp == null) { |
|
100 |
columns = 5; |
|
101 |
} else { |
|
102 |
columns = Integer.parseInt(temp); |
|
103 |
} |
|
104 |
||
105 |
temp = getParameter("scale"); |
|
106 |
if (temp == null) { |
|
107 |
scale = 10; |
|
108 |
} else { |
|
109 |
scale = Integer.parseInt(temp); |
|
110 |
} |
|
111 |
||
112 |
temp = getParameter("orientation"); |
|
113 |
if (temp == null) { |
|
114 |
orientation = VERTICAL; |
|
115 |
} else if (temp.equalsIgnoreCase("horizontal")) { |
|
116 |
orientation = HORIZONTAL; |
|
117 |
} else { |
|
118 |
orientation = VERTICAL; |
|
119 |
} |
|
120 |
} |
|
121 |
||
122 |
private void parseValue(int i) { |
|
8972
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
123 |
String temp = getParameter("C" + (i + 1)); |
2 | 124 |
try { |
125 |
values[i] = Integer.parseInt(temp); |
|
126 |
} catch (NumberFormatException e) { |
|
127 |
values[i] = 0; |
|
128 |
} catch (NullPointerException e) { |
|
129 |
values[i] = 0; |
|
130 |
} |
|
131 |
maxValue = Math.max(maxValue, values[i]); |
|
132 |
} |
|
133 |
||
134 |
private void parseLabel(int i) { |
|
8972
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
135 |
String temp = getParameter("C" + (i + 1) + "_label"); |
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
136 |
if (temp == null) { |
2 | 137 |
labels[i] = ""; |
138 |
} else { |
|
139 |
labels[i] = temp; |
|
140 |
} |
|
8972
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
141 |
maxLabelWidth = Math.max(metrics.stringWidth(labels[i]), maxLabelWidth); |
2 | 142 |
} |
143 |
||
144 |
private void parseStyle(int i) { |
|
8972
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
145 |
String temp = getParameter("C" + (i + 1) + "_style"); |
2 | 146 |
if (temp == null || temp.equalsIgnoreCase("solid")) { |
147 |
styles[i] = SOLID; |
|
148 |
} else if (temp.equalsIgnoreCase("striped")) { |
|
149 |
styles[i] = STRIPED; |
|
150 |
} else { |
|
151 |
styles[i] = SOLID; |
|
152 |
} |
|
153 |
} |
|
154 |
||
155 |
private void parseColor(int i) { |
|
8972
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
156 |
String temp = getParameter("C" + (i + 1) + "_color"); |
2 | 157 |
if (temp != null) { |
158 |
temp = temp.toLowerCase(); |
|
159 |
if (temp.equals("red")) { |
|
160 |
colors[i] = Color.red; |
|
161 |
} else if (temp.equals("green")) { |
|
162 |
colors[i] = Color.green; |
|
163 |
} else if (temp.equals("blue")) { |
|
164 |
colors[i] = Color.blue; |
|
165 |
} else if (temp.equals("pink")) { |
|
166 |
colors[i] = Color.pink; |
|
167 |
} else if (temp.equals("orange")) { |
|
168 |
colors[i] = Color.orange; |
|
169 |
} else if (temp.equals("magenta")) { |
|
170 |
colors[i] = Color.magenta; |
|
171 |
} else if (temp.equals("cyan")) { |
|
172 |
colors[i] = Color.cyan; |
|
173 |
} else if (temp.equals("white")) { |
|
174 |
colors[i] = Color.white; |
|
175 |
} else if (temp.equals("yellow")) { |
|
176 |
colors[i] = Color.yellow; |
|
177 |
} else if (temp.equals("gray")) { |
|
178 |
colors[i] = Color.gray; |
|
179 |
} else if (temp.equals("darkgray")) { |
|
180 |
colors[i] = Color.darkGray; |
|
181 |
} else { |
|
182 |
colors[i] = Color.gray; |
|
183 |
} |
|
184 |
} else { |
|
185 |
colors[i] = Color.gray; |
|
186 |
} |
|
187 |
} |
|
188 |
||
8972
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
189 |
@Override |
2 | 190 |
public void paint(Graphics g) { |
191 |
// draw the title centered at the bottom of the bar graph |
|
192 |
g.setColor(Color.black); |
|
193 |
g.setFont(font); |
|
194 |
||
195 |
g.drawRect(0, 0, getSize().width - 1, getSize().height - 1); |
|
196 |
||
197 |
int titleWidth = metrics.stringWidth(title); |
|
198 |
int cx = Math.max((getSize().width - titleWidth) / 2, 0); |
|
199 |
int cy = getSize().height - metrics.getDescent(); |
|
200 |
g.drawString(title, cx, cy); |
|
201 |
||
202 |
// draw the bars and their titles |
|
8972
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
203 |
if (orientation == HORIZONTAL) { |
2 | 204 |
paintHorizontal(g); |
205 |
} else { // VERTICAL |
|
206 |
paintVertical(g); |
|
207 |
} |
|
208 |
} |
|
209 |
||
210 |
private void paintHorizontal(Graphics g) { |
|
211 |
// x and y coordinates to draw/write to |
|
212 |
int cx, cy; |
|
213 |
int barHeight = metrics.getHeight(); |
|
214 |
||
215 |
for (int i = 0; i < columns; i++) { |
|
216 |
||
217 |
// set the X coordinate for this bar and label and center it |
|
218 |
int widthOfItems = maxLabelWidth + 3 + (maxValue * scale) + 5 |
|
8972
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
219 |
+ metrics.stringWidth(Integer.toString(maxValue)); |
2 | 220 |
cx = Math.max((getSize().width - widthOfItems) / 2, 0); |
221 |
||
222 |
// set the Y coordinate for this bar and label |
|
223 |
cy = getSize().height - metrics.getDescent() - metrics.getHeight() |
|
8972
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
224 |
- barSpacing |
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
225 |
- ((columns - i - 1) * (barSpacing + barHeight)); |
2 | 226 |
|
227 |
// draw the label |
|
228 |
g.setColor(Color.black); |
|
229 |
g.drawString(labels[i], cx, cy); |
|
230 |
cx += maxLabelWidth + 3; |
|
231 |
||
232 |
||
233 |
// draw the shadow |
|
234 |
g.fillRect(cx + 4, cy - barHeight + 4, |
|
8972
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
235 |
(values[i] * scale), barHeight); |
2 | 236 |
|
237 |
// draw the bar |
|
238 |
g.setColor(colors[i]); |
|
239 |
if (styles[i] == STRIPED) { |
|
240 |
for (int k = 0; k <= values[i] * scale; k += 2) { |
|
241 |
g.drawLine(cx + k, cy - barHeight, cx + k, cy); |
|
242 |
} |
|
243 |
} else { // SOLID |
|
244 |
g.fillRect(cx, cy - barHeight, |
|
8972
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
245 |
(values[i] * scale) + 1, barHeight + 1); |
2 | 246 |
} |
247 |
cx += (values[i] * scale) + 4; |
|
248 |
||
249 |
// draw the value at the end of the bar |
|
250 |
g.setColor(g.getColor().darker()); |
|
251 |
g.drawString(Integer.toString(values[i]), cx, cy); |
|
252 |
} |
|
253 |
} |
|
254 |
||
255 |
private void paintVertical(Graphics g) { |
|
256 |
int barWidth = maxLabelWidth; |
|
257 |
||
258 |
for (int i = 0; i < columns; i++) { |
|
259 |
||
260 |
// X coordinate for this label and bar (centered) |
|
261 |
int widthOfItems = (barWidth + barSpacing) * columns - barSpacing; |
|
262 |
int cx = Math.max((getSize().width - widthOfItems) / 2, 0); |
|
263 |
cx += (maxLabelWidth + barSpacing) * i; |
|
264 |
||
265 |
// Y coordinate for this label and bar |
|
266 |
int cy = getSize().height - metrics.getHeight() |
|
8972
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
267 |
- metrics.getDescent() - 4; |
2 | 268 |
|
269 |
// draw the label |
|
270 |
g.setColor(Color.black); |
|
271 |
g.drawString(labels[i], cx, cy); |
|
272 |
cy -= metrics.getHeight() - 3; |
|
273 |
||
274 |
// draw the shadow |
|
275 |
g.fillRect(cx + 4, cy - (values[i] * scale) - 4, |
|
8972
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
276 |
barWidth, (values[i] * scale)); |
2 | 277 |
|
278 |
// draw the bar |
|
279 |
g.setColor(colors[i]); |
|
280 |
if (styles[i] == STRIPED) { |
|
8972
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
281 |
for (int k = 0; k <= values[i] * scale; k += 2) { |
2 | 282 |
g.drawLine(cx, cy - k, |
8972
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
283 |
cx + barWidth, cy - k); |
2 | 284 |
} |
285 |
} else { |
|
286 |
g.fillRect(cx, cy - (values[i] * scale), |
|
8972
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
287 |
barWidth + 1, (values[i] * scale) + 1); |
2 | 288 |
} |
289 |
cy -= (values[i] * scale) + 5; |
|
290 |
||
291 |
// draw the value on top of the bar |
|
292 |
g.setColor(g.getColor().darker()); |
|
293 |
g.drawString(Integer.toString(values[i]), cx, cy); |
|
294 |
} |
|
295 |
} |
|
296 |
||
8972
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
297 |
@Override |
2 | 298 |
public String getAppletInfo() { |
299 |
return "Title: Bar Chart \n" |
|
8972
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
300 |
+ "Author: Sami Shaio \n" |
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
301 |
+ "A simple bar chart demo."; |
2 | 302 |
} |
303 |
||
8972
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
304 |
@Override |
2 | 305 |
public String[][] getParameterInfo() { |
306 |
String[][] info = { |
|
8972
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
307 |
{ "title", "string", "The title of bar graph. Default is 'Chart'" }, |
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
308 |
{ "scale", "int", "The scale of the bar graph. Default is 10." }, |
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
309 |
{ "columns", "int", "The number of columns/rows. Default is 5." }, |
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
310 |
{ "orientation", "{VERTICAL, HORIZONTAL}", |
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
311 |
"The orienation of the bar graph. Default is VERTICAL." }, |
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
312 |
{ "c#", "int", "Subsitute a number for #. " |
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
313 |
+ "The value/size of bar #. Default is 0." }, |
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
314 |
{ "c#_label", "string", "The label for bar #. " |
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
315 |
+ "Default is an empty label." }, |
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
316 |
{ "c#_style", "{SOLID, STRIPED}", "The style of bar #. " |
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
317 |
+ "Default is SOLID." }, |
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
318 |
{ "c#_color", "{RED, GREEN, BLUE, PINK, ORANGE, MAGENTA, CYAN, " |
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
319 |
+ "WHITE, YELLOW, GRAY, DARKGRAY}", |
1bde3211f2c6
7027674: /applets/BarChart demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
320 |
"The color of bar #. Default is GRAY." } |
2 | 321 |
}; |
322 |
return info; |
|
323 |
} |
|
324 |
} |