提交 b2f6b311 编写于 作者: M mrkam

7027674: /applets/BarChart demo needs to be improved

Reviewed-by: alexp
上级 12e00c83
/*
* Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
......@@ -29,28 +29,26 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
*/
import java.awt.*;
/**
* A simple bar chart demo
* @author Sami Shaio
* @modified 06/21/00 Daniel Peek : refactored, comments
*/
@SuppressWarnings("serial")
public class BarChart extends java.applet.Applet {
private static final int VERTICAL = 0;
private static final int HORIZONTAL = 1;
private static final int SOLID = 0;
private static final int STRIPED = 1;
private int orientation;
private String title;
private Font font;
private FontMetrics metrics;
private int fontHeight = 15;
private int columns;
private int values[];
private Color colors[];
......@@ -61,6 +59,7 @@ public class BarChart extends java.applet.Applet {
private int barSpacing = 10;
private int maxValue = 0;
@Override
public void init() {
getSettings();
......@@ -70,7 +69,7 @@ public class BarChart extends java.applet.Applet {
styles = new int[columns];
colors = new Color[columns];
for (int i=0; i < columns; i++) {
for (int i = 0; i < columns; i++) {
parseValue(i);
parseLabel(i);
parseStyle(i);
......@@ -112,7 +111,7 @@ public class BarChart extends java.applet.Applet {
}
private void parseValue(int i) {
String temp = getParameter("C" + (i+1));
String temp = getParameter("C" + (i + 1));
try {
values[i] = Integer.parseInt(temp);
} catch (NumberFormatException e) {
......@@ -124,18 +123,17 @@ public class BarChart extends java.applet.Applet {
}
private void parseLabel(int i) {
String temp = getParameter("C" + (i+1) + "_label");
if (temp==null) {
String temp = getParameter("C" + (i + 1) + "_label");
if (temp == null) {
labels[i] = "";
} else {
labels[i] = temp;
}
maxLabelWidth = Math.max(metrics.stringWidth
((String) (labels[i])), maxLabelWidth);
maxLabelWidth = Math.max(metrics.stringWidth(labels[i]), maxLabelWidth);
}
private void parseStyle(int i) {
String temp = getParameter("C" + (i+1) + "_style");
String temp = getParameter("C" + (i + 1) + "_style");
if (temp == null || temp.equalsIgnoreCase("solid")) {
styles[i] = SOLID;
} else if (temp.equalsIgnoreCase("striped")) {
......@@ -146,7 +144,7 @@ public class BarChart extends java.applet.Applet {
}
private void parseColor(int i) {
String temp = getParameter("C" + (i+1) + "_color");
String temp = getParameter("C" + (i + 1) + "_color");
if (temp != null) {
temp = temp.toLowerCase();
if (temp.equals("red")) {
......@@ -179,6 +177,7 @@ public class BarChart extends java.applet.Applet {
}
}
@Override
public void paint(Graphics g) {
// draw the title centered at the bottom of the bar graph
g.setColor(Color.black);
......@@ -192,7 +191,7 @@ public class BarChart extends java.applet.Applet {
g.drawString(title, cx, cy);
// draw the bars and their titles
if(orientation == HORIZONTAL) {
if (orientation == HORIZONTAL) {
paintHorizontal(g);
} else { // VERTICAL
paintVertical(g);
......@@ -213,7 +212,8 @@ public class BarChart extends java.applet.Applet {
// set the Y coordinate for this bar and label
cy = getSize().height - metrics.getDescent() - metrics.getHeight()
- barSpacing - ((columns - i - 1) * (barSpacing + barHeight));
- barSpacing
- ((columns - i - 1) * (barSpacing + barHeight));
// draw the label
g.setColor(Color.black);
......@@ -269,7 +269,7 @@ public class BarChart extends java.applet.Applet {
// draw the bar
g.setColor(colors[i]);
if (styles[i] == STRIPED) {
for (int k=0; k <= values[i] * scale; k+=2) {
for (int k = 0; k <= values[i] * scale; k += 2) {
g.drawLine(cx, cy - k,
cx + barWidth, cy - k);
}
......@@ -285,28 +285,30 @@ public class BarChart extends java.applet.Applet {
}
}
@Override
public String getAppletInfo() {
return "Title: Bar Chart \n"
+ "Author: Sami Shaio \n"
+ "A simple bar chart demo.";
}
@Override
public String[][] getParameterInfo() {
String[][] info = {
{"title", "string", "The title of bar graph. Default is 'Chart'"},
{"scale", "int", "The scale of the bar graph. Default is 10."},
{"columns", "int", "The number of columns/rows. Default is 5."},
{"orientation", "{VERTICAL, HORIZONTAL}",
"The orienation of the bar graph. Default is VERTICAL."},
{"c#", "int", "Subsitute a number for #. "
+ "The value/size of bar #. Default is 0."},
{"c#_label", "string", "The label for bar #. "
+ "Default is an empty label."},
{"c#_style", "{SOLID, STRIPED}", "The style of bar #. "
+ "Default is SOLID."},
{"c#_color", "{RED, GREEN, BLUE, PINK, ORANGE, MAGENTA, CYAN, "
{ "title", "string", "The title of bar graph. Default is 'Chart'" },
{ "scale", "int", "The scale of the bar graph. Default is 10." },
{ "columns", "int", "The number of columns/rows. Default is 5." },
{ "orientation", "{VERTICAL, HORIZONTAL}",
"The orienation of the bar graph. Default is VERTICAL." },
{ "c#", "int", "Subsitute a number for #. "
+ "The value/size of bar #. Default is 0." },
{ "c#_label", "string", "The label for bar #. "
+ "Default is an empty label." },
{ "c#_style", "{SOLID, STRIPED}", "The style of bar #. "
+ "Default is SOLID." },
{ "c#_color", "{RED, GREEN, BLUE, PINK, ORANGE, MAGENTA, CYAN, "
+ "WHITE, YELLOW, GRAY, DARKGRAY}",
"The color of bar #. Default is GRAY."}
"The color of bar #. Default is GRAY." }
};
return info;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册