Home Segments Top Top Previous Next

722: Mainline

You easily can add a short vertical line such that its position along the horizontal line represents the value of the value variable. For now, the vertical line lies at midpoint of the horizontal line, with no attention paid to the value of the value variable:

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();}
 // Getter to be defined
 public void paint(Graphics g) {
  g.drawLine(0, 50, 100, 50);
  g.drawLine(50, 50, 50, 40);   
 } 
}