jdk/src/share/demo/applets/BarChart/BarChart.java
author jgish
Thu, 18 Apr 2013 16:33:11 -0400
changeset 17159 bb566a21b661
parent 10292 ed7db6a12c2a
permissions -rw-r--r--
8012005: LogManager needs test to ensure stack trace is not being done to find bundle Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
8972
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
     2
 * Copyright (c) 1997, 2011, 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
10292
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8972
diff changeset
    32
/*
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8972
diff changeset
    33
 * This source code is provided to illustrate the usage of a given feature
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8972
diff changeset
    34
 * or technique and has been deliberately simplified. Additional steps
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8972
diff changeset
    35
 * required for a production-quality application, such as security checks,
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8972
diff changeset
    36
 * input validation and proper error handling, might not be present in
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8972
diff changeset
    37
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8972
diff changeset
    38
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8972
diff changeset
    39
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8972
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
8972
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
    44
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * A simple bar chart demo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author Sami Shaio
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @modified 06/21/00 Daniel Peek : refactored, comments
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
8972
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
    50
@SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public class BarChart extends java.applet.Applet {
8972
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
    52
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final int VERTICAL = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final int HORIZONTAL = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static final int SOLID = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static final int STRIPED = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private int orientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private String title;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private Font font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private FontMetrics metrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private int columns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private int values[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private Color colors[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private String labels[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private int styles[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private int scale = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private int maxLabelWidth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private int barSpacing = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private int maxValue = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
8972
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
    71
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public void init() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        getSettings();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        values = new int[columns];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        labels = new String[columns];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        styles = new int[columns];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        colors = new Color[columns];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
8972
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
    81
        for (int i = 0; i < columns; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            parseValue(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            parseLabel(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            parseStyle(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            parseColor(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private void getSettings() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        font = new java.awt.Font("Monospaced", Font.BOLD, 12);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        metrics = getFontMetrics(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        title = getParameter("title");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (title == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            title = "Chart";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        String temp = getParameter("columns");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (temp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            columns = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            columns = Integer.parseInt(temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        temp = getParameter("scale");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (temp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            scale = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            scale = Integer.parseInt(temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        temp = getParameter("orientation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (temp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            orientation = VERTICAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        } else if (temp.equalsIgnoreCase("horizontal")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            orientation = HORIZONTAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            orientation = VERTICAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private void parseValue(int i) {
8972
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   123
        String temp = getParameter("C" + (i + 1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            values[i] = Integer.parseInt(temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            values[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        } catch (NullPointerException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            values[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        maxValue = Math.max(maxValue, values[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private void parseLabel(int i) {
8972
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   135
        String temp = getParameter("C" + (i + 1) + "_label");
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   136
        if (temp == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            labels[i] = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            labels[i] = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
8972
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   141
        maxLabelWidth = Math.max(metrics.stringWidth(labels[i]), maxLabelWidth);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private void parseStyle(int i) {
8972
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   145
        String temp = getParameter("C" + (i + 1) + "_style");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (temp == null || temp.equalsIgnoreCase("solid")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            styles[i] = SOLID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        } else if (temp.equalsIgnoreCase("striped")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            styles[i] = STRIPED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            styles[i] = SOLID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private void parseColor(int i) {
8972
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   156
        String temp = getParameter("C" + (i + 1) + "_color");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (temp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            temp = temp.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            if (temp.equals("red")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                colors[i] = Color.red;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            } else if (temp.equals("green")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                colors[i] = Color.green;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            } else if (temp.equals("blue")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                colors[i] = Color.blue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            } else if (temp.equals("pink")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                colors[i] = Color.pink;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            } else if (temp.equals("orange")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                colors[i] = Color.orange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            } else if (temp.equals("magenta")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                colors[i] = Color.magenta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            } else if (temp.equals("cyan")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                colors[i] = Color.cyan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            } else if (temp.equals("white")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                colors[i] = Color.white;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            } else if (temp.equals("yellow")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                colors[i] = Color.yellow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            } else if (temp.equals("gray")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                colors[i] = Color.gray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            } else if (temp.equals("darkgray")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                colors[i] = Color.darkGray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                colors[i] = Color.gray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            colors[i] = Color.gray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
8972
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   189
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        // draw the title centered at the bottom of the bar graph
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        g.setColor(Color.black);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        g.setFont(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        int titleWidth = metrics.stringWidth(title);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        int cx = Math.max((getSize().width - titleWidth) / 2, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        int cy = getSize().height - metrics.getDescent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        g.drawString(title, cx, cy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        // draw the bars and their titles
8972
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   203
        if (orientation == HORIZONTAL) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            paintHorizontal(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        } else {  // VERTICAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            paintVertical(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    private void paintHorizontal(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        // x and y coordinates to draw/write to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        int cx, cy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        int barHeight = metrics.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        for (int i = 0; i < columns; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            // set the X coordinate for this bar and label and center it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            int widthOfItems = maxLabelWidth + 3 + (maxValue * scale) + 5
8972
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   219
                    + metrics.stringWidth(Integer.toString(maxValue));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            cx = Math.max((getSize().width - widthOfItems) / 2, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            // set the Y coordinate for this bar and label
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            cy = getSize().height - metrics.getDescent() - metrics.getHeight()
8972
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   224
                    - barSpacing
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   225
                    - ((columns - i - 1) * (barSpacing + barHeight));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            // draw the label
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            g.setColor(Color.black);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            g.drawString(labels[i], cx, cy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            cx += maxLabelWidth + 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            // draw the shadow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            g.fillRect(cx + 4, cy - barHeight + 4,
8972
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   235
                    (values[i] * scale), barHeight);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            // draw the bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            g.setColor(colors[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (styles[i] == STRIPED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                for (int k = 0; k <= values[i] * scale; k += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    g.drawLine(cx + k, cy - barHeight, cx + k, cy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            } else {      // SOLID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                g.fillRect(cx, cy - barHeight,
8972
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   245
                        (values[i] * scale) + 1, barHeight + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            cx += (values[i] * scale) + 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            // draw the value at the end of the bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            g.setColor(g.getColor().darker());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            g.drawString(Integer.toString(values[i]), cx, cy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    private void paintVertical(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        int barWidth = maxLabelWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        for (int i = 0; i < columns; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            // X coordinate for this label and bar (centered)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            int widthOfItems = (barWidth + barSpacing) * columns - barSpacing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            int cx = Math.max((getSize().width - widthOfItems) / 2, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            cx += (maxLabelWidth + barSpacing) * i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            // Y coordinate for this label and bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            int cy = getSize().height - metrics.getHeight()
8972
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   267
                    - metrics.getDescent() - 4;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            // draw the label
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            g.setColor(Color.black);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            g.drawString(labels[i], cx, cy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            cy -= metrics.getHeight() - 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            // draw the shadow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            g.fillRect(cx + 4, cy - (values[i] * scale) - 4,
8972
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   276
                    barWidth, (values[i] * scale));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            // draw the bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            g.setColor(colors[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            if (styles[i] == STRIPED) {
8972
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   281
                for (int k = 0; k <= values[i] * scale; k += 2) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    g.drawLine(cx, cy - k,
8972
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   283
                            cx + barWidth, cy - k);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                g.fillRect(cx, cy - (values[i] * scale),
8972
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   287
                        barWidth + 1, (values[i] * scale) + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            cy -= (values[i] * scale) + 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            // draw the value on top of the bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            g.setColor(g.getColor().darker());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            g.drawString(Integer.toString(values[i]), cx, cy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
8972
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   297
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public String getAppletInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        return "Title: Bar Chart \n"
8972
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   300
                + "Author: Sami Shaio \n"
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   301
                + "A simple bar chart demo.";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
8972
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   304
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public String[][] getParameterInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        String[][] info = {
8972
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   307
            { "title", "string", "The title of bar graph.  Default is 'Chart'" },
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   308
            { "scale", "int", "The scale of the bar graph.  Default is 10." },
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   309
            { "columns", "int", "The number of columns/rows.  Default is 5." },
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   310
            { "orientation", "{VERTICAL, HORIZONTAL}",
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   311
                "The orienation of the bar graph.  Default is VERTICAL." },
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   312
            { "c#", "int", "Subsitute a number for #.  "
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   313
                + "The value/size of bar #.  Default is 0." },
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   314
            { "c#_label", "string", "The label for bar #.  "
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   315
                + "Default is an empty label." },
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   316
            { "c#_style", "{SOLID, STRIPED}", "The style of bar #.  "
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   317
                + "Default is SOLID." },
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   318
            { "c#_color", "{RED, GREEN, BLUE, PINK, ORANGE, MAGENTA, CYAN, "
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   319
                + "WHITE, YELLOW, GRAY, DARKGRAY}",
1bde3211f2c6 7027674: /applets/BarChart demo needs to be improved
mrkam
parents: 5506
diff changeset
   320
                "The color of bar #.  Default is GRAY." }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        return info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
}