1 Replies - 3303 Views - Last Post: 17 November 2012 - 02:00 PM Rate Topic: -----

#1 Jmporten  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-October 12

Having trouble on a bar graph project

Posted 16 November 2012 - 05:20 PM

So I have this project I've been working on for my computer science class that requires us to write code for an application. Pretty much the user is supposed to enter however many numbers they have and it creates a bar graph.
Here are the steps im supposed to take for where im stuck at:

create a for loop that runs through your array list of values. For each iteration of the loop, you will be drawing a Rectangle to represent the current value’s bar.

In each loop, you should do the following:
1. Calculate the height of a single bar (barHeight) – Remember to use the max value you previously found.
2. Calculate the width of a single bar (barWidth) (This can be done outside the loop as well.)
3. Create a random color, and set that color – remember that a color can be created and saved into a Color variable using the Color constructor, which takes 3 int values 0-255 representing red, green, and blue.
4. Draw the Rectangle for the current bar – Start at an (x,y) of (xleft,height-barHeight), and remember to use g2.fill().
5. Calculate a new xleft for the next iteration of the loop, by adding the width of a single bar to the previous value.


Here is my code

import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;


public class BarChart 
{
	private int width = 0;
	private int height = 0;
	private ArrayList<Double> barChartValues;
	private Random color;

	public void BarSize(double newHeight, double newWidth)
	{
	newHeight = height;
	newWidth = width;
	ArrayList<Double> barChartValues = new ArrayList<Double>();
	Random color = new Random();
	}

	public void add(double newValue)
	{
	barChartValues.add(newValue);
	}

	public void draw(Graphics g2)
	{
	double max = Collections.max(barChartValues);
	int xleft = 0;
	for (int i = 0; i < barChartValues.size(); i++)
	{
		
	}
}
}

This post has been edited by g00se: 17 November 2012 - 08:56 AM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Having trouble on a bar graph project

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10443
  • View blog
  • Posts: 38,682
  • Joined: 27-December 08

Re: Having trouble on a bar graph project

Posted 17 November 2012 - 02:00 PM

Two points. First, you have this line in your constructor: ArrayList<Double> barChartValues = new ArrayList<Double>();. This creates a new local variable instead of initializing your instance variable. Take out the type declaration before barChartValues here in your constructor.

Second, see the Graphics drawRect() method. As with bar charts, the height will vary based on the value of the element you are evaluating.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1