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.old < prev    next >
Encoding:
Text File  |  1996-06-18  |  12.6 KB  |  488 lines

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