home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-09-06 | 2.8 KB | 104 lines |
- /*
- * Hello4.java
- *
- * class Hello4 is a window application that
- * reads property.list file and loads a gif image
- * and shows the loaded gif image.
- * Note that property.list and the gif image
- * can be bundled into the executable.
- */
-
- package samples.resource;
-
- import java.awt.*;
- import java.awt.event.*;
- import java.awt.image.*;
- import java.io.*;
- import java.util.*;
- import java.net.*;
-
- public class Hello4 extends Frame
- {
- Image image = null;
- Font font = null;
- FontMetrics fontMetric = null;
- String message = null;
-
- public Hello4()
- {
- super( "Resource Example" );
- setLayout( null );
-
- // center the window
-
- Dimension dim = getToolkit().getScreenSize();
- setBounds( dim.width / 2 - 120, dim.height / 2 - 100, 240, 200 );
-
- enableEvents( AWTEvent.WINDOW_EVENT_MASK );
-
- try
- {
- // read property.list and set the font and the message variables.
-
- InputStream iStream = getClass().getResourceAsStream( "property.list" );
- if( iStream == null )
- {
- System.out.println( "error: cannot get property.list file" );
- System.exit( 1 );
- }
- Properties p = new Properties();
- p.load( iStream );
- font = Font.decode( p.getProperty( "Hello.font" ) );
- message = p.getProperty( "Hello.message" );
-
- // also get the gif file name and create image for the gif.
-
- String name = p.getProperty( "Hello.image" );
- URL url = getClass().getResource( name );
- ImageProducer imageProd = (ImageProducer) url.getContent();
- image = createImage( imageProd );
- if( font != null )
- fontMetric = getToolkit().getFontMetrics( font );
- }
- catch( Exception e )
- {
- System.out.println( e.toString() );
- }
- }
-
- public void paint( Graphics g )
- {
- setBackground( Color.yellow );
- g.setFont( font );
- int x;
- if( image != null )
- {
- x = ( getSize().width - image.getWidth( this ) ) / 2;
- g.drawImage( image, x, 60, this );
- }
- if( message != null )
- {
- x = ( getSize().width - fontMetric.stringWidth( message ) ) / 2;
- g.drawString( message, x, 140 );
- }
- }
-
- public void processWindowEvent( WindowEvent e )
- {
- if( e.getID() == WindowEvent.WINDOW_CLOSING )
- {
- setVisible( false );
- dispose();
- System.exit( 0 );
- }
- super.processWindowEvent( e );
- }
-
- public static void main( String args[] )
- {
- Hello4 winApp = new Hello4();
- winApp.show();
- }
- }
-
-