The following version of paint
uses the drawString
method
to write the string assigned to the title
variable,
"To be Supplied"
, with the left side of the T
located at
x=100 and the bottom of the T
located at y=50:
import java.awt.*;
import javax.swing.*;
public class Meter extends JComponent implements MeterInterface {
String title = "Title to Be Supplied";
int minValue, maxValue, value;
public Meter (int x, int y) {
minValue = x;
maxValue = y;
value = (y + x) / 2;
}
public void setValue(int v) {value = v; repaint();}
public void setTitle(String t) {title = t; repaint();}
public int getValueAtCoordinates (int x, int y) {
Dimension d = getSize();
int meterWidth = d.width * 3 / 4;
int xOffset = (d.width - meterWidth) / 2;
float fraction = (float)(x - xOffset) / meterWidth;
return (int)Math.round(fraction * (maxValue - minValue) + minValue);
}
public void paint(Graphics g) {
g.drawString(title, 100, 50);
}
public Dimension getMinimumSize() {return new Dimension(150, 100);}
public Dimension getPreferredSize() {return new Dimension(150, 100);}
}