//package FODpack; import java.awt.*; /** * Holds and displays an integer value between 0-100; * @author Torbjörn Nilsson * @version 1.1 */ public class ValueBar extends Panel { // Panel cv; static final int HEIGHT = 18; static final int WIDTH = 270; static final int BAR_START = 111; static final int BAR_SIZE = WIDTH-115; private int value = 0; private int oldValue = 0; private boolean editable = false; private String label=""; /** * Default constructor. Do not use this one. */ public ValueBar() { super(); resize(WIDTH, HEIGHT); } /** * The contructor you should use! The lines below describes the arguments: * label What the bar represents. * value Initial value(0-100). * edit If it is possible to edit this value by clicking on the bar with the mouse. */ public ValueBar(String label, int value, boolean edit) { super(); editable = edit; this.label = label; setValue(value); // setLayout(new GridLayout(2,1)); // add(new Label(label)); //, Label.LEFT)); resize(WIDTH, HEIGHT); } /* public void update(Graphics g) { //override paint(g); } */ /** * Draw the bar that represents the value stored in this object. */ public void paint(Graphics g){ // resize(WIDTH, HEIGHT); Dimension d = size(); setBackground(Color.white); // g.setColor(Color.black); // g.drawRect(0,0,d.width-1, d.height-1); //contour component g.setColor(Color.darkGray); g.drawLine(0,0,d.width-1,0); g.drawLine(0,0,0,d.height-1); g.setColor(Color.lightGray); g.drawLine(0,d.height-1,d.width-1,d.height-1); g.drawLine(d.width-1,0,d.width-1,d.height-1); // int wid = d.width-113; g.setColor(Color.black); g.drawRect(BAR_START-1,2,BAR_SIZE+1,d.height-5); //contour bar int bar = BAR_SIZE*value/100; g.setColor(Color.red); g.fillRect(BAR_START,3,bar,d.height-6); // bar int oldx = BAR_START+BAR_SIZE*oldValue/100; g.setColor(Color.green); g.drawLine(oldx, 3 , oldx, d.height-4); g.drawLine(oldx+1, 3 , oldx+1, d.height-4); g.setColor(Color.black); g.drawString(label,3,13); // System.out.println("hej"); } /** * If the ValueBar object is editable, then this method will update the internal value when the bar is clicked. */ public boolean mouseDown(Event e, int x, int y) { // System.out.println("pressed! "+x+" "+y); if ( editable && x<=(BAR_START+BAR_SIZE) && x>=BAR_START ) { setValue( (x-BAR_START)*100/BAR_SIZE); repaint(); // System.out.println(value); return true; } else return false; } /** * @return Return the value currently stored in this object. */ public int getValue() { return value; } /** * Set the value of this object. Checks that value is in range 0-100. * If out of range the value is not changed, but error message is printed. */ public void setValue(int x) { oldValue = value; value = x; if(value < 0) value = 0; if(value >100) value = 100; repaint(); } /** * Use the main method to test what this class can do and looks like. */ public static void main(String[] a) { Frame f = new Frame("ValueBar-test"); f.resize(400,300); ValueBar vb = new ValueBar("Developement", 50, false); f.add(vb); vb.move(20,40); vb =new ValueBar("Mana", 75, true); f.add(vb); vb.move(20,60); f.show(); // (f.getGraphics()).drawString("pelle",10,100); } }