Creating a bar graph

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 530 Views - Last Post: 13 September 2012 - 02:13 PM Rate Topic: -----

#1 Meatsawce  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 11-September 12

Creating a bar graph

Posted 11 September 2012 - 05:41 PM

Good afternoon. I am currently working on a program that extends an applet and gives you a "graph" with 4 bars on it. The bars must include the name and length on top of each one. They must also scale in size, so for example if one is 200 and another is 100, the graphs have to actually look like one is double the other in size. Here is my current code:

import java.awt.*;
import java.applet.*;

public class FirstApp extends Applet {

public void paint(Graphics g) {

	int bottom = 500;
	int w = Integer.parseInt(getParameter("barW"));
	int h = Integer.parseInt(getParameter("barH"));
	String bName1 = getParameter("bName1");
	String bName2 = getParameter("bName2");
	String bName3 = getParameter("bName3");
	String bName4 = getParameter("bName4");
	int bLeng1 = Integer.parseInt(getParameter("bLenght1"));
	int bLeng2 = Integer.parseInt(getParameter("bLenght2"));
	int bLeng3 = Integer.parseInt(getParameter("bLenght3"));
	int bLeng4 = Integer.parseInt(getParameter("bLenght4"));
	int bLengMAX = 0;
	int i = 1;
	int height = Integer.parseInt(getParameter("height"));
	int [] bLeng = new int[4];

	while (i <= 4) {
		if (bLeng[i] > bLengMAX)
		bLengMAX = bLeng[i]; {

		i++; {



	g.setColor(Color.blue);
	g.fillRect(w, h, 50, ((bottom - (bLeng1 * (bLengMAX/height)))));
	g.setColor(Color.red);
	g.fillRect((w + 100), h, 50, (bottom - (bLeng2 * (bLengMAX/height))));
	g.setColor(Color.green);
	g.fillRect((w + 200), h, 50, (bottom - (bLeng3 * (bLengMAX/height))));
	g.setColor(Color.cyan);
	g.fillRect((w + 300), h, 50, (bottom - (bLeng4 * (bLengMAX/height))));


		}
      }
	}
  }
}


It seemed like every time I solved the problem, I ran into another. It currently has me drawing 4 different rectangles, each about 100w from each other with the starting point being stated in the program. The issue I'm having is the height parameter of the rectangles, in which it currently has an algorithm for scaling given by our professor. When I open the .html file that goes with it, I just get 4 rects with the same size. Any suggestion on what I could possibly be doing wrong would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Creating a bar graph

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8016
  • View blog
  • Posts: 31,118
  • Joined: 06-March 08

Re: Creating a bar graph

Posted 11 September 2012 - 05:58 PM

A few glitches :) Indent your code properly it will become more obvious

		int i = 1;    // why init to 1 ??? Array start at 0
		int height = Integer.parseInt(getParameter("height"));
		int [] bLeng = new int[4];

		while (i <= 4) {
			if (bLeng[i] > bLengMAX)
				bLengMAX = bLeng[i]; 
			{

				i++;   // at first iteration i will be 2 ???
				{


Was This Post Helpful? 0
  • +
  • -

#3 Meatsawce  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 11-September 12

Re: Creating a bar graph

Posted 11 September 2012 - 06:07 PM

I changed it back to 0 and got the same results. I think my thought process behind the 1 was that it would start at one instead of 0. Little did I realize it would skip the 1 and go into the 2.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8016
  • View blog
  • Posts: 31,118
  • Joined: 06-March 08

Re: Creating a bar graph

Posted 11 September 2012 - 07:25 PM

If you set it to 0 you will have to perform the i++ at the end of the while
and the i <= 4 is also false ut will generate an out of bound exception

and what is the use of the array bLeng[] ?
all its elements are initialized to 0.. what do you do with them ?
Was This Post Helpful? 0
  • +
  • -

#5 Meatsawce  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 11-September 12

Re: Creating a bar graph

Posted 11 September 2012 - 07:42 PM

I set the array and while loop so that it would take the 4 length inputs in the html file and find which one is the largest. From there, I put that into the rectangles scale algorithm given by the teacher in order to make it so the sizes scaled comparative to their values on the html file. At least, that's what I was trying to get it to do.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8016
  • View blog
  • Posts: 31,118
  • Joined: 06-March 08

Re: Creating a bar graph

Posted 11 September 2012 - 07:55 PM

So something like that ?
	int [] bLeng = new int[4];
	bLeng[0] = Integer.parseInt(getParameter("bLenght1"));
	bLeng[1] = Integer.parseInt(getParameter("bLenght2"));
	bLeng[2] = Integer.parseInt(getParameter("bLenght3"));
	bLeng[3] = Integer.parseInt(getParameter("bLenght4"));

	int bLengMAX = bLeng[0];
	for(int i = 1; i < 4; ++i) {
		if (bLeng[i] > bLengMAX)
		    bLengMAX = bLeng[i]; 
	}   // closing here } not { as you have
        


Was This Post Helpful? 0
  • +
  • -

#7 Meatsawce  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 11-September 12

Re: Creating a bar graph

Posted 11 September 2012 - 08:05 PM

When I applied the above corrections, it gave me an error in the lines containing bLeng[0] - bLeng[4]. It said that it "expected" a "]" where all of the numbers were.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8016
  • View blog
  • Posts: 31,118
  • Joined: 06-March 08

Re: Creating a bar graph

Posted 11 September 2012 - 08:14 PM

View PostMeatsawce, on 11 September 2012 - 11:05 PM, said:

When I applied the above corrections, it gave me an error in the lines containing bLeng[0] - bLeng[4]. It said that it "expected" a "]" where all of the numbers were.

Where is the line containing: bLeng[0] - bLeng[4] ???
Was This Post Helpful? 0
  • +
  • -

#9 Meatsawce  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 11-September 12

Re: Creating a bar graph

Posted 11 September 2012 - 08:21 PM

 	int [] bLeng = new int[4]; // Sets up an array to be used in while loop for setting a maximum lenght
	int bLeng[0] = Integer.parseInt(getParameter("bLenght1")); // These next four lines get the bridge lenghts from the html file
	int bLeng[1] = Integer.parseInt(getParameter("bLenght2"));
	int bLeng[2] = Integer.parseInt(getParameter("bLenght3"));
	int bLeng[3] = Integer.parseInt(getParameter("bLenght4")); 


This part of the code.
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8016
  • View blog
  • Posts: 31,118
  • Joined: 06-March 08

Re: Creating a bar graph

Posted 11 September 2012 - 08:25 PM

Post the faulty line of code and the error message
Was This Post Helpful? 0
  • +
  • -

#11 Meatsawce  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 11-September 12

Re: Creating a bar graph

Posted 11 September 2012 - 08:31 PM

 	int [] bLeng = new int[4]; // Sets up an array to be used in while loop for setting a maximum lenght
	int bLeng[0] = Integer.parseInt(getParameter("bLenght1")); // These next four lines get the bridge lenghts from the html file
	int bLeng[1] = Integer.parseInt(getParameter("bLenght2"));
	int bLeng[2] = Integer.parseInt(getParameter("bLenght3"));
	int bLeng[3] = Integer.parseInt(getParameter("bLenght4")); 


Under all of the numbers inside the [] the error said: "]" expected. Under all of the "]" after the number in each one, the error said: Illegal start of expression. I tried to copy and paste it but it wouldn't allow me to copy the part that said where the errors were.
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8016
  • View blog
  • Posts: 31,118
  • Joined: 06-March 08

Re: Creating a bar graph

Posted 11 September 2012 - 08:39 PM

it is
bLeng[0] = Integer.parseInt(getParameter("bLenght1"));
not
int bLeng[0] = Integer.parseInt(getParameter("bLenght1"));
Was This Post Helpful? 1
  • +
  • -

#13 Meatsawce  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 11-September 12

Re: Creating a bar graph

Posted 11 September 2012 - 08:47 PM

Wow, can't believe I didn't notice that difference in mine and the one you showed me. Always the smallest things. Good news is that the program compiled... Unfortunately when I opened the .html that corresponds to it, all of the rectangles were still the same size. Could the parameters given to the squares be affecting this?
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8016
  • View blog
  • Posts: 31,118
  • Joined: 06-March 08

Re: Creating a bar graph

Posted 12 September 2012 - 04:59 AM

Post your updated code
Was This Post Helpful? 0
  • +
  • -

#15 Meatsawce  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 11-September 12

Re: Creating a bar graph

Posted 12 September 2012 - 09:31 AM

I actually ended up working on it some more earlier today. The new one is this:

 import java.awt.*;
import java.applet.*;

public class FirstApp extends Applet {

public void paint(Graphics g) {

	int bottom = 500; // Sets the bottom of the bars to always be on 500
	int w = Integer.parseInt(getParameter("barW")); // barW and barH are the values in which the first bar will start off on
	int h = Integer.parseInt(getParameter("barH"));
	String bName1 = getParameter("bName1"); // The next four lines are used to get the bridge names from the html file
	String bName2 = getParameter("bName2");
	String bName3 = getParameter("bName3");
	String bName4 = getParameter("bName4");
	int [] bLeng = new int[4]; // Sets up an array to be used in while loop for setting a maximum lenght
	bLeng[0] = Integer.parseInt(getParameter("bLenght1")); // These next four lines get the bridge lenghts from the html file
	bLeng[1] = Integer.parseInt(getParameter("bLenght2"));
	bLeng[2] = Integer.parseInt(getParameter("bLenght3"));
	bLeng[3] = Integer.parseInt(getParameter("bLenght4"));
	int i = 0; // Used for loops
	int height = Integer.parseInt(getParameter("height")); // Used for the scale algorithm

	int bLengMAX = bLeng[0]; // Set a new variable called bLengMAX used in the for loop to figure out longest lenght to be used in the scaling algorithm

	// This for loop will look for the longest lenght and set it as bLengMAX
	for (i = 0; i < 4; i++) {
		if (bLeng[i] > bLengMAX)
		bLengMAX = bLeng[i];

		}


		// Creates the four rectangles and sets their colors
		g.setColor(Color.blue);
		g.fillRect(w, ((bottom - (bLeng[0]  * (height/bLengMAX)))), 75, (bLeng[0] * (height/bLengMAX)));
		g.setColor(Color.red);
		g.fillRect((w + 100), (bottom - (bLeng[1]  * (height/bLengMAX))), 75, (bLeng[1] * (height/bLengMAX)));
		g.setColor(Color.green);
		g.fillRect((w + 200), (bottom - (bLeng[2]  * (height/bLengMAX))), 75, (bLeng[2] * (height/bLengMAX)));
		g.setColor(Color.cyan);
		g.fillRect((w + 300), (bottom - (bLeng[3]  * (height/bLengMAX))), 75, (bLeng[3] * (height/bLengMAX)));


		}
    } 


It ended up working when I put small values in (i.e. 100, 200, 300, 400 for the 4 bridge lengths) but when I try putting in large values to see if the scale was working properly, it would not show the graphs. I feel as if something is wrong with the algorithm for the scale. Which is the (bLeng[] * (height/bLengMAX)) you see in the rectangle parameters.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2