View Single Post
  #4 (permalink)  
Old 10-26-2007, 07:40 AM
adlb1300 adlb1300 is offline
Member
 
Join Date: Jul 2007
Posts: 46
adlb1300 is on a distinguished road
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.

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 }
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
Reply With Quote