Question:
ok, this is my homework.?
spinelly
2009-08-19 13:51:09 UTC
Generate a "Histogram" class that displays a "dynamic" graph(one which is formatted each run based on a user input). Use a Label to instruct to input the number of trials; this could be the number of flips of a coin or the number of throws of a dice. Entering the number in a textField will cause the "normalized" histogram to be plotted on a Canvas. The instructions should then suggest typing in a new number for another plot.

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

}
}
}
}
}
Three answers:
Panqueque
2009-08-19 19:28:06 UTC
import javax.swing.*;

import java.awt.*;

import java.awt.event.*;



public class Histogram extends JFrame implements ActionListener {

private JLabel inputLabel = new JLabel("Please input number of trials: ");

private JLabel headsLabel = new JLabel("Heads = white");

private JLabel tailsLabel = new JLabel("Tails = black");

private TextField input = new TextField(5);

private DrawOn canvas = new DrawOn();

private int heads = 0;

private int tails = 0;

JPanel panel = new JPanel();



public Histogram() {

panel.setBackground(Color.PINK);

panel.add(inputLabel);

panel.add(input);

panel.add(canvas);

panel.add(headsLabel);

panel.add(tailsLabel);



canvas.setBackground(Color.GREEN);

canvas.setSize(300, 300);

getContentPane().add(panel);

input.addActionListener(this);

}



public void actionPerformed(ActionEvent e) {

int flip;

int trials = Integer.parseInt(input.getText());

int counter = 0;



heads = 0;

tails = 0;

while (counter < trials) {

flip = (int)(Math.random() * 2 + 1);

if (flip == 1) {

heads++;

} else {

tails++;

}

counter = counter + 1;

}

input.setText("");

canvas.repaint();

}



public class DrawOn extends Canvas {

public void paint(Graphics g) {

g.setColor(Color.white);

g.fillRect(30, 300 - heads, 110, heads);

g.setColor(Color.black);

g.fillRect(170, 300- tails, 110, tails);

}

}



public static void main(String[] args) {

JFrame f = new Histogram();

f.setVisible(true);

}

}
loffelbein
2016-11-29 08:44:58 UTC
i do no longer see why we are in college see you later if we receive plenty homework. i'm at present on homestead certain because of the fact I had surgical treatment. A instructor includes my homestead two times a week to examine with me approximately my assignments. =P the day previous to this, I spent approximately 8-9 hours doing artwork. I nonetheless have approximately that plenty left. I understand that i'm no longer in college so greater homework may well be coming my way yet nonetheless... I somewhat have time for existence. whilst it includes a definite element, I in basic terms end and turn in a 0.5 carried out project. -_- I realize it somewhat isn't the suitable ingredient yet 0.5 way with the aid of the college 3 hundred and sixty 5 days, i'm getting bored to death.
hola
2009-08-19 13:59:11 UTC
idk, sry


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...