Java and Lightweight Components
by David K. Perelman-Hall



Listing One
package dph.awt.lightweight;
public abstract class CommonBase extends java.awt.Component {
    public void addNotify() {
        super.addNotify();
        java.awt.Dimension d = getSize();
        if( d.width == 0  ||  d.height == 0 ) {
            setSize( this.figureMySize( this.getGraphics() ) );
        }
    }
    public void setEnabled( boolean state ) {
        super.setEnabled( state );
        repaint();
    }
    public void update( java.awt.Graphics g ) {
        paint( g );    
    }
    public java.awt.Dimension getMinimumSize() {
        return getPreferredSize();
    }
    public java.awt.Dimension getPreferredSize() {
        if( getSize().width != 0  &&  getSize().height != 0 )
            return getSize();
        else
            return figureMySize( getGraphics() );
    }
    protected abstract java.awt.Dimension figureMySize( java.awt.Graphics g );
}


Listing Two
public Label() {
    this( "", false );
}
public Label( String text ) {
    this( text, false );
}
public Label( String text, boolean showBorder ) {
    super();
    this.text = text;
    this.showBorder = showBorder;


Listing Three
public FlatButton ( java.awt.Image image ) {
    super( "" );
    this.image = image;
    this.setup();
}
private final void setup() {
    if( image != null  &&  (image.getWidth(this)==-1  ||  
                                          image.getHeight(this)==-1) )
        this.ensureImageLoaded( image );
}
private final void ensureImageLoaded( java.awt.Image image ) {
    try {
        java.awt.MediaTracker tracker = new java.awt.MediaTracker( this );
        tracker.addImage( image, 0 );
        tracker.waitForID( 0 );
        Util.assert( tracker.statusID(0, false) == 
                        java.awt.MediaTracker.COMPLETE, "image complete" );
    } catch( InterruptedException ex ) {
        Util.debug( "Image loading interrupted" );
    }
}

Listing Four
public void paint( Graphics g ) {
    super.paint( g );
    this.paintTabPanel( g );
    this.paintTabs( g );
}

Listing Five
public void setSelectedTab( int tabNum ) {
    if( tabNum >= 0  &&  tabNum < tabLabels.length ) {
        this.remove( currentPanel );
        selectedTab = tabNum;
        currentPanel = (DoubleBufferedPanel)cards.get( tabLabels[tabNum] );
        this.add( currentPanel );
        currentPanel.setLocation( 2, 27 );
        currentPanel.setSize( this.getPanelSize() );
    }
    repaint();
}


Listing Six
public void addActionListener( ActionListener al ) {
    actionListener = AWTEventMulticaster.add( actionListener, al );
}
public void removeActionListener( ActionListener al ) {
    actionListener = AWTEventMulticaster.remove( actionListener, al );
}


Listing Seven
public void processMouseEvent( MouseEvent me ) {
    if( isEnabled() ) {
      switch( me.getID() ) {
        case MouseEvent.MOUSE_CLICKED:
            if( actionListener != null ) {
              actionListener.actionPerformed( new ActionEvent (
                this, ActionEvent.ACTION_PERFORMED, this.getActionCommand()
                 ) );
            }
        }
    }
    super.processMouseEvent( me );
}

Listing Eight
public void processMouseEvent( java.awt.event.MouseEvent evt ) {
    if( isEnabled() ) {
      switch( evt.getID() ) {
      case java.awt.event.MouseEvent.MOUSE_ENTERED:
         mouseIsIn = true;
         repaint();
         break;
      case java.awt.event.MouseEvent.MOUSE_EXITED:
         mouseIsIn = false;
         repaint();
         break;
      case java.awt.event.MouseEvent.MOUSE_PRESSED:
         mouseIsDown = true;
         repaint();
         break;
      case java.awt.event.MouseEvent.MOUSE_RELEASED:
         mouseIsDown = false;
         repaint();
         break;
      }
   }


Listing Nine
public void spin() {
    class SpinThread extends Thread {
        SpinThread() {
            super();
            this.setPriority( Thread.MIN_PRIORITY );
        }
        public void run() {
            try {
                if( upPressed )
                    increment();
                if( dnPressed )
                    decrement();
                repaint();
                Thread.sleep( 1250 );
                while( upPressed || dnPressed ) {
                    if( upPressed )
                        increment();
                    if( dnPressed )
                        decrement();
                    repaint();    
                    Thread.sleep( 400 );
                }
            } catch( InterruptedException ex ) {
                // ignore it
            }
        }
    }

 

4


