spinelly
2009-08-19 13:51:09 UTC
ok so thats my homework. i have this already, but it only does ONE bar instead of TWO bars on the chart. i dont know why. please help me modify it!
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class histogram extends JFrame implements ActionListener
{
private JLabel input = new JLabel("Please input number of trials: ");
private JLabel heads = new JLabel("Heads = white");
private JLabel tails = new JLabel("Tails = black");
private TextField data = new TextField(5);
private DrawOn canvas = new DrawOn();
private int[] item = new int[100];//number data
private int count = (int)(Math.random() * 2 +1);
private int head = 1;
private int tail = 2;
JPanel panel = new JPanel();
public histogram()
{
panel.setBackground(Color.PINK);
panel.add(input);
panel.add(data);
panel.add(canvas);
panel.add(heads);
panel.add(tails);
canvas.setBackground(Color.GREEN);
canvas.setSize(300,300);
getContentPane().add(panel);
data.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(head==1)
{
item[head = 1] = new Integer(data.getText()).intValue();
data.setText("");
}
else
{
item[tail = 2] = new Integer(data.getText()).intValue();
data.setText("");
}
canvas.repaint();
}
public class DrawOn extends Canvas
{
public void paint(Graphics g)
{
if(count>0)
{
if (count == head)
{
int width = 250/count;
for(int i = 0; i
g.setColor(Color.white);
g.fillRect(i*width, 250 - item[i], width, item[i]);
}
}
else
{
int width = 250/count;
for (int i =0; i
g.setColor(Color.black);
g.fillRect(i*width, 250-item[i], width, item[i]);
}
}
}
}
}
}