import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LaunchSimpleFrame extends JApplet implements ActionListener   
{

	SimpleFrame simple;
	JButton launchButton;
   
	public void init ( ) 
    	{
		launchButton = new JButton ( " Launch SimpleFrame " );
		getContentPane().add ( launchButton );
		launchButton.addActionListener ( this );
	}

	public void actionPerformed ( ActionEvent evt )   
    	{
		if ( simple == null ) 
        	{
			Image picture1 = getImage ( getCodeBase(), "boys.jpg" );
			Image picture2 = getImage ( getCodeBase(), "tina.jpg" );
			simple = new SimpleFrame ( picture1, picture2 );
			launchButton.setText ( " Close SimpleFrame " );
			simple.addWindowListener ( new WindowAdapter() 
            		{
				public void windowClosed ( WindowEvent evt ) 
                		{
					launchButton.setText ( " Launch SimpleFrame " );
					launchButton.setEnabled ( true );
					simple = null;
				}
			});
		}
		else   
        	{
			launchButton.setEnabled ( false );
			simple.dispose();
		}
	}  
   
	public void destroy() 
    	{
		if ( simple != null ) 
        	{
			simple.dispose();
			simple = null;
		}
	}
}

class SimpleFrame extends JFrame   
{
 
	private DrawingArea canvas;
	private Image image1, image2;

	public static void main(String[] args) 
    	{
		Image boys = Toolkit.getDefaultToolkit().getImage ( "boys.jpg" );
		Image wife = Toolkit.getDefaultToolkit().getImage ( "tina.jpg" );
		SimpleFrame showim = new SimpleFrame ( boys, wife );
	}

	public SimpleFrame ( Image i, Image i2 )   
    	{
		super ( "My Family" );
		image1 = i;
		WaitForImage ( this, image1 );
		image2 = i2;
		WaitForImage ( this, image2 );
		canvas = new DrawingArea ( );
		setContentPane ( canvas );
		canvas.setBackground( Color.darkGray );

		setSize ( 400, 200 );
		setDefaultCloseOperation ( DISPOSE_ON_CLOSE );      
		setLocation ( 120, 150 );
		show ( );
	}

	public void WaitForImage ( JFrame component, Image image )   
    	{
		MediaTracker tracker = new MediaTracker ( component );
		try  
        	{
			tracker.addImage ( image, 0 );
			tracker.waitForID ( 0 );
		}
		catch ( InterruptedException e )   
        	{
			e.printStackTrace ( );   
		}
	}
 
	class DrawingArea extends JPanel   
    	{
		public DrawingArea ( )   
        	{
			setLayout ( null );
		}

		public void paintComponent ( Graphics g ) 
        	{
			g.drawImage ( image1, 0, 0, 200, 200, this );
			g.drawImage ( image2, 200, 0, 200, 200, this );
		}
	}
}

(Applet "LaunchSimpleFrame" would be displayed here
if Java were available.)

Back to Lesson 33 Examples

Back to Java Main Page