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

public class NullAndCard extends JApplet   
{
	private Left lpanel;
	private Bottom bpanel;
	private FlipCard card;

	public void init() 
	{
		setBackground ( Color.black );
        	getContentPane().setLayout(null);

		lpanel = new Left ( ); 
        	lpanel.setLocation ( 5, 5 ); 
        	lpanel.setSize ( 100, 400 ); 
        	getContentPane().add ( lpanel ); 

		bpanel = new Bottom ( ); 
        	bpanel.setLocation ( 5, 410 ); 
        	bpanel.setSize ( 580, 115 ); 
        	getContentPane().add ( bpanel ); 

		card = new FlipCard ( ); 
       		card.setLocation ( 110, 5 ); 
       		card.setSize ( 475, 400 ); 
       		getContentPane().add ( card ); 
	}

	public Insets getInsets ( ) 
	{
		return new Insets ( 5, 5, 5, 5 );
	}

	class Left extends JPanel   
	{

		public Left ( )   
		{
        		setBackground ( Color.darkGray );
		}
	}

	class Bottom extends JPanel
	{

		public Bottom ( )   
		{
        		setBackground ( Color.gray );
		}
	}

	class FlipCard extends JPanel   
	{
		private CardLayout cards;
		private Intropanel ipanel;
		private Gamepanel gpanel;
		private Exitpanel epanel;

		public FlipCard ( )   
		{
			cards = new CardLayout ( );
			this.setLayout ( cards );
        		setBackground ( Color.white );

			ipanel = new Intropanel ( this );
	      		ipanel.setBackground( Color.blue );
	        	this.add ( ipanel, "INTRO" );
	
			gpanel = new Gamepanel ( this );
	      		gpanel.setBackground( Color.red );
	        	this.add ( gpanel, "GAME" );
	
			epanel = new Exitpanel ( this );
	      		epanel.setBackground( Color.green );
	        	this.add ( epanel, "GAME" );
		}

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

		public Insets getInsets ( ) 
		{
			return new Insets ( 5, 5, 5, 5 );
		}
		
		public class Intropanel extends JPanel implements MouseListener   
		{
			private FlipCard flip;	
			private Image image;	

			public Intropanel ( FlipCard f )  
			{
				flip = f;
				addMouseListener(this);
				image = getImage ( getDocumentBase ( ), "intro.gif" );
				WaitForImage ( this, image );
			}
	
			public void paintComponent(Graphics g) 
			{
				super.paintComponent(g);
				boolean b = g.drawImage ( image, 45, 70, this );
			}
	
			public void mousePressed(MouseEvent evt) 
			{
				if ( evt.isShiftDown() )
					cards.first ( flip );
				else if ( evt.isMetaDown() )
					cards.previous ( flip );
				else
					cards.next ( flip );
			}
		
			public void mouseEntered(MouseEvent evt) { }
			public void mouseExited(MouseEvent evt) { }
			public void mouseClicked(MouseEvent evt) { }
			public void mouseReleased(MouseEvent evt) { }
		}
		 
		public class Gamepanel extends JPanel  
		{
			private FlipCard flip;
			public String [] name = { "PRESS", "THE", "BUTTONS", "TO", "MOVE",
				 "ON", "TO", "NEXT", "PANEL" };
			private Buttonpanel [] bpanel;

			public Gamepanel ( FlipCard f )  
			{
				flip = f;
				bpanel = new Buttonpanel [9];
				setLayout ( new GridLayout (3, 3) );
				for ( int i = 0; i < 9; i++ )   {
					bpanel[i] = new Buttonpanel ( this, name[i], i );
			      		bpanel[i].setBackground( Color.red );
			        	this.add ( bpanel[i] );
				}
			}

			public boolean CheckAll ( )   
			{
				for ( int i = 0; i < 9; i++ )
					if ( !bpanel[i].HasBeenPressed ( ) )
						return false;
				return true;
			}

			public class Buttonpanel extends JPanel implements ActionListener   
			{
				Gamepanel game;
				boolean pressed;
				public JButton generic;
				int number;

				public Buttonpanel ( Gamepanel g, String s, int i )   
				{
					number = i;
					pressed = false;
					game = g;
	        			setLayout(null);
	
		       			generic = new JButton ( s );
	       				generic.addActionListener ( this );
	        			generic.setLocation ( 10, 10 ); 
	        			generic.setSize ( 135, 110 ); 
	       				this.add ( generic );
				}

				public boolean HasBeenPressed ( )   
				{
					return pressed;
				}

		   		public void actionPerformed ( ActionEvent evt ) 
				{
	      				String command = evt.getActionCommand();
	               
	      				if ( command.equals ( gpanel.name[number] ) )
					{
						pressed = true;
						generic.setVisible ( false );
						if ( game.CheckAll ( ) )   
						{
							cards.next ( flip );
							for ( int i = 0; i < 9; i++ )   
							{
								gpanel.bpanel[i].pressed = false;
								gpanel.bpanel[i].generic.setVisible ( true );
							}
						}
					}
	   			}
			}
		}

		public class Exitpanel extends JPanel implements KeyListener 
		{
			private FlipCard flip;
			private Image image;	
	
			public Exitpanel ( FlipCard f )  
			{
				flip = f;
				addKeyListener(this);
				image = getImage ( getDocumentBase ( ), "exit.gif" );
				WaitForImage ( this, image );
			}

			public void paintComponent ( Graphics g ) 
			{
				super.paintComponent ( g );
				boolean b = g.drawImage ( image, 45, 70, this );
				this.requestFocus();
			}
	
			public void keyTyped ( KeyEvent e )   {}
		
			public void keyPressed ( KeyEvent e )   
			{
				int value = e.getKeyCode();
				if ( value == KeyEvent.VK_ENTER )
					cards.first ( flip );
			}
			
			public void keyReleased ( KeyEvent e )   {}
		}
	}
}

Back to Lesson 30 Examples

Back to Java Main Page