Crossfire JXClient, Trunk
BarChart.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
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  *
15  * - Neither the name of Oracle nor the names of its
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 
32 /*
33  * This source code is provided to illustrate the usage of a given feature
34  * or technique and has been deliberately simplified. Additional steps
35  * required for a production-quality application, such as security checks,
36  * input validation and proper error handling, might not be present in
37  * this sample code.
38  */
39 
40 
41 
42 import java.awt.*;
43 
44 
50 @SuppressWarnings("serial")
51 public class BarChart extends java.applet.Applet {
52 
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 
71  @Override
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 
81  for (int i = 0; i < columns; i++) {
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) {
123  String temp = getParameter("C" + (i + 1));
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) {
135  String temp = getParameter("C" + (i + 1) + "_label");
136  if (temp == null) {
137  labels[i] = "";
138  } else {
139  labels[i] = temp;
140  }
141  maxLabelWidth = Math.max(metrics.stringWidth(labels[i]), maxLabelWidth);
142  }
143 
144  private void parseStyle(int i) {
145  String temp = getParameter("C" + (i + 1) + "_style");
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) {
156  String temp = getParameter("C" + (i + 1) + "_color");
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 
189  @Override
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
203  if (orientation == HORIZONTAL) {
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
219  + metrics.stringWidth(Integer.toString(maxValue));
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()
224  - barSpacing
225  - ((columns - i - 1) * (barSpacing + barHeight));
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,
235  (values[i] * scale), barHeight);
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,
245  (values[i] * scale) + 1, barHeight + 1);
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()
267  - metrics.getDescent() - 4;
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,
276  barWidth, (values[i] * scale));
277 
278  // draw the bar
279  g.setColor(colors[i]);
280  if (styles[i] == STRIPED) {
281  for (int k = 0; k <= values[i] * scale; k += 2) {
282  g.drawLine(cx, cy - k,
283  cx + barWidth, cy - k);
284  }
285  } else {
286  g.fillRect(cx, cy - (values[i] * scale),
287  barWidth + 1, (values[i] * scale) + 1);
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 
297  @Override
298  public String getAppletInfo() {
299  return "Title: Bar Chart \n"
300  + "Author: Sami Shaio \n"
301  + "A simple bar chart demo.";
302  }
303 
304  @Override
305  public String[][] getParameterInfo() {
306  String[][] info = {
307  { "title", "string", "The title of bar graph. Default is 'Chart'" },
308  { "scale", "int", "The scale of the bar graph. Default is 10." },
309  { "columns", "int", "The number of columns/rows. Default is 5." },
310  { "orientation", "{VERTICAL, HORIZONTAL}",
311  "The orienation of the bar graph. Default is VERTICAL." },
312  { "c#", "int", "Subsitute a number for #. "
313  + "The value/size of bar #. Default is 0." },
314  { "c#_label", "string", "The label for bar #. "
315  + "Default is an empty label." },
316  { "c#_style", "{SOLID, STRIPED}", "The style of bar #. "
317  + "Default is SOLID." },
318  { "c#_color", "{RED, GREEN, BLUE, PINK, ORANGE, MAGENTA, CYAN, "
319  + "WHITE, YELLOW, GRAY, DARKGRAY}",
320  "The color of bar #. Default is GRAY." }
321  };
322  return info;
323  }
324 }
BarChart.parseValue
void parseValue(int i)
Definition: BarChart.java:122
BarChart.metrics
FontMetrics metrics
Definition: BarChart.java:60
BarChart.getSettings
void getSettings()
Definition: BarChart.java:89
BarChart
Definition: BarChart.java:51
BarChart.parseStyle
void parseStyle(int i)
Definition: BarChart.java:144
BarChart.parseColor
void parseColor(int i)
Definition: BarChart.java:155
BarChart.columns
int columns
Definition: BarChart.java:61
BarChart.font
Font font
Definition: BarChart.java:59
BarChart.getAppletInfo
String getAppletInfo()
Definition: BarChart.java:298
BarChart.paint
void paint(Graphics g)
Definition: BarChart.java:190
BarChart.orientation
int orientation
Definition: BarChart.java:57
BarChart.title
String title
Definition: BarChart.java:58
BarChart.parseLabel
void parseLabel(int i)
Definition: BarChart.java:134
BarChart.init
void init()
Definition: BarChart.java:72
BarChart.getParameterInfo
String[][] getParameterInfo()
Definition: BarChart.java:305
BarChart.paintVertical
void paintVertical(Graphics g)
Definition: BarChart.java:255
BarChart.paintHorizontal
void paintHorizontal(Graphics g)
Definition: BarChart.java:210