import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class lab4 extends JFrame {
private JTextArea jta;
private Histogram histogram = new Histogram();
public lab4()
{
// Store text area in a scroll pane
JScrollPane scrollPane = new JScrollPane(jta = new JTextArea());
jta.setWrapStyleWord(true);
jta.setLineWrap(true);
jta.setPreferredSize(new Dimension(800,300));
jta.setLayout(new GridLayout(2,1,1,1));
add(scrollPane);
jta.addKeyListener(new KeyAdapter() {
/** Handle the button action */
public void KeyPressed(KeyEvent e) {
// Count the letters in the text area
int[] count = countLetters();
// Set the letter count to histogram for display
histogram.showHistogram(count);
}
});
jta.add(histogram);
}
private int[] countLetters()
{
// Count for 26 letters
int[] count = new int[26];
// Get contents from the text area
String text = jta.getText();
text = text.toUpperCase();
// Count occurrence of each letter (case insensitive)
for (int i = 0; i < text.length(); i++)
{
char character = text.charAt(i);
if ((character >= 'A') && (character <= 'Z'))
{
count[(int)character - 65]++; // The ASCII for 'A' is 65
}
}
return count; // Return the count array
}
public static void main(String[] args) {
lab4 frame = new lab4();
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("lab4");
frame.setSize(800,600);
frame.pack();
frame.setVisible(true);
}
}
here is the histogram code:
import javax.swing.*;
import java.awt.*;
public class Histogram extends JPanel {
// Count the occurrence of 26 letters
private int[] count;
/** Set the count and display histogram */
public void showHistogram(int[] count) {
this.count = count;
repaint();
}
/** 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
g.drawString((char)(65 + i) + "", x, height - 30);
// Move x for displaying the next character
x += interval;
}
}
/** Override getPreferredSize */
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
}
what this is sapposed to do is display an input field in the bottom which a user inputs random letters and in the top half it displays in bar graph form the amount of A's-Z's the user inputed
This post has been edited by rgfirefly24: 18 April 2008 - 09:51 AM

Start a new topic
Add Reply




MultiQuote


| 


