Histoqram

Ads

 
 
 

Share on Google+Share on Google+

mar
Histoqram
0 Answer(s)      7 years ago
Posted in : Java Beginners
I want to do show histogram, when the user clicks the button, a new window appears that displays a histogram to show the occurrence of the letters in the text area.
This is my coding,

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

g.drawLine(10, height - 45, 10, 10);

// 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);
}
}

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MultipleWindowsDemo extends JFrame {
private JTextArea jta;
private JButton jbtShowHistogram = new JButton("Show Histogram");
private Histogram histogram = new Histogram();
// Create a new frame to hold the histogram panel
private JFrame histogramFrame = new JFrame();

public MultipleWindowsDemo() {
// Store text area in a scroll pane
JScrollPane scrollPane = new JScrollPane(jta = new JTextArea());
scrollPane.setPreferredSize(new Dimension(300, 200));
jta.setWrapStyleWord(true);
jta.setLineWrap(true);
// Place scroll pane and button in the frame
add(scrollPane, BorderLayout.CENTER);
add(jbtShowHistogram, BorderLayout.SOUTH);
// Register listener
jbtShowHistogram.addActionListener(new ActionListener() {
/** Handle the button action */
public void actionPerformed(ActionEvent e) {
// Count the letters in the text area
int[] count = countLetters();
// Set the letter count to histogram for display
histogram.showHistogram(count);
histogramFrame.setVisible(true); // Show the frame
}
});

// Create a new frame to hold the histogram panel
histogramFrame.add(histogram);
histogramFrame.pack();
histogramFrame.setTitle("Histogram");
}

/** Count the letters in the text area */
private int[] countLetters() {
// Count for 26 letters
int[] count = new int[26];
// Get contents from the text area
String text = jta.getText();
// 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
}
else if ((character >= 'a') && (character <= 'z')) {
count[(int)character - 97]++; // The ASCII for 'a' is 97
}
}
return count; // Return the count array
}

public static void main(String[] args) {
MultipleWindowsDemo frame = new MultipleWindowsDemo();
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("MultipleWindowsDemo");
frame.pack();
frame.setVisible(true);
}
}

In the histogram class, i create 'g.drawLine(10, height - 45, 10, 10);'.
When I run it, it shows y-axis..but how to make y-axis has number?I mean for example when user type 'MMM' at text area, then the bar for M is 3.

Ads
View Answers
Ads









Related Tutorials/Questions & Answers:
Tutorials   
Java Spring Hibernate Struts Training java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient How do I resolve this Java Class not found exception? httpclient java.lang.NoClassDefFoundError Apache Commons ioutils maven dependency Read/Convert an inputStream to a String What is the meaning of Java Platform? Why Java is a platform independent language? What is the benefits of learning Core Java? Which technology should I learn after Java? What is array in java with example? How to Convert ArrayList to Array? How to substring in Java? How to format number in Java? What is instance variable in Java? How to download MySQL JDBC driver? What is Calendar class in Java? Which is the best Java tutorials for beginners? How to rename a file in Java? How to delete file in Java code? How to get day from date in Java using Calendar? How to get day of week in Java? How to calculate Date Difference in Java? How to compare date in Java? How to declare array in Java? How to calculate average of array in Java? What is Array in Java? write a java program to find the summation of all the integers entered on command line Sum of two numbers using command line arguments in Java How to create and use Array in Java? How to pass command line arguments in Java? How to create Applet Hello World? Appending String efficiently in Java How to append String in Java? How to list even numbers between 1 and 100? How to add BigDecimal in Java? What is Abstraction In Java? Which is best Beginners Java Tutorial? What is java.util package? Create list from array in Java Filter collection in Java 8 What is the best way to filter a Java Collection? Easy way to transform Collection to Array? How to convert Collection to Array in Java? What are Basic Java Language Elements? Advanced Java Tutorials in 2017 Java brief history Best Reasons to learn Java Java Example Codes and Tutorials in 2017 How do I read a large file quickly in Java? Is learning Java worthwhile?

Ads

 
Advertisement null

Ads