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

  1. /*
  2.  *  Hello4.java
  3.  *
  4.  *  class Hello4 is a window application that
  5.  *  reads property.list file and loads a gif image
  6.  *  and shows the loaded gif image.
  7.  *  Note that property.list and the gif image
  8.  *  can be bundled into the executable.
  9.  */
  10.  
  11. package samples.resource;
  12.  
  13. import java.awt.*;
  14. import java.awt.event.*;
  15. import java.awt.image.*;
  16. import java.io.*;
  17. import java.util.*;
  18. import java.net.*;
  19.  
  20. public class Hello4 extends Frame
  21. {
  22.     Image image = null;
  23.     Font font = null;
  24.     FontMetrics fontMetric = null;
  25.     String message = null;
  26.  
  27.     public Hello4()
  28.     {
  29.         super( "Resource Example" );
  30.         setLayout( null );
  31.  
  32.         // center the window
  33.  
  34.         Dimension dim = getToolkit().getScreenSize();
  35.         setBounds( dim.width / 2 - 120, dim.height / 2 - 100, 240, 200 );
  36.  
  37.         enableEvents( AWTEvent.WINDOW_EVENT_MASK );
  38.  
  39.         try
  40.         {
  41.             // read property.list and set the font and the message variables.
  42.  
  43.             InputStream iStream = getClass().getResourceAsStream( "property.list" );
  44.             if( iStream == null )
  45.             {
  46.                 System.out.println( "error: cannot get property.list file" );
  47.                 System.exit( 1 );
  48.             }
  49.             Properties p = new Properties();
  50.             p.load( iStream );
  51.             font = Font.decode( p.getProperty( "Hello.font" ) );
  52.             message = p.getProperty( "Hello.message" );
  53.  
  54.             // also get the gif file name and create image for the gif.
  55.  
  56.             String name = p.getProperty( "Hello.image" );
  57.             URL url = getClass().getResource( name );
  58.             ImageProducer imageProd = (ImageProducer) url.getContent();
  59.             image = createImage( imageProd );
  60.             if( font != null )
  61.                 fontMetric = getToolkit().getFontMetrics( font );
  62.         }
  63.         catch( Exception e )
  64.         {
  65.             System.out.println( e.toString() );
  66.         }
  67.     }
  68.  
  69.     public void paint( Graphics g )
  70.     {
  71.         setBackground( Color.yellow );
  72.         g.setFont( font );
  73.         int x;
  74.         if( image != null )
  75.         {
  76.             x = ( getSize().width - image.getWidth( this ) ) / 2;
  77.             g.drawImage( image, x, 60, this );
  78.         }
  79.         if( message != null )
  80.         {
  81.             x = ( getSize().width - fontMetric.stringWidth( message ) ) / 2;
  82.             g.drawString( message, x, 140 );
  83.         }
  84.     }
  85.  
  86.     public void processWindowEvent( WindowEvent e )
  87.     {
  88.         if( e.getID() == WindowEvent.WINDOW_CLOSING )
  89.         {
  90.             setVisible( false );
  91.             dispose();
  92.             System.exit( 0 );
  93.         }
  94.         super.processWindowEvent( e );
  95.     }
  96.  
  97.     public static void main( String args[] )
  98.     {
  99.         Hello4 winApp = new Hello4();
  100.         winApp.show();
  101.     }
  102. }
  103.  
  104.