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

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