Java Reference
In-Depth Information
provides a framed area for plotting. It includes a title for the plot, scale values
for the axes, and a horizontal axis label. PlotPanel extends JPanel so it can
be added easily to the layouts of applets and applications. See the Web Course
for a detailed description of the code and a complete listing. Here we give a brief
overview and snippets of the code.
PlotPanel includes four methods that a subclass must override. These meth-
ods set the title and label, they calculate the scaling along the axes, and they draw
a histogram or other type of plot inside the frame. For the axes scale values, we
use a static utility method in the class PlotFormat that converts double values
to strings in decimal or scientific format using the techniques discussed in the
Chapter 5.
The paintComponent() method in PlotPanel draws the frame, title,
label, and scale values and then invokes paintContents() ,which the subclass
must provide:
public void paintComponent (Graphics g) {
// First paint background
setBackground (bgColor);
super.paintComponent (g);
// Get the positions for the titles, labels, etc.
getPositions ();
// Draw the top title
g.setColor (titleColor);
drawText (g, getTitle (), titleX, titleY,
titleWidth, titleHeight, 0,CENTER);
// Draw the bottom label
drawText (g, getXLabel (), horzLabelX, horzLabelY,
horzLabelWidth, horzLabelHeight,0,CENTER);
// Draw the plot frame.
paintFrame (g);
// Draw the plot within in the frame.
// This method must be overriden.
paintContents (g);
} // paintComponent
The HistPanel class extends PlotPanel and displays the contents of the
Histogram class (see below). It provides a paintContents() method that
draws the values of the histogram bins as vertical bars. In the Web Course Te ch
and Physics tracks we use HistPanel in many of the demonstration programs.
 
Search WWH ::




Custom Search