home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-09-06 | 8.7 KB | 332 lines |
- /*
- * JmScale.java
- *
- * JmScale provides a slider bar with numeric output display on
- * top of the thumb and a label below it.
- */
-
- package samples.conversion;
-
- import java.awt.*;
- import java.applet.*;
- import java.awt.event.*;
- import java.util.Vector;
-
- public class JmScale extends Canvas
- {
- private int width = 120;
- private int height = 48;
-
- private final int BUFFER = 2;
- private final int THUMB_SIZE = 10;
-
- private final int TEXT_HEIGHT = 16;
- private final int TEXT_BUFFER = 3;
-
- private int minWidth;
- private int minHeight;
-
- private int barHeight;
- private int titlePosX = 0;
- private int titlePosY = 0;
-
- private int min, max, value, point;
- private int minPoint, maxPoint;
- private int decPt;
-
- private Color backgroundColor;
- private Color thumbColor;
- private Color barColor;
- private Color slashColor;
- private Color textColor;
- private Font font;
- private String title;
-
- private boolean holdThumb = false;
- private Image image;
- private Graphics imageGC;
- private Vector listeners = new Vector();
-
- public JmScale( String s, int min, int max, int x, int dec, int w, int h )
- {
- title = new String( s );
- width = w;
- height = h;
- font = new Font( "TimesRoman", Font.PLAIN, 12 );
- backgroundColor = Color.lightGray;
- thumbColor = Color.lightGray;
- barColor = Color.lightGray;
- slashColor = Color.black;
- textColor = Color.black;
- this.min = min;
- this.max = max;
- decPt = dec;
- setValue( x );
- setSize( width, height );
- addMouseListener( new MouseAdapter() );
- addMouseMotionListener( new MouseAdapter() );
- }
-
- public void addNotify()
- {
- super.addNotify();
- image = createImage( width, height );
- imageGC = image.getGraphics();
- }
-
- public void setValue( int num )
- {
- /*
- * This method determines the value of point.
- * point is where the thumb slider is drawn.
- */
-
- value = num;
-
- if( value < min )
- value = min;
- else if( value > max )
- value = max;
-
- if( value != min )
- point = (int) Math.round( ( (double)( value - min ) /
- (double)( max - min )
- ) * (double)( maxPoint - minPoint )
- ) + minPoint;
- else
- point = minPoint;
- repaint();
- }
-
- public int getValue()
- {
- return value;
- }
-
- public void paint( Graphics gc )
- {
- /*
- * Clear the whole slider.
- */
-
- imageGC.setColor( backgroundColor );
- imageGC.fillRect( 0, 0, width, height );
-
- /*
- * Draw the bar.
- */
-
- imageGC.setColor( barColor );
- imageGC.fill3DRect( 0, TEXT_HEIGHT, width, barHeight, false );
-
- /*
- * Draw the thumb.
- */
-
- imageGC.setColor( thumbColor );
- imageGC.fill3DRect( point - THUMB_SIZE, TEXT_HEIGHT + BUFFER,
- THUMB_SIZE * 2 + 1, barHeight - 2 * BUFFER,
- true );
-
- imageGC.setColor( slashColor );
- imageGC.drawLine( point, TEXT_HEIGHT + BUFFER,
- point, TEXT_HEIGHT + barHeight - 2 * BUFFER );
-
- /*
- * Draw the value. Check for decimal places.
- */
-
- imageGC.setColor( textColor );
- imageGC.setFont( font );
-
- String str;
- if( decPt > 0 )
- {
- double fpvalue = (double) value / Math.pow( 10.0, (double) decPt );
- str = String.valueOf( fpvalue );
- if( str.indexOf( '.' ) == -1 )
- str += ".0";
- }
- else
- str = String.valueOf( value );
-
- imageGC.drawString( str,
- point - (int)( getFontMetrics( font ).stringWidth( str ) / 2 ),
- TEXT_HEIGHT - TEXT_BUFFER );
-
- /*
- * Draw the title.
- */
-
- imageGC.drawString( title, titlePosX, titlePosY );
-
- gc.drawImage( image, 0, 0, this );
- }
-
- public void update( Graphics g )
- {
- paint( g );
- }
-
- public void setSize( int w, int h )
- {
- /*
- * This method resizes the whole component.
- * It also computes for the variables needed
- * for calculating the display.
- */
-
- width = w;
- height = h;
- super.setSize( width, height );
-
- minWidth = 2 * (THUMB_SIZE + BUFFER) + 1;
-
- minHeight = 2 * (BUFFER + 1) + 2 * TEXT_HEIGHT;
- barHeight = height - 2 * TEXT_HEIGHT;
-
- titlePosX = ( width / 2 ) -
- (int)( getFontMetrics( font ).stringWidth( title ) / 2 );
- titlePosY = TEXT_HEIGHT + barHeight - TEXT_BUFFER +
- getFontMetrics( font ).getHeight();
-
- if( width < minWidth )
- width = minWidth;
- if( height < minHeight )
- height = minHeight;
-
- minPoint = THUMB_SIZE + BUFFER;
- maxPoint = width - THUMB_SIZE - BUFFER - 1;
-
- setValue( value );
- }
-
- public synchronized void addActionListener( ActionListener l )
- {
- listeners.addElement( l );
- }
-
- public synchronized void removeActionListener( ActionListener l )
- {
- listeners.removeElement( l );
- }
-
- public void postAction()
- {
- /*
- * This method gets called when an event occurs
- * that this object is interested in and will
- * propagate the event to all of its listeners.
- */
-
- Vector targets;
- synchronized( this )
- {
- targets = (Vector) listeners.clone();
- }
-
- ActionEvent e = new ActionEvent( this, 0, title );
-
- for( int i = 0; i < targets.size(); i++ )
- {
- ActionListener target = (ActionListener) targets.elementAt( i );
- target.actionPerformed( e );
- }
- }
-
- class MouseAdapter implements MouseListener, MouseMotionListener
- {
- /*
- * inner class MouseAdapter handles the mouse events
- * for class JmScale.
- */
-
- Cursor prevCursor = getCursor();
- Cursor handCursor = new Cursor( Cursor.HAND_CURSOR );
-
- public void mouseClicked( MouseEvent e ){}
-
- public void mouseEntered( MouseEvent e )
- {
- prevCursor = getCursor();
- setCursor( handCursor );
- }
-
- public void mouseExited( MouseEvent e )
- {
- setCursor( prevCursor );
- }
-
- public void mousePressed( MouseEvent e )
- {
- int x = e.getX();
- int y = e.getY();
-
- if( ( y >= TEXT_HEIGHT ) &&
- ( y <= barHeight + TEXT_HEIGHT ) &&
- ( x < ( point + THUMB_SIZE ) ) &&
- ( x > ( point - THUMB_SIZE ) ) )
- holdThumb = true;
- }
-
- public void mouseReleased( MouseEvent e )
- {
- /*
- * Set the value on the mouse click.
- * And call postEvent() to pass event out.
- */
-
- int x = e.getX();
- int y = e.getY();
-
- if( holdThumb )
- {
- setValue( value );
- postAction();
- }
- else if( ( y >= TEXT_HEIGHT ) && ( y <= barHeight + TEXT_HEIGHT ) )
- {
- if( x > point )
- setValue( value + 1 );
- else if ( x < point )
- setValue( value - 1 );
- postAction();
- }
-
- holdThumb = false;
- }
-
- public void mouseMoved( MouseEvent e ){}
-
- public void mouseDragged( MouseEvent e )
- {
- /*
- * When thumb is being dragged,
- * compute for value and repaint.
- */
-
- int x = e.getX();
- int y = e.getY();
-
- if( ( y >= TEXT_HEIGHT ) &&
- ( y <= barHeight + TEXT_HEIGHT ) )
- {
- double percent;
- point = Math.max( x, minPoint );
- point = Math.min( point, maxPoint );
-
- if( point != minPoint )
- percent = (double)( point - minPoint ) /
- (double)( maxPoint - minPoint );
- else
- percent = 0;
-
- value = (int) Math.round( percent *
- (double)( max - min ) ) +
- min;
- repaint();
- }
- }
- }
- }
-