import javax.swing.*; // Needed to build GUI.
import java.awt.*; // Needed for some parts of the GUI (frame colours).
import java.awt.event.*; // Needed to use event listeners.
import java.text.DecimalFormat; /* Needed to format the percentage of vowels
to 2 decimal places. */
public class Statistics extends JFrame
{ // Start of Statistics class.
public void StatsWindow(String text, String printable, String consonants,
String vowels, int nonPrintable, char[] vowelArray, int[] vowelFrequency)
{ // Start of StatsWindow method.
DecimalFormat df = new DecimalFormat("#.##"); // Sets decimal format to 2 places.
// Declare and work out percentage for vowel frequency.
double aPerc = ((double)vowelFrequency[0] / printable.length()) * 100;
double ePerc = ((double)vowelFrequency[1] / printable.length()) * 100;
double iPerc = ((double)vowelFrequency[2] / printable.length()) * 100;
double oPerc = ((double)vowelFrequency[3] / printable.length()) * 100;
double uPerc = ((double)vowelFrequency[4] / printable.length()) * 100;
// Set the properties of the JFrame.
setTitle("Results of Text Analysis");
setSize(600, 425);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// Declare the labels for the results window. Some labels use HTML, some do not.
JLabel msg = new JLabel("The original text is: ");
JLabel origStringLength = new JLabel("<html>Original length of text was "
+ text.length() +
" characters. <P>This includes white-space and non-printing characters.");
JLabel ratios = new JLabel(
"<html>The ratio of printing to non-printing characters is "
+ printable.length() + ":" + nonPrintable
+ ". <P>The ratio of vowels to consonants is "
+ vowels.length() + ":" + consonants.length() + ".");
JLabel aFreq = new JLabel("The vowel 'A' occured " + vowelFrequency[0]
+ " times. That is " + df.format(aPerc) + "% of the printable characters.");
JLabel eFreq = new JLabel("The vowel 'E' occured " + vowelFrequency[1]
+ " times. That is " + df.format(ePerc) + "% of the printable characters.");
JLabel iFreq = new JLabel("The vowel 'I' occured " + vowelFrequency[2]
+ " times. That is " + df.format(iPerc) + "% of the printable characters.");
JLabel oFreq = new JLabel("The vowel 'O' occured " + vowelFrequency[3]
+ " times. That is " + df.format(oPerc) + "% of the printable characters.");
JLabel uFreq = new JLabel("The vowel 'U' occured " + vowelFrequency[4]
+ " times. That is " + df.format(uPerc) + "% of the printable characters.");
JLabel again = new JLabel("Would you like to analyse again?");
// Create new JTextArea with the original String in it and make it scrollable.
JTextArea origString = new JTextArea(text, 4, 25);
origString.setLineWrap(true);
origString.setWrapStyleWord(true);
JScrollPane scroll = new JScrollPane(origString, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
// Create a Yes & No button for if the user wants to analyse again or not.
JButton yesButton = new JButton("Yes");
JButton noButton = new JButton("No");
// Create 5 new JPanels.
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
JPanel p5 = new JPanel();
// Set black borders around each panel.
p1.setBorder(BorderFactory.createLineBorder(Color.black));
p2.setBorder(BorderFactory.createLineBorder(Color.black));
p3.setBorder(BorderFactory.createLineBorder(Color.black));
p4.setBorder(BorderFactory.createLineBorder(Color.black));
p5.setBorder(BorderFactory.createLineBorder(Color.black));
// Set the size of each panel.
p1.setSize(600,100);
p2.setSize(600,50);
p3.setSize(600,50);
p4.setSize(600,110);
p5.setSize(600,115);
// Set the location in the JFrame of each panel.
p1.setLocation(0,0);
p2.setLocation(0,100);
p3.setLocation(0,150);
p4.setLocation(0,200);
p5.setLocation(0,310);
// Add two button listeners.
yesButton.addActionListener(new yesButtonListener());
noButton.addActionListener(new noButtonListener());
// Add elements to each panel.
p1.add(msg);
p1.add(scroll);
p2.add(origStringLength);
p3.add(ratios);
p4.add(aFreq);
p4.add(eFreq);
p4.add(iFreq);
p4.add(oFreq);
p4.add(uFreq);
p5.add(again);
p5.add(yesButton);
p5.add(noButton);
// Add each panel to the JFrame.
add(p1);
add(p2);
add(p3);
add(p4);
add(p5);
// Set the window to visible.
setVisible(true);
} // End of StatsWindow method.
public class yesButtonListener implements ActionListener
{ // Start of yesButtonListener class.
public void actionPerformed(ActionEvent e)
{ // Start of actionPerformed method.
// Opens new instance of the ReadFromKeyboard class (opens new window).
new InputOrFile();
// Sets the old window to not be visible.
setVisible(false);
// Disposes of the old window once invisible.
dispose();
} // End of actionPerformed method.
} // End of yesButtonListener class.
public class noButtonListener implements ActionListener
{ // Start of noButtonListener class.
public void actionPerformed(ActionEvent e)
{ // Start of actionPerformed method.
// Create new JFrame for pop-up message.
JFrame frame = new JFrame();
// Open pop-up message.
JOptionPane.showMessageDialog(frame, "Thank you, program will now exit.");
setVisible(false);
dispose();
// Stop the program.
System.exit(0);
} // End of actionPerformed method.
} // End of noButtonListener class.
} // End of Statistics class.