/* * ImageSelector.java */ import java.awt.*; /* Image Selector Object */ public class ImageSelector extends ImageCanvas { int type; /* VERTICAL/HORIZONTAL Type */ int pos; /* Current Position */ int value; /* Current Value */ int minimum; /* Minimum Value */ int maximum; /* Maximum Value */ Color color = gray; /* Line Color */ boolean visible = false; /* Is Line Visible */ /** Constructor */ public ImageSelector(int _type, int _value, int _minimum, int _maximum) { type = _type; minimum = _minimum; maximum = _maximum; setValue(_value, false); } /** Set Line Color */ public void setColor(Color _color) { if ( _color == color ) return; color = _color; drawPos(); } /** Drawing Action Here */ public void update(Graphics g) { super.update(g); drawPos(g); } /** Get Current Window Length */ private int getSelectHeight() { return ((type == Scrollbar.VERTICAL) ? size().height : size().width)-d2-1; } /** Draw Current Line */ private synchronized void drawPos() { if ( !visible ) return; Graphics g = getGraphics(); if ( g == null ) return; drawPos(g); g.dispose(); } /** Draw Current Line */ private void drawPos(Graphics g) { if ( (g == null) || !visible ) return; g.setColor(color); if ( type == Scrollbar.VERTICAL ) g.drawLine(depth, pos, size().width-depth-1, pos); else g.drawLine(pos, depth, pos, size().height-depth-1); } /** Translate Value to Window Position */ private int Value2Pos(int _value) { return (_value-minimum)* getSelectHeight()/(maximum-minimum)+depth; } /** Translate Window Position to Value */ private int Pos2Value(int _pos) { int h = getSelectHeight(); if ( (_pos-=depth) < 0 ) _pos = 0; if ( _pos > h ) _pos = h; return _pos*(maximum-minimum)/h+minimum; } /** Get Current Value */ public int getValue() { return value;} /** Set Current Value */ public synchronized boolean setValue(int _value, boolean repaint) { int oldval = value; int oldpos = pos; if ( _value < minimum ) _value = minimum; if ( _value >= maximum ) _value = maximum-1; pos = Value2Pos(value = _value); if ( (pos != oldpos) && repaint ) repaint(); return (value != oldval); } /** Reflect Current Mouse Position */ private boolean setMouse(int x, int y) { return setValue(Pos2Value( (type == Scrollbar.VERTICAL) ? y : x), true); } /** Update Current Image */ public void updateImage(Image _img) { visible = false; super.updateImage(_img); } /** Image is Prepare Complete */ public void imageComplete() { needTrack = false; setValue(value, false); visible = true; repaint(); } /** Mouse Down Event */ public boolean mouseDown(Event ev, int x, int y) { int npos = (type == Scrollbar.VERTICAL) ? y : x; if ( (pos-4 < npos) && (npos < pos+4) ) { setValue(Pos2Value(npos), true); Notify(ev, this); mousePressed = true; } else { ev.id = Event.KEY_PRESS; Notify(ev, this); } return true; } /** Mouse Up Event */ public boolean mouseUp(Event ev, int x, int y) { if ( mousePressed ) { mousePressed = false; setMouse(x, y); Notify(ev, this); } return true; } /** Delay Mouse Drag Event */ public boolean delayDrag(Event ev, int x, int y) { if ( mousePressed ) { setMouse(x, y); ev.id = Event.MOUSE_DRAG; Notify(ev, this); } return true; } }