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.orientation
int orientation
Definition: BarChart.java:57
font
Font2DTest To run then you should either specify the complete path to the commands or update your PATH environment variable as described in the installation instructions for the load Font2DTest html If you wish to modify any of the source you may want to extract the contents of the Font2DTest jar file by executing this the browser plugin viewer needs following permissions given in order to run but some of its features will be limited To enable all please add these permissions with policytool Introduction Font2DTest is an encompassing application for testing various fonts found on the user s system A number of controls are available to change many attributes of the current font including and rendering hints The user can select from multiple display such as one Unicode range at a all glyphs of a particular font
Definition: README.txt:44
BarChart.title
String title
Definition: BarChart.java:58
columns
Font2DTest To run then you should either specify the complete path to the commands or update your PATH environment variable as described in the installation instructions for the load Font2DTest html If you wish to modify any of the source you may want to extract the contents of the Font2DTest jar file by executing this the browser plugin viewer needs following permissions given in order to run but some of its features will be limited To enable all please add these permissions with policytool Introduction Font2DTest is an encompassing application for testing various fonts found on the user s system A number of controls are available to change many attributes of the current font including and rendering hints The user can select from multiple display such as one Unicode range at a all glyphs of a particular user edited or text loaded from a file In the user can control which method will be used to render the text to the if none of the characters can be displayed A tooltip is shown with this information This indication is available only if Unicode Range is selected in Text to use combobox This feature is enabled by default For disabling this use command line flag disablecandisplaycheck or dcdc java jar Font2DTest jar dcdc For the Font Size field to have an it is necessary to press ENTER when finished inputting data in those fields When Unicode Range or All Glyphs is selected for Text to the status bar will show the range of the characters that is currently being displayed If mouse cursor is pointed to one of the character the message will be changed to indicate what character the cursor is pointing to By clicking on a character one can also Zoom a character Options can be set to show grids around each or force the number of characters displayed across the screen to be These features are not available in User Text or File Text mode The default number of columns in a Unicode Range or All Glyphs drawing is fit as many as possible If this is too hard to then you can force number of columns to be this will not resize the window to fit all columns
Definition: README.txt:80
BarChart.init
void init()
Definition: BarChart.java:72
BarChart.paintVertical
void paintVertical(Graphics g)
Definition: BarChart.java:255
BarChart
Definition: BarChart.java:51
BarChart.columns
int columns
Definition: BarChart.java:61
class
About including and JRadioButtonMenuItem Metalworks is optimized to work with the Java look and such as that are specific to the Java look and feel Running then you should either specify the complete path to the java command or update your PATH environment variable as described in the installation instructions for the and many controls are non functional They are intended only to show how to construct the UI for such interfaces Things that do work in the Metalworks demo but also the sizes of many controls Also included with this demo is the PropertiesMetalTheme class
Definition: README.txt:54
BarChart.font
Font font
Definition: BarChart.java:59
BarChart.metrics
FontMetrics metrics
Definition: BarChart.java:60
applet
SwingApplet illustrates how it s possible to run a Swing based applet
Definition: README.txt:2
BarChart.getParameterInfo
String[][] getParameterInfo()
Definition: BarChart.java:305
BarChart.parseLabel
void parseLabel(int i)
Definition: BarChart.java:134
BarChart.parseColor
void parseColor(int i)
Definition: BarChart.java:155
BarChart.parseStyle
void parseStyle(int i)
Definition: BarChart.java:144
BarChart.getSettings
void getSettings()
Definition: BarChart.java:89
BarChart.getAppletInfo
String getAppletInfo()
Definition: BarChart.java:298
BarChart.paint
void paint(Graphics g)
Definition: BarChart.java:190
BarChart.paintHorizontal
void paintHorizontal(Graphics g)
Definition: BarChart.java:210
BarChart.parseValue
void parseValue(int i)
Definition: BarChart.java:122