kenjiro310 0 Newbie Poster

I am trying create a Java program which draws a bar graph based on the input from the command line.

Problem i am running into is that I cannot get the values of the Args[] starrting at index 2 to provide values unless i specify the numbers. ANy thoughts or ideas will help.


Requirements


1. The input is going to be organized in the following format:


<width> <height> <data1> <data2> <data3> ... <dataN>


2. <width> and <height> are going to represent the width and height of the graph respectively.


3. <data1> through <dataN> will represent the value for each of the bars in the bar graph.


4. The largest bar in the bar graph will span from 20 pixels from the top of the graph to the bottom.


5. The size of the other bars are going to be relative to the largest bar. This means that if the largest bar has a value of 100, then a bar with a value of 50 is going to be half its height.


6. There are 5 pixels between each of the bars.


7. The color for each of the bars should be different.


8. The width of the bars is depended on the screen size. All bars should be fully visible on the screen. The first bar in the graph should have a 5 pixel space before it and the last bar graph should have 5 pixel space after it.


9. The program should be able to handle any number of input data.


10. The data can be both floats and integers.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

  public class GraphBarChart1 extends JPanel {
  private double[] value;
 
  private String title;

  public GraphBarChart1(double[] val, String t) {
    
    value = val;
    title = t;
  }
   
  public void paintComponent(Graphics graphics) {
    super.paintComponent(graphics);
    if (value == null || value.length == 0)
      return;
    double minValue = 0;
    double maxValue = 0;
    for (int i = 0; i < value.length; i++) {
      if (minValue > value[i])
        minValue = value[i];
      if (maxValue < value[i])
        maxValue = value[i];
    }

    Dimension dim = getSize();
    int clientWidth = dim.width;
    int clientHeight = dim.height;
    int barWidth = clientWidth / value.length;
    Font titleFont = new Font("Project One", Font.BOLD, 15);
    FontMetrics titleFontMetrics = graphics.getFontMetrics(titleFont);
    Font labelFont = new Font("Project One", Font.PLAIN, 10);
    FontMetrics labelFontMetrics = graphics.getFontMetrics(labelFont);
    int titleWidth = titleFontMetrics.stringWidth(title);
    int q = titleFontMetrics.getAscent();
    int p = (clientWidth - titleWidth) / 2;
    graphics.setFont(titleFont);
    graphics.drawString(title, p, q);
    int top = titleFontMetrics.getHeight();
    int bottom = labelFontMetrics.getHeight();
    if (maxValue == minValue)
      return;
    double scale = (clientHeight - top - bottom) / (maxValue - minValue);
   q = clientHeight - labelFontMetrics.getDescent();
    graphics.setFont(labelFont);

    for (int j = 0; j < value.length; j++) {
      int valueP = j * barWidth + 1;
      int valueQ = top;
      int height = (int) (value[j] * scale);
      if (value[j] >= 0)
        valueQ += (int) ((maxValue - value[j]) * scale);
      else {
        valueQ += (int) (maxValue * scale);
        height = -height;
      }
      //Randomizes bar color
      int R = (int) (Math.random( )*256);
      int G = (int)(Math.random( )*256);
      int B = (int)(Math.random( )*256);
      Color randomColor = new Color(R, G, B);

      //Fills the bars with random colors
      graphics.setColor(randomColor);
      graphics.fillRect(valueP, valueQ, barWidth - 2, height);
      graphics.setColor(Color.black);
      graphics.drawRect(valueP, valueQ, barWidth - 2, height);
      //int labelWidth = labelFontMetrics.stringWidth(labels[j]);
      //p = j * barWidth + (barWidth - labelWidth) / 2;
      //graphics.drawString(labels[j], p, q);
    }
  }

   public static void main(String[] args) {
      for (int i = 0; i < args.length; i++){
       //Grabs the first 2 elements of the users input
       int width = Integer.parseInt(args[0]);
       int height = Integer.parseInt(args[1]);

       //Controls the Height and Width of the frame
       JFrame frame = new JFrame();
       frame.setSize(height, width);

      double[] value = new double[10];
      //String[] labels = new String[10];
      value[0] = 80;
      //labels[0] = "1";

      value[1] = 40;
      //labels[1] = "2";

      value[2] = 30;
      //labels[2] = "2";

      value[3] = 60;
     // labels[3] = "3";

      value[4] = 45;
      //labels[4] = "4";

      value[5] = 96;
      //labels[5] = "5";

      value[6] = 74;
      //labels[6] = "6";

      value[7] = 100;
      //labels[7] = "7";

      value[8] = 240;
      //labels[8] = "8";

      value[9] = 60;
      //labels[9] = "9";

     frame.getContentPane().add(new GraphBarChart1(value, "ProjectOne"));

    WindowListener winListener = new WindowAdapter() {
      public void windowClosing(WindowEvent event) {
        System.exit(0);
        }
    };

    frame.addWindowListener(winListener);
    frame.setVisible(true);

  }
 }
}