home *** CD-ROM | disk | FTP | other *** search
- DEF_COMPONENTNAME
- Gauge
- DEF_SUPERCLASS
- Panel
- DEF_SUPERCOMPONENT
-
- DEF_PACKAGE
- plugins
- widgets
- DEF_ENDLIST
- DEF_SUBCOMPONENTLIST
- DEF_ENDLIST
- DEF_SUBCOMPONENTCLASSLIST
- DEF_ENDLIST
- DEF_CATEGORY
- Widgets
- DEF_BITMAP
- gaugem.bmp
- DEF_THUMBNAIL_UP
- gauge.bmp
- DEF_THUMBNAIL_DOWN
- 2-gauge.bmp
- DEF_VISUAL
- DEF_TOOL
- DEF_IMPORTS
- DEF_ENDLIST
- DEF_REQUIRES
- DEF_ENDLIST
- DEF_IMPLEMENTS
- DEF_ENDLIST
- DEF_DECLARATION
- // A class that produces a Progress Gauge.
- // @author Grant R. Gainey
- // @version 1.0 03-JAN-1996
- // Variable Declarations
- // Publically settable:
- private int minVal, maxVal, currVal;
- private double warnPercent, critPercent;
- private String legend;
- private String units;
-
- // Still internal-only:
- private Color warnColor, critColor, internalColor, normalColor;
- private int separation = 5;
- private int margin = separation;
- private double minAngleRads = Math.PI; // 180 degrees
- private double maxAngleRads = 0.0; // zero degrees
- private int penRadius = 1;
- private boolean draw3D;
- private int tickSize = 8;
- private int bigTick = 10;
- private int littleTick = 5;
-
- Gauge myGauge;
- int min;
- int max;
-
- Color bgColor;
- Color fgColor;
-
- /*
- * Computed attributes (derived from Base or elsewhere)
- */
- private int warnVal, critVal;
- private String maxStr, minStr;
- private Dimension minSize, maxSize, legendSize, unitsSize;
- private Point pivotLoc;
- private int pointerLen;
- private int minAngleDegs, maxAngleDegs;
- private Rectangle scaleRect;
- private FontMetrics myMetrics = null;
- private int halfTick = (int)Math.round(tickSize/2.0);
- DEF_ENDLIST
- DEF_METHOD
- private double d2r (int degs) {
- // Degrees-to-Radians
- return Math.round((degs/180)*Math.PI);
- };
- DEF_ENDLIST
- DEF_METHOD
- private int r2d (double rads) {
- // Radians-to-Degrees
- return (int)Math.round((rads/Math.PI)*180);
- };
- DEF_ENDLIST
- DEF_METHOD
- private void setStringMetrics() {
- // Based on current font, determine dimensions of all strings.
- // Reset PointerLen here.
- if (myMetrics==null) return;
-
- minSize = new Dimension(myMetrics.stringWidth(minStr),
- myMetrics.getHeight());
- maxSize = new Dimension(myMetrics.stringWidth(maxStr),
- myMetrics.getHeight());
- legendSize = new Dimension(myMetrics.stringWidth(legend),
- myMetrics.getHeight());
- unitsSize = new Dimension(myMetrics.stringWidth(units),
- myMetrics.getHeight());
- margin += myMetrics.getHeight();
- pointerLen = (int)Math.round(bounds().width/2.0) - margin - separation;
- };
- DEF_ENDLIST
- DEF_METHOD
- private Rectangle findScaleRect() {
- // Return rect into which the scale-arc will be drawn
- return new Rectangle(margin, pivotLoc.y - pointerLen, 2*pointerLen, pointerLen);
- };
- DEF_ENDLIST
- DEF_METHOD
- private void partition() {
- // Determine locations of Pivot and Scale, and len of Pointer
- // Called after any size-change
- pivotLoc = new Point( bounds().width/2, bounds().height/2 );
- pointerLen = (int)Math.round(bounds().width/2.0) - margin - separation;
- scaleRect = findScaleRect();
- };
- DEF_ENDLIST
- DEF_METHOD
- void initialize() {
- // warnPercent = 0.7;
- // critPercent = 0.9;
- warnVal = (int)Math.round(maxVal*warnPercent);
- critVal = (int)Math.round(maxVal*critPercent);
-
- // legend = new String("Velocity");
- // units = new String("KPH");
- minStr = String.valueOf(minVal);
- maxStr = String.valueOf(maxVal);
-
- setStringMetrics();
-
- warnColor = Color.yellow;
- critColor = Color.red;
- internalColor = fgColor;
-
- partition();
- minAngleDegs = r2d(minAngleRads);
- maxAngleDegs = r2d(maxAngleRads);
-
- setForeground(Color.green);
- normalColor = getForeground();
- setBackground(Color.gray);
- setFont(new Font("Courier", Font.BOLD, 18));
-
- setBackground(bgColor);
- };
- DEF_ENDLIST
- DEF_METHOD
- public void setLegend(String newLegend) {
- legend = newLegend;
- if (myMetrics!=null) {
- legendSize = new Dimension(myMetrics.stringWidth(legend),
- myMetrics.getHeight());
- }
- repaint();
- };
- DEF_ENDLIST
- DEF_METHOD
- public void setUnits(String newUnits) {
- units = newUnits;
- if (myMetrics!=null) {
- unitsSize = new Dimension(myMetrics.stringWidth(units),
- myMetrics.getHeight());
- }
- repaint();
- };
- DEF_ENDLIST
- DEF_METHOD
- public void setMinimum(int min) {
- minVal = min;
- minStr = String.valueOf(minVal);
- if (myMetrics!=null) {
- minSize = new Dimension(myMetrics.stringWidth(minStr),
- myMetrics.getHeight());
- }
- repaint();
- };
- DEF_ENDLIST
- DEF_METHOD
- public int getMinimum() {
- return minVal;
- };
- DEF_ENDLIST
- DEF_METHOD
- public void setMaximum(int max) {
- maxVal = max;
- maxStr = String.valueOf(maxVal);
- if (myMetrics!=null) {
- maxSize = new Dimension(myMetrics.stringWidth(maxStr),
- myMetrics.getHeight());
- }
- warnVal = (int)Math.round(maxVal*warnPercent);
- critVal = (int)Math.round(maxVal*critPercent);
- repaint();
- };
- DEF_ENDLIST
- DEF_METHOD
- public int getMaximum() {
- return maxVal;
- };
- DEF_ENDLIST
- DEF_METHOD
- public void setCurrent(int newCurr) {
- currVal = newCurr;
- repaint();
- };
- DEF_ENDLIST
- DEF_METHOD
- public int getCurrent() {
- return currVal;
- };
- DEF_ENDLIST
- DEF_METHOD
- public void setCriticalThreshold(float newCritPcnt) {
- critPercent = newCritPcnt;
- critVal = (int)Math.round(maxVal*critPercent);
- repaint();
- };
- DEF_ENDLIST
- DEF_METHOD
- public double getCriticalThreshold() {
- return critPercent;
- };
- DEF_ENDLIST
- DEF_METHOD
- public void update(Graphics g) {
- Image osImg = createImage(bounds().width, bounds().height);
- osImg.getGraphics().fillRect(0,0,bounds().width, bounds().height);
- paint(osImg.getGraphics());
- g.drawImage(osImg,0,0,null);
- };
- DEF_ENDLIST
- DEF_METHOD
- public void drawBackground(Graphics g) {
- g.setColor(getBackground());
-
- if (draw3D) {
- for (int i=0;i<=3;i++) {
- g.fill3DRect(i,i,bounds().width-(2*i), bounds().height-(2*i),true);
- }
- } else {
- g.fillRect(0,0, bounds().width, bounds().height);
- }
- };
- DEF_ENDLIST
- DEF_METHOD
- public void drawTick(Graphics g, int where, int offset) {
- int arcCenter = pointerLen+separation;
- Point startPt = mapValToPoint(where, arcCenter-offset);
- Point endPt = mapValToPoint(where, arcCenter);
- g.setColor(getValColor(where));
- g.drawLine(startPt.x, startPt.y, endPt.x, endPt.y);
- };
- DEF_ENDLIST
- DEF_METHOD
- public void drawScale(Graphics g) {
-
- System.out.println(Integer.toString(bounds().height));
- // Draw the meter-interior. Then draw the Arc itself. Then, draw tickmarks.
- g.setColor(internalColor);
- g.fillArc(margin, margin,
- bounds().width - 2*margin, bounds().height - 2*margin,
- maxAngleDegs, (minAngleDegs-maxAngleDegs));
-
- for (int i=-penRadius;i<=penRadius;i++) {
- // Draw the critical-color segment
- g.setColor(critColor);
- g.drawArc(margin+i, margin+i,
- bounds().width - 2*margin - 2*i, bounds().height - 2*margin - 2*i,
- maxAngleDegs,
- (int)Math.round(minAngleDegs*(1.0-critPercent)));
-
- // Draw the warning-color segment
- g.setColor(warnColor);
- g.drawArc(margin+i, margin+i,
- bounds().width - 2*margin - 2*i, bounds().height - 2*margin - 2*i,
- (int)Math.round(minAngleDegs*(1.0-critPercent)),
- (int)Math.round(minAngleDegs*(critPercent-warnPercent)));
-
- // Draw the normal-color segment
- g.setColor(normalColor);
- g.drawArc(margin+i, margin+i,
- bounds().width - 2*margin - 2*i, bounds().height - 2*margin - 2*i,
- minAngleDegs,
- (int)Math.round(minAngleDegs*(-warnPercent)));
- }
-
- for (int i=minVal;i<=maxVal;i+=littleTick) {drawTick(g,i,halfTick);}
- for (int i=minVal;i<=maxVal;i+=bigTick) {drawTick(g,i,tickSize);}
- };
- DEF_ENDLIST
- DEF_METHOD
- public void drawStrings(Graphics g) {
- g.setColor(getForeground());
- g.setFont(getFont());
- g.drawString(minStr, separation, pivotLoc.y + minSize.height);
- g.drawString(maxStr, location().x+size().width - 2*separation - maxSize.width,
- pivotLoc.y + minSize.height);
- g.drawString(legend,
- pivotLoc.x - (int)Math.round(legendSize.width/2),
- pivotLoc.y + legendSize.height + margin);
- g.drawString(units,
- pivotLoc.x - (int)Math.round(unitsSize.width/2),
- pivotLoc.y + legendSize.height+margin + unitsSize.height+separation);
- };
- DEF_ENDLIST
- DEF_METHOD
- public Color getValColor(int val) {
- if (val < warnVal) {
- return normalColor;
- } else if (val < critVal) {
- return warnColor;
- } else {
- return critColor;
- }
- };
- DEF_ENDLIST
- DEF_METHOD
- public Point mapValToPoint(int val) {
- double diffRads = minAngleRads - maxAngleRads;
- double valPcnt = (double)val/(double)maxVal;
- double valRads = minAngleRads - diffRads*valPcnt;
- return new Point(pivotLoc.x + (int)Math.round(Math.cos(valRads)*pointerLen),
- pivotLoc.y - (int)Math.round(Math.sin(valRads)*pointerLen));
- };
- DEF_ENDLIST
- DEF_METHOD
- public Point mapValToPoint(int val, int radius) {
- double diffRads = minAngleRads - maxAngleRads;
- double valPcnt = (double)val/(double)maxVal;
- double valRads = minAngleRads - diffRads*valPcnt;
- return new Point(pivotLoc.x + (int)Math.round(Math.cos(valRads)*radius),
- pivotLoc.y - (int)Math.round(Math.sin(valRads)*radius));
- };
- DEF_ENDLIST
- DEF_METHOD
- public void drawPointer(Graphics g) {
- g.setColor(getValColor(currVal));
- g.fillOval(pivotLoc.x-5, pivotLoc.y-5, 11, 11);
- Point scalePt = mapValToPoint(currVal);
- for (int i=-penRadius;i<=penRadius;i++) {
- g.drawLine(pivotLoc.x, pivotLoc.y+i, scalePt.x, scalePt.y+i);
- }
- };
- DEF_ENDLIST
- DEF_METHOD
- public Dimension minimumSize() {
- return new Dimension(100,100);
- };
- DEF_ENDLIST
- DEF_METHOD
- public Dimension preferredSize() {
- return minimumSize();
- };
- DEF_ENDLIST
- DEF_METHOD
- public void paint(Graphics g) {
- if (myMetrics==null) {
- myMetrics = getGraphics().getFontMetrics();
- setStringMetrics();
- }
-
- //Image osImg = createImage(bounds().width, bounds().height);
- //Graphics osGfx = osImg.getGraphics();
- drawBackground(g);
- drawScale(g);
- drawStrings(g);
- drawPointer(g);
-
- //g.drawImage(osImg, 0, 0, this);
- //System.out.println(this.toString());
- };
- DEF_ENDLIST
- DEF_METHOD
- public void repaint() {
- super.repaint();
- };
- DEF_ENDLIST
- DEF_METHOD
- public void repaint(long when) {
- super.repaint(when);
- };
- DEF_ENDLIST
- DEF_METHOD
- public void repaint(int x, int y, int w, int h) {
- super.repaint(x,y,w,h);
- };
- DEF_ENDLIST
- DEF_METHOD
- public void repaint(long when, int x, int y, int w, int h) {
- super.repaint(when,x,y,w,h);
- };
- DEF_ENDLIST
- DEF_METHOD
- public boolean mouseDown(Event evt, int x, int y) {
- return super.mouseDown(evt,x,y);
- };
- DEF_ENDLIST
- DEF_METHOD
- public boolean mouseDrag(Event evt, int x, int y) {
- return super.mouseDrag(evt,x,y);
- };
- DEF_ENDLIST
- DEF_METHOD
- public String toString() {
- String strBuf = getClass().getName();
- strBuf += "Rect: " +bounds().toString()+ '\n';
- strBuf += "Min : " +minVal+ " Max: " +maxVal+ " Curr: " +currVal+ '\n';
- strBuf += "wP : " +warnPercent+ " cP:" + critPercent+ '\n';
- strBuf += "wV : " +warnVal+ " cV:" + critVal+ '\n';
- strBuf += "Lgnd: " +legend+'\n';
- strBuf += "Unit: " +units+'\n';
- strBuf += "MnAR: " +minAngleRads+ " MnAD: " +minAngleDegs+ '\n';
- strBuf += "MxAR: " +maxAngleRads+ " MxAD: " +maxAngleDegs+ '\n';
- strBuf += "PivotLoc: " +pivotLoc.toString()+ '\n';
- strBuf += "PtrLen: " +pointerLen+ '\n';
- strBuf += "SclRect : " +scaleRect.toString()+ '\n';
- return strBuf;
- }
- DEF_ENDLIST
- DEF_METHOD
- public void setWarning(int aParam)
- {
- warnPercent = ((double)(aParam))/(100);
- }
- DEF_ENDLIST
- DEF_METHOD
- public int getWarning()
- {
- return (int)(warnPercent * 100);
- }
- DEF_ENDLIST
- DEF_METHOD
- public void setCritical(int aParam)
- {
- critPercent = ((double)(aParam))/(100);
- }
- DEF_ENDLIST
- DEF_METHOD
- public int getCritical()
- {
- return (int)(critPercent*100);
- }
- DEF_ENDLIST
- DEF_METHOD
- public String getUnits()
- {
- return units;
- }
- DEF_ENDLIST
- DEF_METHOD
- public String getLegend()
- {
- return legend;
- }
- DEF_ENDLIST
- DEF_METHOD
- public void set3D(boolean aParam)
- {
- draw3D = aParam;
- }
- DEF_ENDLIST
- DEF_METHOD
- public boolean get3D()
- {
- return draw3D;
- }
- DEF_ENDLIST
- DEF_METHOD
- public void setFGColor(Color aParam)
- {
- fgColor = aParam;
- }
- DEF_ENDLIST
- DEF_METHOD
- public Color getFGColor()
- {
- return fgColor;
- }
- DEF_ENDLIST
- DEF_METHOD
- public void setBGColor(Color aParam)
- {
- bgColor = aParam;
- }
- DEF_ENDLIST
- DEF_METHOD
- public Color getBGColor()
- {
- return bgColor;
- }
- DEF_ENDLIST
- DEF_PROPERTY
- Minimum
- int
- setMinimum(AVALUE);
- AVALUE=getMinimum();
- 0
- DEF_ENDLIST
- DEF_PROPERTY
- Maximum
- int
- setMaximum(AVALUE);
- AVALUE=getMaximum();
- 100
- DEF_ENDLIST
- DEF_PROPERTY
- Current Value
- int
- setCurrent(AVALUE);
- AVALUE=getCurrent();
- 0
- DEF_ENDLIST
- DEF_PROPERTY
- Warning Percentage
- int
- setWarning(AVALUE);
- AVALUE=getWarning();
- 70
- DEF_ENDLIST
- DEF_PROPERTY
- Critical Percentage
- int
- setCritical(AVALUE);
- AVALUE=getCritical(AVALUE);
- 90
- DEF_ENDLIST
- DEF_PROPERTY
- Units
- String
- setUnits(AVALUE);
- AVALUE=getUnits();
- MPH
- DEF_ENDLIST
- DEF_PROPERTY
- Legend
- String
- setLegend(AVALUE);
- AVALUE=getLegend();
- Velocity
- DEF_ENDLIST
- DEF_PROPERTY
- Top
- int
- move(bounds().x, AVALUE);
- AVALUE = bounds().y;
- 0
- DEF_ENDLIST
- DEF_PROPERTY
- Draw 3D
- boolean
- set3D(AVALUE);
- AVALUE=get3d();
- true
- DEF_ENDLIST
- DEF_PROPERTY
- Internal Color
- Color
- setFGColor(AVALUE);
- AVALUE=getFGColor();
- Color.black
- DEF_ENDLIST
- DEF_PROPERTY
- Background Color
- Color
- setBGColor(AVALUE);
- AVALUE=getBGColor();
- Color.white
- DEF_ENDLIST
- DEF_PROPERTY
- Left
- int
- move(AVALUE, bounds().y);
- AVALUE = bounds().x;
- 0
- DEF_ENDLIST
- DEF_PROPERTY
- Height
- int
- resize(bounds().width, AVALUE);
- AVALUE = bounds().height;
- 150
- DEF_ENDLIST
- DEF_PROPERTY
- Width
- int
- resize(AVALUE, bounds().height);
- AVALUE = bounds().width;
- 150
- DEF_ENDLIST
- DEF_ENDCOMPONENT
-