Class Histogram

java.lang.Object
   |
   +----java.awt.Component
           |
           +----java.awt.Container
                   |
                   +----java.awt.Panel
                           |
                           +----Histogram

public class Histogram
extends Panel
implements DrawingObj
This is one example of the drawing object inheriting java.awt.Panel which can be added to the drawing panel (DrawingPanel).

Since DrawingPanel is also an extension of panel, this Histogram panel has to be added to the drawing panel by using the following statement (otherwise, the layout manager of this class might interprete its dimension as null during the panel initialization and nothing will show up):

	Histogram histogram = new Histogram();
	drawingPanel.setLayout(null);
	drawingPanel.add(histogram);
 	...
	histogram.setTitle("title");
	histogram.setXLabel("x-axis label");
	histogram.setYLabel("y-axis label");
	histogram.setYMax(maxY);
	histogram.setXMax(maxX);
 	histogram.setYStep(10);
	histogram.setXStep(5);
	histogram.reshape(x, y, width, height);
 
drawingPanel is an instance of the class object DrawingPanel, which can be typically obtained from the instance of AlgAnimFrame, by calling the getDrawingPanel() method of the class. e.g. in AlgThread, the instance of AlgAnimFrame is passed in as frame. Therefore,
 	drawingPanel = frame.getDrawingPanel();
 
The reshape method is inherited from a parent class. It is called to move the histogram to position (x, y) and set the dimension of the histogram to width x height.

See Also:
DrawingPanel, AlgAnimFrame, Panel

Constructor Index

 o Histogram()
A constructor to this class which set the background of the panel to white, font to size 12 PLAIN courier, and initializes the title, x-axis label, y-axis label, etc.

Method Index

 o draw(Graphics)
Same as paint().
 o getX()
Get the left most position of the panel.
 o getY()
Get the top most position of the panel.
 o incValueX(int)
Increment the Y value of vertical bar specified by the parameter.
 o init()
Initialize the histogram, removing all bars from the graph.
 o paint(Graphics)
Method to draw objects on the histogram panel.
 o setTitle(String)
Set a title for the histogram.
 o setValueXY(int, int)
Set the Y value for the vertical bar at the specified X position.
 o setXLabel(String)
Set a title for the x-axis.
 o setXMax(int)
Set the maximum value for the x-axis of the histogram.
 o setXMin(int)
Set the minimum value for the x-axis of the histogram.
 o setXStep(int)
Set the incremental step for the x-axis of the histogram.
 o setYLabel(String)
Set a title for the y-axis of the histogram.
 o setYMax(int)
Set the maximum value for the y-axis of the histogram.
 o setYMin(int)
Set the minimum value for the y-axis of the histogram.
 o setYStep(int)
Set the incremental step for the y-axis.
 o update(Graphics)
This method is invoked when the repaint() method is called.

Constructors

 o Histogram
 public Histogram()
A constructor to this class which set the background of the panel to white, font to size 12 PLAIN courier, and initializes the title, x-axis label, y-axis label, etc. Note that although the initial background color is set to white, it can be changed at any time after this contructor is called by using the setBackground method inherited. e.g.
	Histogram histogram = new Histogram();
	histogram.setBackground(Color.lightGray);
 
For detail of what color can be set, refer to java.awt.Color.

Methods

 o init
 public void init()
Initialize the histogram, removing all bars from the graph.

 o getX
 public int getX()
Get the left most position of the panel.

Returns:
The x-coordinate of the top-left position of the histogram.
 o getY
 public int getY()
Get the top most position of the panel.

Returns:
The y-coordinate of the top-left position of the histogram.
 o setTitle
 public void setTitle(String title)
Set a title for the histogram.

Parameters:
title - The new title of the histogram.
 o setXLabel
 public void setXLabel(String xLabel)
Set a title for the x-axis.

Parameters:
xLabel - new title for the x-axis of the histogram.
 o setYLabel
 public void setYLabel(String yLabel)
Set a title for the y-axis of the histogram.

Parameters:
yLabel - new title for the y-axis of the histogram.
 o setYMax
 public void setYMax(int yMax)
Set the maximum value for the y-axis of the histogram.

Parameters:
yMax - The maximum value for the y-axis of the histogram.
 o setYStep
 public void setYStep(int step)
Set the incremental step for the y-axis. For example, if the minimum value of the y-axis is 0, and this incremental step is set to 5, then the value 0, 5, 10, 15, 20, ... will be displayed on the y-axis of the histogram.

Parameters:
step - The incremental step for the y-axis of the histogram.
 o setYMin
 public void setYMin(int yMin)
Set the minimum value for the y-axis of the histogram.

Parameters:
yMin - The new minimum value for the y-axis of the histogram.
 o setXMax
 public void setXMax(int xMax)
Set the maximum value for the x-axis of the histogram.

Parameters:
xMax - The maximum value for the x-axis of the histogram.
 o setXStep
 public void setXStep(int step)
Set the incremental step for the x-axis of the histogram.

Parameters:
step - The new incremental step for the x-axis of the histogram.
See Also:
setYStep
 o setXMin
 public void setXMin(int xMin)
Set the minimum value for the x-axis of the histogram.

Parameters:
xMin - The new minimum value for the x-axis of the histogram.
 o setValueXY
 public void setValueXY(int x,
                        int y)
Set the Y value for the vertical bar at the specified X position. If the x or y value specified in the parameter is larger than the maximum x or y value, the maximum value will be set to the specified value + 1.

Parameters:
x - The vertical bar at X = x

y - The value of the vertical bar specified by the first parameter.
 o incValueX
 public void incValueX(int x)
Increment the Y value of vertical bar specified by the parameter. Initially, all x values are set to zero. If the x-value does not exist in the hashtable governing the histogram, it will be added.

Parameters:
x - Specifies the vertical bar at X = x.
 o update
 public void update(Graphics g)
This method is invoked when the repaint() method is called. The update method is override here to eleminate flashing during the updating of the histogram panel.

Overrides:
update in class Container
 o draw
 public void draw(Graphics g)
Same as paint(). This method just calls the paint() method. It must be defined here to implement the DrawingObj interface.

See Also:
paint
 o paint
 public void paint(Graphics g)
Method to draw objects on the histogram panel.

Overrides:
paint in class Container