home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Sample.bin / JmScale.java < prev    next >
Text File  |  1997-09-06  |  9KB  |  332 lines

  1. /*
  2.  *  JmScale.java
  3.  *
  4.  *  JmScale provides a slider bar with numeric output display on
  5.  *  top of the thumb and a label below it.
  6.  */
  7.  
  8. package samples.conversion;
  9.  
  10. import java.awt.*;
  11. import java.applet.*;
  12. import java.awt.event.*;
  13. import java.util.Vector;
  14.  
  15. public class JmScale extends Canvas
  16. {
  17.     private int width = 120;
  18.     private int height = 48;
  19.  
  20.     private final int BUFFER = 2;
  21.     private final int THUMB_SIZE = 10;
  22.  
  23.     private final int TEXT_HEIGHT = 16;
  24.     private final int TEXT_BUFFER = 3;
  25.  
  26.     private int minWidth;
  27.     private int minHeight;
  28.  
  29.     private int barHeight;
  30.     private int titlePosX = 0;
  31.     private int titlePosY = 0;
  32.  
  33.     private int min, max, value, point;
  34.     private int minPoint, maxPoint;
  35.     private int decPt;
  36.  
  37.     private Color backgroundColor;
  38.     private Color thumbColor;
  39.     private Color barColor;
  40.     private Color slashColor;
  41.     private Color textColor;
  42.     private Font font;
  43.     private String title;
  44.  
  45.     private boolean holdThumb = false;
  46.     private Image image;
  47.     private Graphics imageGC;
  48.     private Vector listeners = new Vector();
  49.  
  50.     public JmScale( String s, int min, int max, int x, int dec, int w, int h )
  51.     {
  52.         title = new String( s );
  53.         width = w;
  54.         height = h;
  55.         font = new Font( "TimesRoman", Font.PLAIN, 12 );
  56.         backgroundColor = Color.lightGray;
  57.         thumbColor = Color.lightGray;
  58.         barColor = Color.lightGray;
  59.         slashColor = Color.black;
  60.         textColor = Color.black;
  61.         this.min = min;
  62.         this.max = max;
  63.         decPt = dec;
  64.         setValue( x );
  65.         setSize( width, height );
  66.         addMouseListener( new MouseAdapter() );
  67.         addMouseMotionListener( new MouseAdapter() );
  68.     }
  69.  
  70.     public void addNotify()
  71.     {
  72.         super.addNotify();
  73.         image = createImage( width, height );
  74.         imageGC = image.getGraphics();
  75.     }
  76.  
  77.     public void setValue( int num )
  78.     {
  79.        /*
  80.         *  This method determines the value of point.
  81.         *  point is where the thumb slider is drawn.
  82.         */
  83.  
  84.         value = num;
  85.  
  86.         if( value < min )
  87.             value = min;
  88.         else if( value > max )
  89.             value = max;
  90.  
  91.         if( value != min )
  92.             point = (int) Math.round( ( (double)( value - min ) /
  93.                                         (double)( max - min )
  94.                                       ) * (double)( maxPoint - minPoint )
  95.                                     ) + minPoint;
  96.         else
  97.             point = minPoint;
  98.         repaint();
  99.     }
  100.  
  101.     public int getValue()
  102.     {
  103.         return value;
  104.     }
  105.  
  106.     public void paint( Graphics gc )
  107.     {
  108.        /*
  109.         *  Clear the whole slider.
  110.         */
  111.  
  112.         imageGC.setColor( backgroundColor );
  113.         imageGC.fillRect( 0, 0, width, height );
  114.  
  115.        /*
  116.         *  Draw the bar.
  117.         */
  118.  
  119.         imageGC.setColor( barColor );
  120.         imageGC.fill3DRect( 0, TEXT_HEIGHT, width, barHeight, false );
  121.  
  122.        /*
  123.         *  Draw the thumb.
  124.         */
  125.  
  126.         imageGC.setColor( thumbColor );
  127.         imageGC.fill3DRect( point - THUMB_SIZE, TEXT_HEIGHT + BUFFER,
  128.                      THUMB_SIZE * 2 + 1, barHeight - 2 * BUFFER,
  129.                      true );
  130.  
  131.         imageGC.setColor( slashColor );
  132.         imageGC.drawLine( point, TEXT_HEIGHT + BUFFER,
  133.                            point, TEXT_HEIGHT + barHeight - 2 * BUFFER );
  134.  
  135.        /*
  136.         *  Draw the value.  Check for decimal places.
  137.         */
  138.  
  139.         imageGC.setColor( textColor );
  140.         imageGC.setFont( font );
  141.  
  142.         String str;
  143.         if( decPt > 0 )
  144.         {
  145.             double fpvalue = (double) value / Math.pow( 10.0, (double) decPt );
  146.             str = String.valueOf( fpvalue );
  147.             if( str.indexOf( '.' ) == -1 )
  148.                 str += ".0";
  149.         }
  150.         else
  151.             str = String.valueOf( value );
  152.  
  153.         imageGC.drawString( str,
  154.             point - (int)( getFontMetrics( font ).stringWidth( str ) / 2 ),
  155.             TEXT_HEIGHT - TEXT_BUFFER );
  156.  
  157.        /*
  158.         *  Draw the title.
  159.         */
  160.  
  161.         imageGC.drawString( title, titlePosX, titlePosY );
  162.  
  163.         gc.drawImage( image, 0, 0, this );
  164.     }
  165.  
  166.     public void update( Graphics g )
  167.     {
  168.         paint( g );
  169.     }
  170.  
  171.     public void setSize( int w, int h )
  172.     {
  173.        /*
  174.         *  This method resizes the whole component.
  175.         *  It also computes for the variables needed
  176.         *  for calculating the display.
  177.         */
  178.  
  179.         width = w;
  180.         height = h;
  181.         super.setSize( width, height );
  182.  
  183.         minWidth = 2 * (THUMB_SIZE + BUFFER) + 1;
  184.  
  185.         minHeight = 2 * (BUFFER + 1) + 2 * TEXT_HEIGHT;
  186.         barHeight = height - 2 * TEXT_HEIGHT;
  187.  
  188.         titlePosX = ( width / 2 ) -
  189.             (int)( getFontMetrics( font ).stringWidth( title ) / 2 );
  190.         titlePosY = TEXT_HEIGHT + barHeight - TEXT_BUFFER +
  191.                       getFontMetrics( font ).getHeight();
  192.  
  193.         if( width < minWidth )
  194.             width = minWidth;
  195.         if( height < minHeight )
  196.             height = minHeight;
  197.  
  198.         minPoint = THUMB_SIZE + BUFFER;
  199.         maxPoint = width - THUMB_SIZE - BUFFER - 1;
  200.  
  201.         setValue( value );
  202.     }
  203.  
  204.     public synchronized void addActionListener( ActionListener l )
  205.     {
  206.         listeners.addElement( l );
  207.     }
  208.  
  209.     public synchronized void removeActionListener( ActionListener l )
  210.     {
  211.         listeners.removeElement( l );
  212.     }
  213.  
  214.     public void postAction()
  215.     {
  216.        /*
  217.         *  This method gets called when an event occurs
  218.         *  that this object is interested in and will
  219.         *  propagate the event to all of its listeners.
  220.         */
  221.  
  222.         Vector targets;
  223.         synchronized( this )
  224.         {
  225.             targets = (Vector) listeners.clone();
  226.         }
  227.  
  228.         ActionEvent e = new ActionEvent( this, 0, title );
  229.  
  230.         for( int i = 0; i < targets.size(); i++ )
  231.         {
  232.             ActionListener target = (ActionListener) targets.elementAt( i );
  233.             target.actionPerformed( e );
  234.         }
  235.     }
  236.  
  237.     class MouseAdapter implements MouseListener, MouseMotionListener
  238.     {
  239.        /*
  240.         *  inner class MouseAdapter handles the mouse events
  241.         *  for class JmScale.
  242.         */
  243.  
  244.         Cursor prevCursor = getCursor();
  245.         Cursor handCursor = new Cursor( Cursor.HAND_CURSOR );
  246.  
  247.         public void mouseClicked( MouseEvent e ){}
  248.  
  249.         public void mouseEntered( MouseEvent e )
  250.         {
  251.             prevCursor = getCursor();
  252.             setCursor( handCursor );
  253.         }
  254.  
  255.         public void mouseExited( MouseEvent e )
  256.         {
  257.             setCursor( prevCursor );
  258.         }
  259.  
  260.         public void mousePressed( MouseEvent e )
  261.         {
  262.             int x = e.getX();
  263.             int y = e.getY();
  264.  
  265.             if( ( y >= TEXT_HEIGHT ) &&
  266.                 ( y <= barHeight + TEXT_HEIGHT ) &&
  267.                 ( x < ( point + THUMB_SIZE ) ) &&
  268.                 ( x > ( point - THUMB_SIZE ) ) )
  269.                 holdThumb = true;
  270.         }
  271.  
  272.         public void mouseReleased( MouseEvent e )
  273.         {
  274.            /*
  275.             *  Set the value on the mouse click.
  276.             *  And call postEvent() to pass event out.
  277.             */
  278.  
  279.             int x = e.getX();
  280.             int y = e.getY();
  281.  
  282.             if( holdThumb )
  283.             {
  284.                 setValue( value );
  285.                 postAction();
  286.             }
  287.             else if( ( y >= TEXT_HEIGHT ) && ( y <= barHeight + TEXT_HEIGHT ) )
  288.             {
  289.                 if( x > point )
  290.                     setValue( value + 1 );
  291.                 else if ( x < point )
  292.                     setValue( value - 1 );
  293.                 postAction();
  294.             }
  295.  
  296.             holdThumb = false;
  297.         }
  298.  
  299.         public void mouseMoved( MouseEvent e ){}
  300.  
  301.         public void mouseDragged( MouseEvent e )
  302.         {
  303.            /*
  304.             *  When thumb is being dragged,
  305.             *  compute for value and repaint.
  306.             */
  307.  
  308.             int x = e.getX();
  309.             int y = e.getY();
  310.  
  311.             if( ( y >= TEXT_HEIGHT ) &&
  312.                 ( y <= barHeight + TEXT_HEIGHT ) )
  313.             {
  314.                 double percent;
  315.                 point = Math.max( x, minPoint );
  316.                 point = Math.min( point, maxPoint );
  317.  
  318.                 if( point != minPoint )
  319.                     percent = (double)( point - minPoint ) /
  320.                               (double)( maxPoint - minPoint );
  321.                 else
  322.                     percent = 0;
  323.  
  324.                 value = (int) Math.round( percent *
  325.                                (double)( max - min ) ) +
  326.                                min;
  327.                 repaint();
  328.             }
  329.         }
  330.     }
  331. }
  332.