3 Replies - 307 Views - Last Post: 04 April 2013 - 01:47 PM Rate Topic: -----

#1 hambonie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 13-March 12

How to display specific lables in a histogram?

Posted 04 April 2013 - 12:45 PM

my assignment is to create a program that reads a text file then displays a histogram of each letter occurrence. the program runs fine but my professor has challenged me to make it so if a letter does not exist in the text file it will not appear in the histogram. as of now, if the letter count is 0 it is still displayed. below is the histogram class file where i think i need to add an if statement...i'm just not sure how to code it. (the second set of code is my test program)
import javax.swing.*;
import java.awt.*;

 public class Histogram extends JPanel{
 // Count the occurrences of 26 letters
 private int[] count;

 /** Set the count and display histogram */
 public void showHistogram(int[] count) {
 this.count = count;
 repaint();
 }

 @Override /** Paint the histogram */
 protected void paintComponent(Graphics g) {
 if (count == null) return; // No display if count is null

 super.paintComponent(g);

 // Find the panel size and bar width and interval dynamically
 int width = getWidth();
 int height = getHeight();
 int interval = (width - 40) / count.length;
 int individualWidth = (int)(((width - 40) / 24) * 0.60);

 // Find the maximum count. The maximum count has the highest bar
 int maxCount = 0;
 for (int i = 0; i < count.length; i++) {
 if (maxCount < count[i])
 maxCount = count[i];
 }

 // x is the start position for the first bar in the histogram
 int x = 30;

 // Draw a horizontal base line
 g.drawLine(10, height - 45, width - 10, height - 45);
 for (int i = 0; i < count.length; i++) {
 // Find the bar height
 int barHeight =
 (int)(((double)count[i] / (double)maxCount) * (height - 55));

 // Display a bar (i.e., rectangle)
 g.drawRect(x, height - 45 - barHeight, individualWidth,
 barHeight);

 // Display a letter under the base line
//!!!!!
//IM PRETTY SURE THIS IS WHERE I NEED TO DO IT!!!!!
//!!!!!
 g.drawString((char)(65 + i) + "", x, height - 30);

 // Move x for displaying the next character
 x += interval;
 }
 }

 @Override
 public Dimension getPreferredSize() {
 return new Dimension(300, 300);
 }
 }



TEST PROGRAM
//display histogram


import java.util.Scanner;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.awt.*;

import javax.swing.border.LineBorder;
import java.awt.event.ActionListener;





public class CreateHistogram extends JFrame
implements ActionListener
{
int[] counts = new int[26];
private JTextField jtf = new JTextField(20);
private Histogram display = new Histogram();
private JButton jbtShowHistogram = new JButton("Show Histogram");

public CreateHistogram() {
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(new JLabel("Text File"), "West");
p.add(this.jbtShowHistogram, "East");


p.add(this.jtf, "Center");

this.display.setBorder(new LineBorder(Color.black, 1));

setLayout(new BorderLayout());
add(p, "South");
add(this.display, "Center");


this.jbtShowHistogram.addActionListener(this);

}

public void actionPerformed(ActionEvent e)
{
for (int i = 0; i < 26; i++) {
this.counts[i] = 0;
}

try
{
Scanner input = new Scanner(new File(this.jtf.getText().trim()));

while (input.hasNext()) {
String line = input.nextLine();

for (int i = 0; i < line.length(); i++) {
if (Character.isLetter(line.charAt(i))) {
this.counts[(Character.toUpperCase(line.charAt(i)) - 'A')] += 1;
}
}
}

this.display.showHistogram(this.counts);
}
catch (FileNotFoundException ex) {
System.out.println("File not found: " + this.jtf.getText().trim());
}
}

public static void main(String[] args) {
CreateHistogram frame = new CreateHistogram();
frame.setSize(500, 300);
frame.setDefaultCloseOperation(3);
frame.setTitle("Display Histogram");
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: How to display specific lables in a histogram?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to display specific lables in a histogram?

Posted 04 April 2013 - 01:27 PM

if count[i] == 0 then simply don't do

g.drawString((char)(65 + i) + "", x, height - 30);
x += interval;

so in your code, befor calculating the heigh simply add
if(count[i] == 0) continue;
Was This Post Helpful? 1
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: How to display specific lables in a histogram?

Posted 04 April 2013 - 01:27 PM

Why don't you try what you think needs to be done and see what happens? It seems a relatively trivial change relative to the complexity of the code you've posted.
Was This Post Helpful? 0
  • +
  • -

#4 hambonie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 13-March 12

Re: How to display specific lables in a histogram?

Posted 04 April 2013 - 01:47 PM

thanks guys. the fix was simple...for some reason i was having a mental block
   import javax.swing.*;
   import java.awt.*;

   public class Histogram extends JPanel{
   // Count the occurrences of 26 letters
      private int[] count;
   
   /** Set the count and display histogram */
      public void showHistogram(int[] count) {
         this.count = count;
         repaint();
      }
   
      @Override /** Paint the histogram */
      protected void paintComponent(Graphics g) {
         if (count == null) 
            return; // No display if count is null
      
         super.paintComponent(g);
      
      // Find the panel size and bar width and interval dynamically
         int width = getWidth();
         int height = getHeight();
         int interval = (width - 40) / count.length;
         int individualWidth = (int)(((width - 40) / 24) * 0.60);
      
      // Find the maximum count. The maximum count has the highest bar
         int maxCount = 0;
         for (int i = 0; i < count.length; i++) {
            if (maxCount < count[i])
               maxCount = count[i];
         }
      
      // x is the start position for the first bar in the histogram
         int x = 30;
      
      // Draw a horizontal base line
         g.drawLine(10, height - 45, width - 10, height - 45);
         for (int i = 0; i < count.length; i++) {
         // Find the bar height
            int barHeight =
               (int)(((double)count[i] / (double)maxCount) * (height - 55));
         
         // Display a bar (i.e., rectangle
            //System.out.println("Bar height" +barHeight);
            
            g.drawRect(x, height - 45 - barHeight, individualWidth,
                  barHeight);
         /////FIX///////
         // Display a letter under the base line
            if(barHeight != 0){
               g.drawString((char)(65 + i) + "", x, height - 30);
            
            // Move x for displaying the next character
               x += interval;
            }
            else {
               System.out.println("Letter removed: "+ (char)(65+i));
            }
         }
      }
   
      @Override
      public Dimension getPreferredSize() {
         return new Dimension(300, 300);
      }
   }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1