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
-
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.
-
draw(Graphics)
- Same as
paint()
.
-
getX()
- Get the left most position of the panel.
-
getY()
- Get the top most position of the panel.
-
incValueX(int)
- Increment the Y value of vertical bar specified by the parameter.
-
init()
- Initialize the histogram, removing all bars from the graph.
-
paint(Graphics)
- Method to draw objects on the histogram panel.
-
setTitle(String)
- Set a title for the histogram.
-
setValueXY(int, int)
- Set the Y value for the vertical bar at the specified X position.
-
setXLabel(String)
- Set a title for the x-axis.
-
setXMax(int)
- Set the maximum value for the x-axis of the histogram.
-
setXMin(int)
- Set the minimum value for the x-axis of the histogram.
-
setXStep(int)
- Set the incremental step for the x-axis of the histogram.
-
setYLabel(String)
- Set a title for the y-axis of the histogram.
-
setYMax(int)
- Set the maximum value for the y-axis of the histogram.
-
setYMin(int)
- Set the minimum value for the y-axis of the histogram.
-
setYStep(int)
- Set the incremental step for the y-axis.
-
update(Graphics)
- This method is invoked when the
repaint()
method is called.
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
.
init
public void init()
- Initialize the histogram, removing all bars from the graph.
getX
public int getX()
- Get the left most position of the panel.
- Returns:
- The x-coordinate of the top-left position of the histogram.
getY
public int getY()
- Get the top most position of the panel.
- Returns:
- The y-coordinate of the top-left position of the histogram.
setTitle
public void setTitle(String title)
- Set a title for the histogram.
- Parameters:
- title - The new title of the histogram.
setXLabel
public void setXLabel(String xLabel)
- Set a title for the x-axis.
- Parameters:
- xLabel - new title for the x-axis of the histogram.
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.
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.
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.
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.
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.
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
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.
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.
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.
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
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
paint
public void paint(Graphics g)
- Method to draw objects on the histogram panel.
- Overrides:
- paint in class Container