import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Assignment extends Applet implements ActionListener
{
TextArea textInput = new TextArea(); // user input
Button analyzebutton = new Button("Analyze");
Button resetbutton = new Button("Reset");
Label lbloutput = new Label ("Please enter text into the textbox!");
Label lblmean = new Label ("");
int [] xw = {400,410,415,425,435,440,450,445,435,425,415,405};
int [] yw = {260,260,280,270,280,260,260,300,300,290,300,300};
int [] xr = {350,370,380,380,370,380,370,360,360,350};
int [] yr = {260,260,265,275,280,300,300,280,300,300};
int [] xi = {360,365,370,370,365,360};
int [] yi = {265,265,270,275,275,275};
double average, sum = 0.0;
public void init()
{
// give background a color
setBackground(Color.white);
setBackground(Color.black);
lbloutput.setForeground(Color.white);
lblmean.setForeground(Color.white);
//add buttons and labels to applet
add (textInput);
add (analyzebutton);
add (resetbutton);
add (lbloutput);
add (lblmean);
//button to listen for mouse click
analyzebutton.addActionListener(this);
resetbutton.addActionListener(this);
}
public void paint(Graphics g)
{
g.setColor(Color.white);
g.drawPolygon(xw, yw, 12);
g.drawPolygon(xr, yr, 10);
g.drawPolygon(xi, yi, 6);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource () == analyzebutton)
{
String txtInput = textInput.getText ();
int [] lengthOfWordArray = new int [1000];
String [] resultSplit = txtInput.split (" ");
for (int i=0; i<resultSplit.length; i++)
{
int length = resultSplit[i].length();
lengthOfWordArray [length]++;
}
String strlength = "There Are: \r\n";
for (int j=1; j<lengthOfWordArray.length; j++)
{
if (lengthOfWordArray [j] !=0)
{
strlength += lengthOfWordArray[j] + " Word(s) Of Length " + j + ", \r\n";
}
}
for (int i = 0; i<resultSplit.length; i++)
{
int length = resultSplit[i].length ();
lengthOfWordArray[length]++;
}
lbloutput.setText (strlength);
lbloutput.invalidate();
validate();
}
else if (e.getSource () == resetbutton)
{
textInput.setText(" ");
lbloutput.setText("Please enter text into the textbox!");
lbloutput.invalidate();
lbloutput.validate();
repaint();
}
}
}