Im required to create a simple bar chart for one of my projects. This program requires an input of 1 to 9 integers that are greater than zero. The program is then required to display a very simple bar graph with the integers displayed above the bars. My professor did not explain this program very well to me and this is the first time working with graphics.
The program requires:
A class "SimpleBarChart" (which will extend Frame) with private variables Bar[] bars and int [] inputData among others (like private Graphics g, private int windowWid, windowHt, etc) and the following functions. It also uses an auxiliary class Bars as shown:
class Bar
{ public int height, width, value, nix, nwy;
public Bar() {}
public Bar(int height, int width, int value, int nix, int nwy)
{ this.height = height; etc }
}
Next a constructor SimpleBarChart(), which calls readData(), createBars(), and drawBars().
A function private void readData(), which reads inputData for the bar-chart. Use the code given below, which uses JOptionPane.
A function private void createBars() to create the bars array on assigning width, height, value (bars[i].value = inputData[i]), nix, and nwy of each bar. Requires 25 pixels of space on top and bottom of the display-window, 10 pixels between the bars, and has to allow bar-heights to be scaled to the form of inputData items.
Finally a function private void drawBars() to draw the bars, one at a time with a suitable sleep-time between bars, with two different colors. Requires the use of g.drawString("" +b.value, nix + b.width/2, nwy - 10) to label each bar in black b its value at 10 pixel above its top.
Ive been trying to figure this out all day and I'm lost. Any help would be greatly appreciated!
Heres what code I have so far:
package simplebarchart;
import java.awt.*;
import java.awt.event.*;
import java.awt.Toolkit.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class SimpleBarchart extends JFrame
{
private final int OUTER_MARGIN = 20;
private static final Color BACKGROUND_COLOR = Color.white;
private static final Color BAR_COLOR = Color.red;
private int SPACE_ON_LEFT_RIGHT;
private Image fImageBuffer;
private Insets fInsets;
private Graphics g;
private Bar[] bars;
private static int SLEEP = 500;
private int[] inputData;
class Bar
{
private int height, value;
public int width;
public int nwx;
public int nwy;
public Color color;
public Bar() {}
public Bar(int height, int width, int value, int nwx, int nwy)
{
this.height = height;
this.width = width;
this.value = value;
this.nwx = nwx;
this.nwy = nwy;
}
}
public SimpleBarchart(final int[] inputData)
{
this.inputData = inputData;
addWindowListener(new WindowCloser());
fInsets = getInsets();
setSize(WIDTH + fInsets.left + fInsets.right, HEIGHT + fInsets.top + fInsets.bottom);
setTitle("Bar Chart");
if (((fImageBuffer = createImage(WIDTH, HEIGHT)) == null) ||
((g = fImageBuffer.getGraphics()) == null))
System.exit(1);
readData();
createBars();
getContentPane().add(new SimpleBarchart(inputData), BorderLayout.CENTER);
setVisible(true);
}
/**
*
* @param g
*/
protected void paintComponent(final Graphics g) {
g.drawImage(fImageBuffer, fInsets.left, fInsets.top, null);
}
class WindowCloser extends WindowAdapter
{
@Override
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
private void readData()
{
String[] inputItems = JOptionPane.showInputDialog("Enter 1 to 9 integers > 0").trim().split(" +");
int numData = inputItems.length;
inputData = new int[numData];
for (int itemIndex = 0; itemIndex < inputItems.length; itemIndex++)
inputData[itemIndex] = numData;
}
private void createBars()
{
//Im confused on how to create the bars for this program.
//This function requires 25 pixels of space on top and bottom of the display- window, 10 pixels between the bars, and has to allow bar-heights to be **scaled to the form of inputData items.**
Bar[] bars = new Bar[];
int pixelBetweenBars = 25;
int width = 800 + 2*OUTER_MARGIN;
int height = 600 + 2*OUTER_MARGIN;
}
private void drawBars(final Graphics g)
{
int OUTER_MARGIN = 20,
WIDTH = 800 + 2 * OUTER_MARGIN,
HEIGHT = 600 + 2 * OUTER_MARGIN;
g.setColor(BACKGROUND_COLOR);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(BAR_COLOR);
final int barWidth = 20;
for (int itemIndex = 0; itemIndex < inputData.length; itemIndex++) {
final int x = OUTER_MARGIN + 25 * itemIndex;
final int barHeight = 10 * inputData[itemIndex];
final int y = barHeight;
g.fillRect(x, y, barWidth, barHeight);
}
}
public static void main(String[] args)
{
new SimpleBarchart;
}