Reputation: 41
I have created a code that looks for word patterns in a passage of text, it discounts anything that is not a letter. So basically the text 'hello world it is a lovely day' would give 1, 2, 1, 2, 1. the applet displays it in a basic text format, what I want is for it to show the results in a bar chart.
I know how to to it in principle, I need to link my data from my array and get my applet to draw it and then have the width increase depending on the value given (the cart will be drawn horizontally, with the the main base, where the bars start from being vertical, if you understand what i mean)
my problem is, I don't know how to do this code, I don't know how to start it off, any help would be much appreciated.
here is my code
import java.util.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Clarke_J_resitAss extends Applet implements ActionListener
{
Button pr_input1, pr_input2;
Label pr_label;
TextField pr_text;
String pr_name;
public void init()
{
pr_input1 = new Button("Analyze");
pr_input2 = new Button("Reset");
add(pr_input1);
add(pr_input2);
pr_input1.addActionListener(this);
pr_input2.addActionListener(this);
//add the buttons with action listeners
pr_label = new Label("Word Pattern");
add(pr_label);
pr_text = new TextField();
add(pr_text);
pr_text.addActionListener(this);
//add text field
}
public void start()
{
pr_name="";
setSize(400,400);
setBackground(Color.gray);
pr_text.setBackground(Color.white);
}
public void actionPerformed(ActionEvent e){
pr_name = e.getActionCommand();
repaint();
if(e.getSource() == pr_input1)
pr_name = pr_text.getText();
else
if(e.getSource() == pr_input2)
{ pr_name = "";
pr_text.setText("");
pr_label.setText("Word Pattern");
}
repaint();
// The user's input from the text area.
int pr_char;
String array[]=pr_name.split(" ");
int counter=0;
for(int i=0;i<array.length;i++){
int length = array[i].replaceAll("[^A-Za-z]", "").length();
if(counter<length)
counter=length;}
int intArray[]=new int[counter];
for(int i=0;i<intArray.length;i++){
intArray[i]=0;
}
for(int i=0;i<array.length;i++){
intArray[array[i].replaceAll("[^A-Za-z]", "").length()-1]++;
}
String a="";
for(int i=0;i<intArray.length;i++){
if(intArray[i]>0)
{
a+=String.valueOf(intArray[i]);
a+=", ";
}
}
pr_label.setText(a);
pr_char = pr_name.length();
}
public void paint(Graphics g)
{
pr_text.setSize(400, 200);
pr_text.setLocation(0,0);
pr_input1.setLocation(0,220);
pr_input2.setLocation(337,220);
pr_label.setLocation(0,270);
pr_label.setSize(400,30);
}
}
Upvotes: 1
Views: 3820
Reputation: 347204
If you really want to have a crack at writing it yourself try having a read through
Upvotes: 1
Reputation: 39164
If you need to draw charts and things like that with Java, I would suggest you to use JFreechart. It's extremely powerful and flexible, and will save you a lot of time rather than reimplement the wheel.
Upvotes: 4