Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/demo/applets/BarChart/BarChart.java
38829 views
/*1* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/38394041import java.awt.*;424344/**45* A simple bar chart demo46* @author Sami Shaio47* @modified 06/21/00 Daniel Peek : refactored, comments48*/49@SuppressWarnings("serial")50public class BarChart extends java.applet.Applet {5152private static final int VERTICAL = 0;53private static final int HORIZONTAL = 1;54private static final int SOLID = 0;55private static final int STRIPED = 1;56private int orientation;57private String title;58private Font font;59private FontMetrics metrics;60private int columns;61private int values[];62private Color colors[];63private String labels[];64private int styles[];65private int scale = 10;66private int maxLabelWidth = 0;67private int barSpacing = 10;68private int maxValue = 0;6970@Override71public void init() {7273getSettings();7475values = new int[columns];76labels = new String[columns];77styles = new int[columns];78colors = new Color[columns];7980for (int i = 0; i < columns; i++) {81parseValue(i);82parseLabel(i);83parseStyle(i);84parseColor(i);85}86}8788private void getSettings() {89font = new java.awt.Font("Monospaced", Font.BOLD, 12);90metrics = getFontMetrics(font);9192title = getParameter("title");93if (title == null) {94title = "Chart";95}9697String temp = getParameter("columns");98if (temp == null) {99columns = 5;100} else {101columns = Integer.parseInt(temp);102}103104temp = getParameter("scale");105if (temp == null) {106scale = 10;107} else {108scale = Integer.parseInt(temp);109}110111temp = getParameter("orientation");112if (temp == null) {113orientation = VERTICAL;114} else if (temp.equalsIgnoreCase("horizontal")) {115orientation = HORIZONTAL;116} else {117orientation = VERTICAL;118}119}120121private void parseValue(int i) {122String temp = getParameter("C" + (i + 1));123try {124values[i] = Integer.parseInt(temp);125} catch (NumberFormatException e) {126values[i] = 0;127} catch (NullPointerException e) {128values[i] = 0;129}130maxValue = Math.max(maxValue, values[i]);131}132133private void parseLabel(int i) {134String temp = getParameter("C" + (i + 1) + "_label");135if (temp == null) {136labels[i] = "";137} else {138labels[i] = temp;139}140maxLabelWidth = Math.max(metrics.stringWidth(labels[i]), maxLabelWidth);141}142143private void parseStyle(int i) {144String temp = getParameter("C" + (i + 1) + "_style");145if (temp == null || temp.equalsIgnoreCase("solid")) {146styles[i] = SOLID;147} else if (temp.equalsIgnoreCase("striped")) {148styles[i] = STRIPED;149} else {150styles[i] = SOLID;151}152}153154private void parseColor(int i) {155String temp = getParameter("C" + (i + 1) + "_color");156if (temp != null) {157temp = temp.toLowerCase();158if (temp.equals("red")) {159colors[i] = Color.red;160} else if (temp.equals("green")) {161colors[i] = Color.green;162} else if (temp.equals("blue")) {163colors[i] = Color.blue;164} else if (temp.equals("pink")) {165colors[i] = Color.pink;166} else if (temp.equals("orange")) {167colors[i] = Color.orange;168} else if (temp.equals("magenta")) {169colors[i] = Color.magenta;170} else if (temp.equals("cyan")) {171colors[i] = Color.cyan;172} else if (temp.equals("white")) {173colors[i] = Color.white;174} else if (temp.equals("yellow")) {175colors[i] = Color.yellow;176} else if (temp.equals("gray")) {177colors[i] = Color.gray;178} else if (temp.equals("darkgray")) {179colors[i] = Color.darkGray;180} else {181colors[i] = Color.gray;182}183} else {184colors[i] = Color.gray;185}186}187188@Override189public void paint(Graphics g) {190// draw the title centered at the bottom of the bar graph191g.setColor(Color.black);192g.setFont(font);193194g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);195196int titleWidth = metrics.stringWidth(title);197int cx = Math.max((getSize().width - titleWidth) / 2, 0);198int cy = getSize().height - metrics.getDescent();199g.drawString(title, cx, cy);200201// draw the bars and their titles202if (orientation == HORIZONTAL) {203paintHorizontal(g);204} else { // VERTICAL205paintVertical(g);206}207}208209private void paintHorizontal(Graphics g) {210// x and y coordinates to draw/write to211int cx, cy;212int barHeight = metrics.getHeight();213214for (int i = 0; i < columns; i++) {215216// set the X coordinate for this bar and label and center it217int widthOfItems = maxLabelWidth + 3 + (maxValue * scale) + 5218+ metrics.stringWidth(Integer.toString(maxValue));219cx = Math.max((getSize().width - widthOfItems) / 2, 0);220221// set the Y coordinate for this bar and label222cy = getSize().height - metrics.getDescent() - metrics.getHeight()223- barSpacing224- ((columns - i - 1) * (barSpacing + barHeight));225226// draw the label227g.setColor(Color.black);228g.drawString(labels[i], cx, cy);229cx += maxLabelWidth + 3;230231232// draw the shadow233g.fillRect(cx + 4, cy - barHeight + 4,234(values[i] * scale), barHeight);235236// draw the bar237g.setColor(colors[i]);238if (styles[i] == STRIPED) {239for (int k = 0; k <= values[i] * scale; k += 2) {240g.drawLine(cx + k, cy - barHeight, cx + k, cy);241}242} else { // SOLID243g.fillRect(cx, cy - barHeight,244(values[i] * scale) + 1, barHeight + 1);245}246cx += (values[i] * scale) + 4;247248// draw the value at the end of the bar249g.setColor(g.getColor().darker());250g.drawString(Integer.toString(values[i]), cx, cy);251}252}253254private void paintVertical(Graphics g) {255int barWidth = maxLabelWidth;256257for (int i = 0; i < columns; i++) {258259// X coordinate for this label and bar (centered)260int widthOfItems = (barWidth + barSpacing) * columns - barSpacing;261int cx = Math.max((getSize().width - widthOfItems) / 2, 0);262cx += (maxLabelWidth + barSpacing) * i;263264// Y coordinate for this label and bar265int cy = getSize().height - metrics.getHeight()266- metrics.getDescent() - 4;267268// draw the label269g.setColor(Color.black);270g.drawString(labels[i], cx, cy);271cy -= metrics.getHeight() - 3;272273// draw the shadow274g.fillRect(cx + 4, cy - (values[i] * scale) - 4,275barWidth, (values[i] * scale));276277// draw the bar278g.setColor(colors[i]);279if (styles[i] == STRIPED) {280for (int k = 0; k <= values[i] * scale; k += 2) {281g.drawLine(cx, cy - k,282cx + barWidth, cy - k);283}284} else {285g.fillRect(cx, cy - (values[i] * scale),286barWidth + 1, (values[i] * scale) + 1);287}288cy -= (values[i] * scale) + 5;289290// draw the value on top of the bar291g.setColor(g.getColor().darker());292g.drawString(Integer.toString(values[i]), cx, cy);293}294}295296@Override297public String getAppletInfo() {298return "Title: Bar Chart \n"299+ "Author: Sami Shaio \n"300+ "A simple bar chart demo.";301}302303@Override304public String[][] getParameterInfo() {305String[][] info = {306{ "title", "string", "The title of bar graph. Default is 'Chart'" },307{ "scale", "int", "The scale of the bar graph. Default is 10." },308{ "columns", "int", "The number of columns/rows. Default is 5." },309{ "orientation", "{VERTICAL, HORIZONTAL}",310"The orienation of the bar graph. Default is VERTICAL." },311{ "c#", "int", "Subsitute a number for #. "312+ "The value/size of bar #. Default is 0." },313{ "c#_label", "string", "The label for bar #. "314+ "Default is an empty label." },315{ "c#_style", "{SOLID, STRIPED}", "The style of bar #. "316+ "Default is SOLID." },317{ "c#_color", "{RED, GREEN, BLUE, PINK, ORANGE, MAGENTA, CYAN, "318+ "WHITE, YELLOW, GRAY, DARKGRAY}",319"The color of bar #. Default is GRAY." }320};321return info;322}323}324325326