I\'m trying to make a custom swing control that is a meter. Swing Meter http://dl.dropbox.com/u/2363305/Programming/Java/swing_meter.gif
The arrow will move up and d
You could use a JSlider and use setValue(int n)
to set the value whenever you need. You can also change the default appearance, so you get an arrow and a gradient as you want.
In addition to @Jonas' example, you might like to look at the article How to Write a Custom Swing Component.
Addendum: On reflection, it looks a little intimidating, but you can extend BasicSliderUI
and reuse some of your code in paintThumb()
and paintTrack()
.
JSlider slider = new JSlider();
slider.setUI(new MySliderUI(slider));
...
private static class MySliderUI extends BasicSliderUI {
public MySliderUI(JSlider b) {
super(b);
}
@Override
public void paintTrack(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Rectangle r = trackRect;
g2d.setPaint(new GradientPaint(
r.x, r.y, Color.red, r.x + r.width, r.y + r.height, Color.blue));
g.fillRect(r.x, r.y, r.width, r.height);
}
@Override
public void paintThumb(Graphics g) {
super.paintThumb(g); // replace with your fill()
}
}