The following definition both extends
JComponent
and implements MeterInterface
.
Note that the setters required by the MeterInterface
include calls
to the zero-argument repaint
method. You learned in
Segment 712 that Java calls paint
whenever you call
repaint
. Thus, calls to the meter setters cause the meter to be
redrawn.
import java.awt.*; import javax.swing.*; public class Meter extends JComponent implements MeterInterface { String title = "Title to be Supplied"; int minValue, maxValue, value; // Constructor: public Meter (int x, int y) { minValue = x; maxValue = y; value = (y + x) / 2; } // Setters: public void setValue(int v) {value = v; repaint();} public void setTitle(String t) {title = t; repaint();} // Getter to be defined // Paint to be defined }