|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

10-25-2007, 10:32 PM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 46
|
|
|
Help Creating A Graph From Inputted Data
I have a program that requests a series of numbers from the user. Once everything is inputted the frequency of each number within a range is supposed to be graphed in a JFrame. The graph basically would show on the y-axis the ranges, ie 10-19, 0-9, and the x-axis would show the number falling into each range using a rectangle coming out of the location of the range on the y-axis. I can get the input to work and I have verified that my code compiling all of the data into an array that adds up the frequency is working. My problem is I have no clue where to start with the graphing part of it. I have tried some things with Swing and AWT but with my newbie understanding of Java the results were not good. Here is the code I already have:
import java.io.*;
import javax.swing.*;
public class Histogram {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// creates
int inputtedValue[] = new int[ 10 ]; // array to hold inputted data
for( int inputCounter = 0; inputCounter < inputtedValue.length; inputCounter++ ) {
String input = JOptionPane.showInputDialog
("Please enter a number between 0 and 99\n" +
"Stop entering values at any time by entering -1.\n");
int numberProvided = Integer.parseInt(input);
inputtedValue[ inputCounter ] = numberProvided;
HistogramFreq freqCalc = new HistogramFreq(inputtedValue);
freqCalc.determineFrequency();
}
} // end main
} // end class Histogram
import java.awt.*;
import javax.swing.*;
public class HistogramFreq
{
private int valuesProvided[];
public HistogramFreq(int inputtedValue[])
{
valuesProvided = inputtedValue;
}
public void determineFrequency(){
// stores frequency of inputted numbers in the appropriate ranges
int frequency[] = new int [10];
// for each range increment the appropriate range
for( int valueEntered : valuesProvided)
++frequency[valueEntered/10];
} // end method determineFrequency()
} // end class HistogramFreq
Thanks
|
|

10-26-2007, 12:41 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,144
|
|
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
public class H2 extends JPanel {
int[] data;
final int PAD = 20;
final int BAR_HEIGHT = 25;
H2(int[] data) {
this.data = data;
System.out.print(Arrays.toString(data));
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
// draw axes
g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));
int numBars = data.length;
double yInc = (double)(h - 2*PAD - numBars*BAR_HEIGHT)/(numBars-1);
int maxValue = getMaxValue();
double unitLength = (double)(w - 2*PAD)/maxValue;
// plot data
g2.setPaint(Color.blue);
for(int j = 0; j < numBars; j++) {
double y = PAD + j * (BAR_HEIGHT + yInc);
double width = unitLength*data[j];
g2.fill(new Rectangle2D.Double(PAD, y, width, BAR_HEIGHT));
}
}
private int getMaxValue() {
int max = -Integer.MAX_VALUE;
for(int j = 0; j < data.length; j++) {
if(data[j] > max)
max = data[j];
}
return max;
}
public static void main(String[] args) {
Random seed = new Random();
int[] vals = new int[10];
for(int j = 0; j < vals.length; j++) {
vals[j] = seed.nextInt(100);
System.out.print(vals[j]);
if(j < vals.length-1) System.out.print(" ");
else System.out.println();
}
HistogramFreq freqCalc = new HistogramFreq(vals);
int[] freqs = freqCalc.getValues();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new H2(freqs));
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}
class HistogramFreq {
int[] frequency;
int range = 10;
public HistogramFreq(int[] inputtedValue) {
determineFrequency(inputtedValue);
}
public int[] getValues() {
return frequency;
}
public void determineFrequency(int[] valuesProvided) {
// stores frequency of inputted numbers in the appropriate ranges
frequency = new int[range];
// for each range increment the appropriate range
for( int valueEntered : valuesProvided)
++frequency[valueEntered/range];
}
}
|
|

10-26-2007, 03:59 AM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 46
|
|
|
Hardwired you are the best. Thanks a ton you are helping me start to catch on to this Java thing
|
|

10-26-2007, 06:40 AM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 46
|
|
Hardwired,
I went over the code you provided and it works when I compile it but it never asked for the user to input any values. Therefore, I tried to modify it to include my original code that used a JOptionPane to ask for and receive the data. Problem is now the whole thing does not compile. Any chance you could go over my code and let me know what I'm missing or doing wrong.
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
public class H2 extends JPanel {
int[] data;
final int PAD = 20;
final int BAR_HEIGHT = 25;
H2(int[] data) {
this.data = data;
System.out.print(Arrays.toString(data));
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
// draw axes
g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));
int numBars = data.length;
double yInc = (double)(h - 2*PAD - numBars*BAR_HEIGHT)/(numBars-1);
int maxValue = getMaxValue();
double unitLength = (double)(w - 2*PAD)/maxValue;
// plot data
g2.setPaint(Color.blue);
for(int j = 0; j < numBars; j++) {
double y = PAD + j * (BAR_HEIGHT + yInc);
double width = unitLength*data[j];
g2.fill(new Rectangle2D.Double(PAD, y, width, BAR_HEIGHT));
}
}
private int getMaxValue() {
int max = -Integer.MAX_VALUE;
for(int j = 0; j < data.length; j++) {
if(data[j] > max)
max = data[j];
}
return max;
}
public static void main(String[] args) {
// creates
int inputtedValue[] = new int[ 10 ]; // array to hold inputted data
for( int inputCounter = 0; inputCounter < inputtedValue.length; inputCounter++ ) {
String input = JOptionPane.showInputDialog
("Please enter a number between 0 and 99\n" +
"Stop entering values at any time by entering -1.\n");
int numberProvided = Integer.parseInt(input);
inputtedValue[ inputCounter ] = numberProvided;
}
HistogramFreq freqCalc = new HistogramFreq(vals);
int[] freqs = freqCalc.getValues();
JFrame f= new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(H2(freqs));
f.getSize(400, 400);
f.setLocation(200, 200);
f.setVisible(true);
} // end main
}
import java.awt.*;
import javax.swing.*;
class HistogramFreq {
int[] frequency;
int range = 10;
public HistogramFreq(int[] inputtedValue) {
determineFrequency(inputtedValue);
}
public int[] getValues(){
return frequency;
}
public void determineFrequency(int[] valuesProvided){
// stores frequency of inputted numbers in the appropriate ranges
frequency = new int [range];
// for each range increment the appropriate range
for( int valueEntered : valuesProvided)
++frequency[valueEntered/range];
} // end method determineFrequency()
} // end class HistogramFreq
Thanks
|
|

10-26-2007, 09:25 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,144
|
|
I copied, pasted, altered the class names (to avoid name–clashing with previous class files) and compiled the code in your last post and got this in the console
C:\jexp>javac h3.java
h3.java:62: cannot find symbol
symbol : variable vals
location: class H3
HistogramFreq2 freqCalc = new HistogramFreq2(vals);
^
h3.java:67: cannot find symbol
symbol : method H3(int[])
location: class H3
f.getContentPane().add(H3(freqs));
^
h3.java:68: cannot find symbol
symbol : method getSize(int,int)
location: class javax.swing.JFrame
f.getSize(400, 400);
^
3 errors
1 — You are collecting the user–entered values and saving them in an array named
int inputtedValue[] = new int[ 10 ];
so this would be the logical variable to pass into the HistogramFreq2 class constructor. Instead you have an undeclared variable "vals".
HistogramFreq2 freqCalc = new HistogramFreq2(vals);
// should be
HistogramFreq2 freqCalc = new HistogramFreq2(inputtedValue);
2 — You have omitted the new operator here
f.getContentPane().add(H3(freqs));
// should be
f.getContentPane().add(new H3(freqs));
3 — Typo
f.getSize(400, 400);
// should be
f.setSize(400, 400);
Console errors generally give the line number of the difficulty in your source file and tell what is wrong.
|
|

10-26-2007, 03:31 PM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 46
|
|
|
Thanks again. I'm still trying to understand some of the error messages.
|
|

10-28-2007, 05:45 AM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 46
|
|
Hardwired I've been able to get the program to accept the data and display the plotted graph. But I have another question about this graph program. I'm trying to add labels for the x and y data ranges, axis labels stating what each does and a title to the graph. I have tried a couple of things within the class that creates the graph but have not had success after trying all day Saturday. I'm starting to get frustrated and was wondering if you had some thoughts on how to resolve it. Here is what I have for code:
import java.io.*;
import javax.swing.*;
public class Histogram {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// creates
int inputtedValue[] = new int[ 10 ]; // array to hold inputted data
int validInput = 0;
for( int inputCounter = 0; inputCounter < 20; inputCounter++ ) {
String input = JOptionPane.showInputDialog
("Please enter a number between 0 and 99\n" +
"Stop entering values at any time by entering -1.\n");
int numberProvided = Integer.parseInt(input);
if(numberProvided == -1){
break;
}
if((numberProvided >= 0) && (numberProvided <= 99)){
inputtedValue[ inputCounter ] = numberProvided;
validInput++;
}
if(validInput == 10){
break;
}
}
HistogramFreq freqCalc = new HistogramFreq(inputtedValue);
int[] freqs = freqCalc.getValues();
JFrame f= new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new H3(freqs));
f.setSize(400, 400);
f.setLocation(200, 200);
f.setVisible(true);
} // end main
} // end class Histogram
import java.awt.*;
import javax.swing.*;
public class HistogramFreq {
int[] frequency;
int range = 10;
public HistogramFreq(int[] inputtedValue) {
determineFrequency(inputtedValue);
}
public int[] getValues(){
return frequency;
}
public void determineFrequency(int[] valuesProvided){
// stores frequency of inputted numbers in the appropriate ranges
frequency = new int [range];
// for each range increment the appropriate range
for( int valueEntered : valuesProvided)
++frequency[valueEntered/range];
} // end method determineFrequency()
} // end class HistogramFreq
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
public class H3 extends JPanel {
int[] data;
final int PAD = 40;
final int BAR_HEIGHT = 25;
H3(int[] data) {
this.data = data;
System.out.print(Arrays.toString(data));
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
// draw axes
g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));
int numBars = data.length;
double yInc = (double)(h - 2*PAD - numBars*BAR_HEIGHT)/(numBars-1);
int maxValue = getMaxValue();
double unitLength = (double)(w - 2*PAD)/maxValue;
// plot data
g2.setPaint(Color.blue);
for(int j = 0; j < numBars; j++) {
double y = PAD + j * (BAR_HEIGHT + yInc);
double width = unitLength*data[j];
g2.fill(new Rectangle2D.Double(PAD, y, width, BAR_HEIGHT));
}
}
private int getMaxValue() {
int max = -Integer.MAX_VALUE;
for(int j = 0; j < data.length; j++) {
if(data[j] > max)
max = data[j];
}
return max;
}
}
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|