//  Scott DeRuiter                 4/2/03
//  Flow.java
//  Simple example of a FlowLayout
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class Flow extends JApplet implements ActionListener   
{
 
	private Image image;
	private boolean click;

	public void init ( )  
    	{
		click = false;
		getContentPane().setBackground( Color.darkGray );

		getContentPane().setLayout( new FlowLayout(FlowLayout.LEFT, 300, 50) );

       		JButton pressit = new JButton ( "SHOW PIC" );
       		pressit.addActionListener ( this );  
       		getContentPane().add( pressit );

       		JButton reset = new JButton ( "RESET" );
       		reset.addActionListener ( this );  
       		getContentPane().add( reset );

		image = getImage ( getDocumentBase ( ), "cousins.jpg" );
		WaitForImage ( this, image );
	}

	public void WaitForImage ( JApplet component, Image image )   
    	{
		MediaTracker tracker = new MediaTracker ( component );
		try  
        	{
			tracker.addImage ( image, 0 );
			tracker.waitForID ( 0 );
		}
		catch ( InterruptedException e )   
        	{
			e.printStackTrace ( );   
		}
	}

	public void paint(Graphics g) 
    	{
		if ( !click )
			super.paint ( g );
		else
			g.drawImage ( image, 20, 20, 270, 180, this );
		click = false;
	}

	public void actionPerformed ( ActionEvent evt )   
    	{
     		String command = evt.getActionCommand();
               
      		if ( command.equals ( "SHOW PIC" ) )   
            	{
			click = true;
 	        	repaint ( );
		}
      		else if ( command.equals ( "RESET" ) )
 	        	repaint ( );
	}
}

Back to Lesson 29 Examples

Back to Java Main Page