Results 1 to 7 of 7
- 10-25-2007, 09:32 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 46
- Rep Power
- 0
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:
Java 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 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 HistogramThanksJava Code: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
- 10-25-2007, 11:41 PM #2
Java Code: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, 02:59 AM #3
Member
- Join Date
- Jul 2007
- Posts
- 46
- Rep Power
- 0
Hardwired you are the best. Thanks a ton you are helping me start to catch on to this Java thing
- 10-26-2007, 05:40 AM #4
Member
- Join Date
- Jul 2007
- Posts
- 46
- Rep Power
- 0
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.
Java Code: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 }ThanksJava Code: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
- 10-26-2007, 08:25 AM #5
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
1 — You are collecting the user–entered values and saving them in an array namedJava Code: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
so this would be the logical variable to pass into the HistogramFreq2 class constructor. Instead you have an undeclared variable "vals".Java Code:int inputtedValue[] = new int[ 10 ];
2 — You have omitted the new operator hereJava Code:HistogramFreq2 freqCalc = new HistogramFreq2(vals); // should be HistogramFreq2 freqCalc = new HistogramFreq2(inputtedValue);
3 — TypoJava Code:f.getContentPane().add(H3(freqs)); // should be f.getContentPane().add(new H3(freqs));
Console errors generally give the line number of the difficulty in your source file and tell what is wrong.Java Code:f.getSize(400, 400); // should be f.setSize(400, 400);
- 10-26-2007, 02:31 PM #6
Member
- Join Date
- Jul 2007
- Posts
- 46
- Rep Power
- 0
Thanks again. I'm still trying to understand some of the error messages.
- 10-28-2007, 04:45 AM #7
Member
- Join Date
- Jul 2007
- Posts
- 46
- Rep Power
- 0
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:
Java 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 HistogramJava Code: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 HistogramFreqJava Code: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; } }
Similar Threads
-
Plot 2D graph in Java from RS-232 data
By spratana in forum Java 2DReplies: 4Last Post: 02-11-2009, 06:49 PM -
Need Help for Dot Plot Graph
By BHCluster in forum Java 2DReplies: 5Last Post: 04-15-2008, 02:54 PM -
Help with a Graph Data Structure and the Shortest Path
By rhm54 in forum New To JavaReplies: 1Last Post: 03-21-2008, 03:14 PM -
JVM terminated while creating a new Data-Source
By woeko in forum EclipseReplies: 0Last Post: 01-03-2008, 12:28 PM -
Creating a stack with data
By ai_2007 in forum Advanced JavaReplies: 2Last Post: 07-02-2007, 03:28 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks