home *** CD-ROM | disk | FTP | other *** search
/ The Best of Windows 95.com 1996 September / WIN95_09961.iso / java / mojo1-2e.exe / MOJODISK / DATA.4 / plugins / gauge.def < prev    next >
Encoding:
Text File  |  1996-07-01  |  14.2 KB  |  593 lines

  1. DEF_COMPONENTNAME
  2. Gauge
  3. DEF_SUPERCLASS
  4. Panel
  5. DEF_SUPERCOMPONENT
  6.  
  7. DEF_PACKAGE
  8. plugins
  9. widgets
  10. DEF_ENDLIST
  11. DEF_SUBCOMPONENTLIST
  12. DEF_ENDLIST
  13. DEF_SUBCOMPONENTCLASSLIST
  14. DEF_ENDLIST
  15. DEF_CATEGORY
  16. Widgets
  17. DEF_BITMAP
  18. gaugem.bmp
  19. DEF_THUMBNAIL_UP
  20. gauge.bmp
  21. DEF_THUMBNAIL_DOWN
  22. 2-gauge.bmp
  23. DEF_VISUAL
  24. DEF_TOOL
  25. DEF_IMPORTS
  26. DEF_ENDLIST
  27. DEF_REQUIRES
  28. DEF_ENDLIST
  29. DEF_IMPLEMENTS
  30. DEF_ENDLIST
  31. DEF_DECLARATION
  32. // A class that produces a Progress Gauge.
  33. // @author      Grant R. Gainey
  34. // @version     1.0 03-JAN-1996
  35. //  Variable Declarations
  36.         // Publically settable:
  37.     private int         minVal, maxVal, currVal;
  38.     private double         warnPercent, critPercent;
  39.     private String         legend;
  40.     private String         units;
  41.  
  42.     // Still internal-only:
  43.     private Color         warnColor, critColor, internalColor, normalColor;
  44.     private int        separation = 5;
  45.     private int         margin = separation;
  46.     private double         minAngleRads = Math.PI;    // 180 degrees
  47.     private double         maxAngleRads = 0.0;    // zero degrees
  48.     private int        penRadius = 1;
  49.         private boolean         draw3D;
  50.     private int        tickSize = 8;
  51.     private int        bigTick = 10;
  52.     private int        littleTick = 5;
  53.  
  54.     Gauge myGauge;
  55.         int   min;
  56.         int   max;
  57.  
  58.         Color bgColor;
  59.         Color fgColor;
  60.  
  61.     /*
  62.      * Computed attributes (derived from Base or elsewhere)
  63.      */
  64.     private int        warnVal, critVal;
  65.         private String          maxStr, minStr;
  66.     private Dimension     minSize, maxSize, legendSize, unitsSize;
  67.     private Point         pivotLoc;
  68.     private int           pointerLen;
  69.     private int         minAngleDegs, maxAngleDegs;
  70.     private Rectangle     scaleRect;
  71.     private FontMetrics     myMetrics = null;
  72.     private int        halfTick = (int)Math.round(tickSize/2.0);
  73. DEF_ENDLIST
  74. DEF_METHOD
  75.     private double d2r (int degs) {
  76.     // Degrees-to-Radians
  77.         return Math.round((degs/180)*Math.PI);
  78.     };
  79. DEF_ENDLIST
  80. DEF_METHOD
  81.     private int r2d (double rads) {
  82.     // Radians-to-Degrees
  83.         return (int)Math.round((rads/Math.PI)*180);
  84.     };
  85. DEF_ENDLIST
  86. DEF_METHOD
  87.     private void setStringMetrics() {
  88.     // Based on current font, determine dimensions of all strings.
  89.     // Reset PointerLen here.
  90.         if (myMetrics==null) return;
  91.  
  92.         minSize = new Dimension(myMetrics.stringWidth(minStr),
  93.                     myMetrics.getHeight());
  94.         maxSize = new Dimension(myMetrics.stringWidth(maxStr),
  95.                     myMetrics.getHeight());
  96.         legendSize = new Dimension(myMetrics.stringWidth(legend),
  97.                        myMetrics.getHeight());
  98.         unitsSize = new Dimension(myMetrics.stringWidth(units),
  99.                        myMetrics.getHeight());
  100.         margin += myMetrics.getHeight();
  101.                 pointerLen = (int)Math.round(bounds().width/2.0) - margin - separation;
  102.     };
  103. DEF_ENDLIST
  104. DEF_METHOD
  105.     private Rectangle findScaleRect() {
  106.     // Return rect into which the scale-arc will be drawn
  107.         return new Rectangle(margin, pivotLoc.y - pointerLen, 2*pointerLen, pointerLen);
  108.     };
  109. DEF_ENDLIST
  110. DEF_METHOD
  111.     private void partition() {
  112.     // Determine locations of Pivot and Scale, and len of Pointer
  113.     // Called after any size-change
  114.                 pivotLoc = new Point( bounds().width/2, bounds().height/2 );
  115.                 pointerLen = (int)Math.round(bounds().width/2.0) - margin - separation;
  116.         scaleRect = findScaleRect();
  117.     };
  118. DEF_ENDLIST
  119. DEF_METHOD
  120.     void initialize() {
  121. //                warnPercent = 0.7;
  122. //                critPercent = 0.9;
  123.         warnVal = (int)Math.round(maxVal*warnPercent);
  124.         critVal = (int)Math.round(maxVal*critPercent);
  125.  
  126. //                legend = new String("Velocity");
  127. //                units = new String("KPH");
  128.         minStr = String.valueOf(minVal);
  129.         maxStr = String.valueOf(maxVal);
  130.  
  131.         setStringMetrics();
  132.  
  133.         warnColor = Color.yellow;
  134.         critColor = Color.red;
  135.                 internalColor = fgColor;
  136.  
  137.         partition();
  138.         minAngleDegs = r2d(minAngleRads);
  139.         maxAngleDegs = r2d(maxAngleRads);
  140.  
  141.         setForeground(Color.green);
  142.         normalColor = getForeground();
  143.         setBackground(Color.gray);
  144.         setFont(new Font("Courier", Font.BOLD, 18));
  145.  
  146.                 setBackground(bgColor);
  147.     };
  148. DEF_ENDLIST
  149. DEF_METHOD
  150.     public void setLegend(String newLegend) {
  151.         legend = newLegend;
  152.         if (myMetrics!=null) {
  153.             legendSize = new Dimension(myMetrics.stringWidth(legend),
  154.                               myMetrics.getHeight());
  155.         }
  156.         repaint();
  157.     };
  158. DEF_ENDLIST
  159. DEF_METHOD
  160.     public void setUnits(String newUnits) {
  161.         units = newUnits;
  162.         if (myMetrics!=null) {
  163.             unitsSize = new Dimension(myMetrics.stringWidth(units),
  164.                             myMetrics.getHeight());
  165.         }
  166.         repaint();
  167.     };
  168. DEF_ENDLIST
  169. DEF_METHOD
  170.     public void setMinimum(int min) {
  171.         minVal = min;
  172.         minStr = String.valueOf(minVal);
  173.         if (myMetrics!=null) {
  174.             minSize = new Dimension(myMetrics.stringWidth(minStr),
  175.                         myMetrics.getHeight());
  176.         }
  177.         repaint();
  178.     };
  179. DEF_ENDLIST
  180. DEF_METHOD
  181.     public int getMinimum() {
  182.         return minVal;
  183.     };
  184. DEF_ENDLIST
  185. DEF_METHOD
  186.     public void setMaximum(int max) {
  187.         maxVal = max;
  188.         maxStr = String.valueOf(maxVal);
  189.         if (myMetrics!=null) {
  190.             maxSize = new Dimension(myMetrics.stringWidth(maxStr),
  191.                         myMetrics.getHeight());
  192.         }
  193.         warnVal = (int)Math.round(maxVal*warnPercent);
  194.         critVal = (int)Math.round(maxVal*critPercent);
  195.         repaint();
  196.     };
  197. DEF_ENDLIST
  198. DEF_METHOD
  199.     public int getMaximum() {
  200.         return maxVal;
  201.     };
  202. DEF_ENDLIST
  203. DEF_METHOD
  204.     public void setCurrent(int newCurr) {
  205.         currVal = newCurr;
  206.         repaint();
  207.     };
  208. DEF_ENDLIST
  209. DEF_METHOD
  210.     public int getCurrent() {
  211.         return currVal;
  212.     };
  213. DEF_ENDLIST
  214. DEF_METHOD
  215.     public void setCriticalThreshold(float newCritPcnt) {
  216.         critPercent = newCritPcnt;
  217.         critVal = (int)Math.round(maxVal*critPercent);
  218.         repaint();
  219.     };
  220. DEF_ENDLIST
  221. DEF_METHOD
  222.     public double getCriticalThreshold() {
  223.         return critPercent;
  224.     };
  225. DEF_ENDLIST
  226. DEF_METHOD
  227.     public void update(Graphics g) {
  228.                 Image osImg = createImage(bounds().width, bounds().height);
  229.                 osImg.getGraphics().fillRect(0,0,bounds().width, bounds().height);
  230.         paint(osImg.getGraphics());
  231.         g.drawImage(osImg,0,0,null);
  232.     };
  233. DEF_ENDLIST
  234. DEF_METHOD
  235.     public void drawBackground(Graphics g) {
  236.         g.setColor(getBackground());
  237.  
  238.         if (draw3D) {
  239.             for (int i=0;i<=3;i++) {
  240.                                 g.fill3DRect(i,i,bounds().width-(2*i), bounds().height-(2*i),true);
  241.             }
  242.         } else {
  243.                         g.fillRect(0,0, bounds().width, bounds().height);
  244.         }
  245.     };
  246. DEF_ENDLIST
  247. DEF_METHOD
  248.     public void drawTick(Graphics g, int where, int offset) {
  249.         int arcCenter = pointerLen+separation; 
  250.         Point startPt = mapValToPoint(where, arcCenter-offset);
  251.         Point endPt   = mapValToPoint(where, arcCenter);
  252.         g.setColor(getValColor(where));
  253.         g.drawLine(startPt.x, startPt.y, endPt.x, endPt.y);
  254.     };
  255. DEF_ENDLIST
  256. DEF_METHOD
  257.     public void drawScale(Graphics g) {
  258.  
  259.                 System.out.println(Integer.toString(bounds().height));
  260.         // Draw the meter-interior.  Then draw the Arc itself.  Then, draw tickmarks.
  261.         g.setColor(internalColor);
  262.         g.fillArc(margin, margin, 
  263.                           bounds().width - 2*margin, bounds().height - 2*margin,
  264.               maxAngleDegs, (minAngleDegs-maxAngleDegs));
  265.  
  266.         for (int i=-penRadius;i<=penRadius;i++) {
  267.             // Draw the critical-color segment
  268.             g.setColor(critColor);
  269.             g.drawArc(margin+i, margin+i, 
  270.                                   bounds().width - 2*margin - 2*i, bounds().height - 2*margin - 2*i,
  271.                   maxAngleDegs, 
  272.                   (int)Math.round(minAngleDegs*(1.0-critPercent)));
  273.  
  274.             // Draw the warning-color segment
  275.             g.setColor(warnColor);
  276.             g.drawArc(margin+i, margin+i, 
  277.                                   bounds().width - 2*margin - 2*i, bounds().height - 2*margin - 2*i,
  278.                   (int)Math.round(minAngleDegs*(1.0-critPercent)), 
  279.                   (int)Math.round(minAngleDegs*(critPercent-warnPercent)));
  280.  
  281.             // Draw the normal-color segment
  282.             g.setColor(normalColor);
  283.             g.drawArc(margin+i, margin+i, 
  284.                                   bounds().width - 2*margin - 2*i, bounds().height - 2*margin - 2*i,
  285.                   minAngleDegs, 
  286.                   (int)Math.round(minAngleDegs*(-warnPercent)));
  287.         }
  288.  
  289.         for (int i=minVal;i<=maxVal;i+=littleTick) {drawTick(g,i,halfTick);}
  290.         for (int i=minVal;i<=maxVal;i+=bigTick)    {drawTick(g,i,tickSize);}
  291.     };
  292. DEF_ENDLIST
  293. DEF_METHOD
  294.     public void drawStrings(Graphics g) {
  295.         g.setColor(getForeground());
  296.         g.setFont(getFont());
  297.         g.drawString(minStr, separation, pivotLoc.y + minSize.height);
  298.                 g.drawString(maxStr, location().x+size().width - 2*separation - maxSize.width,
  299.                  pivotLoc.y + minSize.height);
  300.         g.drawString(legend, 
  301.                  pivotLoc.x - (int)Math.round(legendSize.width/2), 
  302.                  pivotLoc.y + legendSize.height + margin);
  303.         g.drawString(units, 
  304.                  pivotLoc.x - (int)Math.round(unitsSize.width/2), 
  305.                  pivotLoc.y + legendSize.height+margin + unitsSize.height+separation);
  306.     };
  307. DEF_ENDLIST
  308. DEF_METHOD
  309.     public Color getValColor(int val) {
  310.         if (val < warnVal) {
  311.             return normalColor;
  312.         } else if (val < critVal) {
  313.             return warnColor;
  314.         } else {
  315.             return critColor;
  316.         }
  317.     };
  318. DEF_ENDLIST
  319. DEF_METHOD
  320.     public Point mapValToPoint(int val) {
  321.         double diffRads = minAngleRads - maxAngleRads;
  322.         double valPcnt  = (double)val/(double)maxVal;
  323.         double valRads  = minAngleRads - diffRads*valPcnt;
  324.         return new Point(pivotLoc.x + (int)Math.round(Math.cos(valRads)*pointerLen), 
  325.                  pivotLoc.y - (int)Math.round(Math.sin(valRads)*pointerLen));
  326.     };
  327. DEF_ENDLIST
  328. DEF_METHOD
  329.     public Point mapValToPoint(int val, int radius) {
  330.         double diffRads = minAngleRads - maxAngleRads;
  331.         double valPcnt  = (double)val/(double)maxVal;
  332.         double valRads  = minAngleRads - diffRads*valPcnt;
  333.         return new Point(pivotLoc.x + (int)Math.round(Math.cos(valRads)*radius), 
  334.                  pivotLoc.y - (int)Math.round(Math.sin(valRads)*radius));
  335.     };
  336. DEF_ENDLIST
  337. DEF_METHOD
  338.     public void drawPointer(Graphics g) {
  339.         g.setColor(getValColor(currVal));
  340.         g.fillOval(pivotLoc.x-5, pivotLoc.y-5, 11, 11);
  341.         Point scalePt = mapValToPoint(currVal);
  342.         for (int i=-penRadius;i<=penRadius;i++) {
  343.             g.drawLine(pivotLoc.x, pivotLoc.y+i, scalePt.x, scalePt.y+i);
  344.         }
  345.     };
  346. DEF_ENDLIST
  347. DEF_METHOD
  348.     public Dimension minimumSize() {
  349.         return new Dimension(100,100);
  350.     };
  351. DEF_ENDLIST
  352. DEF_METHOD
  353.     public Dimension preferredSize() {
  354.         return minimumSize();
  355.     };
  356. DEF_ENDLIST
  357. DEF_METHOD
  358.     public void paint(Graphics g) {
  359.         if (myMetrics==null) {
  360.             myMetrics = getGraphics().getFontMetrics();
  361.             setStringMetrics();
  362.         }
  363.  
  364.                 //Image osImg = createImage(bounds().width, bounds().height);
  365.         //Graphics osGfx = osImg.getGraphics();
  366.         drawBackground(g);
  367.         drawScale(g);
  368.         drawStrings(g);
  369.         drawPointer(g);
  370.  
  371.         //g.drawImage(osImg, 0, 0, this);
  372. //System.out.println(this.toString());
  373.     };
  374. DEF_ENDLIST
  375. DEF_METHOD
  376.     public void repaint() {
  377.         super.repaint();
  378.     };
  379. DEF_ENDLIST
  380. DEF_METHOD
  381.     public void repaint(long when) {
  382.         super.repaint(when);
  383.     };
  384. DEF_ENDLIST
  385. DEF_METHOD
  386.     public void repaint(int x, int y, int w, int h) {
  387.         super.repaint(x,y,w,h);
  388.     };
  389. DEF_ENDLIST
  390. DEF_METHOD
  391.     public void repaint(long when, int x, int y, int w, int h) {
  392.         super.repaint(when,x,y,w,h);
  393.     };
  394. DEF_ENDLIST
  395. DEF_METHOD
  396.     public boolean mouseDown(Event evt, int x, int y) {
  397.         return super.mouseDown(evt,x,y);
  398.     };
  399. DEF_ENDLIST
  400. DEF_METHOD
  401.     public boolean mouseDrag(Event evt, int x, int y) {
  402.         return super.mouseDrag(evt,x,y);
  403.     };
  404. DEF_ENDLIST
  405. DEF_METHOD
  406.     public String toString() {
  407.                 String strBuf = getClass().getName();
  408.         strBuf += "Rect: " +bounds().toString()+ '\n';
  409.         strBuf += "Min : " +minVal+ " Max: " +maxVal+ " Curr: " +currVal+ '\n';
  410.         strBuf += "wP  : " +warnPercent+ " cP:" + critPercent+ '\n';
  411.         strBuf += "wV  : " +warnVal+ " cV:" + critVal+ '\n';
  412.         strBuf += "Lgnd: " +legend+'\n';
  413.         strBuf += "Unit: " +units+'\n';
  414.         strBuf += "MnAR: " +minAngleRads+ " MnAD: " +minAngleDegs+ '\n';
  415.         strBuf += "MxAR: " +maxAngleRads+ " MxAD: " +maxAngleDegs+ '\n';
  416.         strBuf += "PivotLoc: " +pivotLoc.toString()+ '\n';
  417.         strBuf += "PtrLen: " +pointerLen+ '\n';
  418.         strBuf += "SclRect : " +scaleRect.toString()+ '\n';
  419.         return strBuf;
  420.     }
  421. DEF_ENDLIST
  422. DEF_METHOD
  423. public void setWarning(int aParam)
  424. {
  425.   warnPercent = ((double)(aParam))/(100);
  426. }
  427. DEF_ENDLIST
  428. DEF_METHOD
  429. public int getWarning()
  430. {
  431.   return (int)(warnPercent * 100);
  432. }
  433. DEF_ENDLIST
  434. DEF_METHOD
  435. public void setCritical(int aParam)
  436. {
  437.   critPercent = ((double)(aParam))/(100);
  438. }
  439. DEF_ENDLIST
  440. DEF_METHOD
  441. public int getCritical()
  442. {
  443.   return (int)(critPercent*100);
  444. }
  445. DEF_ENDLIST
  446. DEF_METHOD
  447. public String getUnits()
  448. {
  449.   return units;  
  450. }
  451. DEF_ENDLIST
  452. DEF_METHOD
  453. public String getLegend()
  454. {
  455.   return legend;
  456. }
  457. DEF_ENDLIST
  458. DEF_METHOD
  459. public void set3D(boolean aParam)
  460. {
  461.   draw3D = aParam;
  462. }
  463. DEF_ENDLIST
  464. DEF_METHOD
  465. public boolean get3D()
  466. {
  467.   return draw3D;
  468. }
  469. DEF_ENDLIST
  470. DEF_METHOD
  471. public void setFGColor(Color aParam)
  472. {
  473.   fgColor = aParam;
  474. }
  475. DEF_ENDLIST
  476. DEF_METHOD
  477. public Color getFGColor()
  478. {
  479.   return fgColor;
  480. }
  481. DEF_ENDLIST
  482. DEF_METHOD
  483. public void setBGColor(Color aParam)
  484. {
  485.   bgColor = aParam;
  486. }
  487. DEF_ENDLIST
  488. DEF_METHOD
  489. public Color getBGColor()
  490. {
  491.   return bgColor;
  492. }
  493. DEF_ENDLIST
  494. DEF_PROPERTY
  495. Minimum
  496. int
  497. setMinimum(AVALUE);
  498. AVALUE=getMinimum();
  499. 0
  500. DEF_ENDLIST
  501. DEF_PROPERTY
  502. Maximum
  503. int
  504. setMaximum(AVALUE);
  505. AVALUE=getMaximum();
  506. 100
  507. DEF_ENDLIST
  508. DEF_PROPERTY
  509. Current Value
  510. int
  511. setCurrent(AVALUE);
  512. AVALUE=getCurrent();
  513. 0
  514. DEF_ENDLIST
  515. DEF_PROPERTY
  516. Warning Percentage
  517. int
  518. setWarning(AVALUE);
  519. AVALUE=getWarning();
  520. 70
  521. DEF_ENDLIST
  522. DEF_PROPERTY
  523. Critical Percentage
  524. int
  525. setCritical(AVALUE);
  526. AVALUE=getCritical(AVALUE);
  527. 90
  528. DEF_ENDLIST
  529. DEF_PROPERTY
  530. Units
  531. String
  532. setUnits(AVALUE);
  533. AVALUE=getUnits();
  534. MPH
  535. DEF_ENDLIST
  536. DEF_PROPERTY
  537. Legend
  538. String
  539. setLegend(AVALUE);
  540. AVALUE=getLegend();
  541. Velocity
  542. DEF_ENDLIST
  543. DEF_PROPERTY
  544. Top
  545. int
  546. move(bounds().x, AVALUE);
  547. AVALUE = bounds().y;
  548. 0
  549. DEF_ENDLIST
  550. DEF_PROPERTY
  551. Draw 3D
  552. boolean
  553. set3D(AVALUE);
  554. AVALUE=get3d();
  555. true
  556. DEF_ENDLIST
  557. DEF_PROPERTY
  558. Internal Color
  559. Color
  560. setFGColor(AVALUE);
  561. AVALUE=getFGColor();
  562. Color.black
  563. DEF_ENDLIST
  564. DEF_PROPERTY
  565. Background Color
  566. Color
  567. setBGColor(AVALUE);
  568. AVALUE=getBGColor();
  569. Color.white
  570. DEF_ENDLIST
  571. DEF_PROPERTY
  572. Left
  573. int
  574. move(AVALUE, bounds().y);
  575. AVALUE = bounds().x;
  576. 0
  577. DEF_ENDLIST
  578. DEF_PROPERTY
  579. Height
  580. int
  581. resize(bounds().width, AVALUE);
  582. AVALUE = bounds().height;
  583. 150
  584. DEF_ENDLIST
  585. DEF_PROPERTY
  586. Width
  587. int
  588. resize(AVALUE, bounds().height);
  589. AVALUE = bounds().width;
  590. 150
  591. DEF_ENDLIST
  592. DEF_ENDCOMPONENT
  593.