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