/** * ColorBar - one of the three bars in a ColorPanel * * This is strictly a helper class for ColorPanel. */ import java.awt.*; /** ColorBar: A color bar */ final class ColorBar { /* what does one pixel in any direction mean to any color? */ float maxdim = (float)1.0; float mindim = (float)0.0; ColorPanel b; /* the panel in which this widget lives */ float value; /* current color */ int x1; /* endpoints of the bar */ int y1; int xmid; int ymid; int x2; int y2; final int lenx; /* length in x direction */ final int leny; /* length in y direction */ final float sqrlen; /* lenx*lenx + leny*leny */ final float abslen; /* sqrt(lenx*lenx + leny*leny) */ final boolean xbigger; /* abs(lenx) > abs(leny) */ final float xdelta; /* x pixels per y pixel */ final float ydelta; /* y change per x pixel */ final float xfrac; /* 1.0/abs(x2-x1) */ final float yfrac; /* 1.0/abs(y2-y1) */ final float xfrac255; /* 255/abs(x2-x1) */ final float yfrac255; /* 255/abs(y2-y1) */ final int mycolor; /* RED, GREEN, BLUE */ final static int RED = 0x1000000; final static int GREEN = 0x0010000; final static int BLUE = 0x0000100; final private int colormask; /* thickness of the bar -- other endpoint is 0,0 */ final int thickx; final int thicky; /* bar is split into two parts by the handle, offset to not overlap handle */ final int firstx; final int firsty; final int lastx; final int lasty; /** ColorBar: initialize a color bar */ ColorBar(ColorPanel b, /* panel this is drawn on */ int mycolor, /* ColorBar.RED, GREEN, or BLUE */ float value, /* color value, values 0.0 to 1.0 */ int lenx, /* length of bar, x direction */ int leny) /* length of bar, y direction */ { this.sqrlen = (float)(lenx*lenx+leny*leny); this.abslen = (float)Math.sqrt(sqrlen); float avoidhandle = (b.thickness*(float)1.5)/abslen; this.b = b; this.lenx = lenx; this.leny = leny; this.firstx = -(int)(lenx*avoidhandle); this.firsty = -(int)(leny*avoidhandle); this.lastx = -this.firstx; this.lasty = -this.firsty; this.xbigger = (Math.abs(lenx) > Math.abs(leny)); this.thickx = -(int)((leny*b.thickness)/abslen); this.thicky = (int)((lenx*b.thickness)/abslen); this.mycolor = mycolor; this.colormask = mycolor-(mycolor/256); move(value); this.xdelta = (y2==y1) ? (float)0.0 : Math.abs((float)lenx/(float)leny); this.ydelta = (x2==x1) ? (float)0.0 : Math.abs((float)leny/(float)lenx); this.xfrac = (x2==x1) ? (float)0.0 : mycolor/(float)Math.abs(lenx); this.yfrac = (y2==y1) ? (float)0.0 : mycolor/(float)Math.abs(leny); this.xfrac255 = ((float)255.0)*xfrac; this.yfrac255 = ((float)255.0)*yfrac; } /* move the color bar according to value and current pixel */ void move(float value) { this.value = value; b.cc = ((b.cc&(0xffffff^colormask))^ /* change this component of cc */ ((int)(value*colormask)&colormask)); x1 = b.currx+firstx-(int)(lenx*value)-(thickx/2); y1 = b.curry+firsty-(int)(leny*value)-(thicky/2); xmid = x1+(int)(lenx*value); ymid = y1+(int)(leny*value); x2 = x1+lenx+lastx-firstx; y2 = y1+leny+lasty-firsty; } /* clear a color bar */ public void clear(Graphics g) { int i, x, y, xstop, ystop; float fx, fy; int xinc = (x1 1.0) { fy -= 1.0; y += yinc; g.drawLine(x, y, x+thickx, y+thicky); } } } else /* !xbigger */ { for (; y!=ystop; y+=yinc, fx+=xdelta) { g.drawLine(x, y, x+thickx, y+thicky); if (fx > 1.0) { fx -= 1.0; x += xinc; g.drawLine(x, y, x+thickx, y+thicky); } } } if (i==0) { x += lastx-firstx; y += lasty-firsty; xstop = x2; ystop = y2; } } } /* draw a color bar */ public void draw(Graphics g) { int i, x, y, xstop, ystop; int basecolor = (b.cc & (0xffffff ^ colormask)); float fx, fy, amount; int xinc = (x1 1.0) { fy -= 1.0; y += yinc; g.drawLine(x, y, x+thickx, y+thicky); } } } else /* !xbigger */ { for (; y!=ystop; y+=yinc, fx+=xdelta, amount+=yfrac) { int q = basecolor^(((int)amount)&colormask); g.setColor(new Color(q)); g.drawLine(x, y, x+thickx, y+thicky); if (fx > 1.0) { fx -= 1.0; x += xinc; g.drawLine(x, y, x+thickx, y+thicky); } } } if (i==0) { x = x+lastx-firstx; y = y+lasty-firsty; xstop = x2; ystop = y2; } } } }