package visHuman.onePointZero; /* * ImageSelector.java */ import java.awt.*; import ImageProcessing.RubberBand; /* 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 */ private RubberBand band; private int width, height, X, Y; Color bandColour = Color.green; boolean banding = false; /** Constructor */ public ImageSelector(int _type, int _value, int _minimum, int _maximum) { type = _type; minimum = _minimum; maximum = _maximum; setValue(_value, false); } public void layout() { setValue(value, true); } /** 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); if (width != 0 && height != 0) { int y1, x1, w, h; g.setColor(bandColour); if (width < 0) { w = java.lang.Math.abs(width); x1 = X; } else { w = width; x1 = X - w; } if (height < 0) { h = java.lang.Math.abs(height); y1 = Y; } else { h = height; y1 = Y - h; } g.drawRect(x1, y1, w, h); } } /** 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() { band = new RubberBand(getImage()); System.gc(); 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; clearBand(); } else { ev.id = Event.KEY_PRESS; Notify(ev, this); banding = true; band.MousePressed(ev); X = band.getX(); Y = band.getY(); width = 0; height = 0; repaint(); } return true; } /** Mouse Up Event */ public boolean mouseUp(Event ev, int x, int y) { if ( mousePressed ) { mousePressed = false; setMouse(x, y); Notify(ev, this); } else if (banding) { band.MouseMoved(ev); width = band.getWidth(); height = band.getHeight(); banding = false; repaint(); } 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); } else if (banding) { band.MouseMoved(ev); width = band.getWidth(); height = band.getHeight(); repaint(); } return true; } public void clearBand() { width = 0; height = 0; X = 0; Y = 0; } public RubberBand getBand() { if (width != 0 && height != 0) { return band; } return null; } }