Results 1 to 7 of 7
  1. #1
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

    Default 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 Histogram
    Java 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
    Thanks

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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];
        }
    }

  3. #3
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

    Default

    Hardwired you are the best. Thanks a ton you are helping me start to catch on to this Java thing

  4. #4
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

    Default

    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
    }
    Java 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
    Thanks

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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
    Java 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
    1 — You are collecting the user–entered values and saving them in an array named
    Java Code:
    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".
    Java Code:
    HistogramFreq2 freqCalc = new HistogramFreq2(vals);
    // should be
    HistogramFreq2 freqCalc = new HistogramFreq2(inputtedValue);
    2 — You have omitted the new operator here
    Java Code:
    f.getContentPane().add(H3(freqs));
    // should be
    f.getContentPane().add(new H3(freqs));
    3 — Typo
    Java Code:
    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.

  6. #6
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

    Default

    Thanks again. I'm still trying to understand some of the error messages.

  7. #7
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

    Default

    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 Histogram
    Java 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 HistogramFreq
    Java 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

  1. Plot 2D graph in Java from RS-232 data
    By spratana in forum Java 2D
    Replies: 4
    Last Post: 02-11-2009, 06:49 PM
  2. Need Help for Dot Plot Graph
    By BHCluster in forum Java 2D
    Replies: 5
    Last Post: 04-15-2008, 02:54 PM
  3. Replies: 1
    Last Post: 03-21-2008, 03:14 PM
  4. Replies: 0
    Last Post: 01-03-2008, 12:28 PM
  5. Creating a stack with data
    By ai_2007 in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 03:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •