//  Scott DeRuiter     4/10/2003
//  Gridit.java
//  Using a grid layout, set up some panels that do different things.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class Gridit extends JApplet 
{

	private ImagePanel panel1;
	private BackPanel panel2;
	private ClickPanel panel3;
	private DrawPanel panel4;
 
	public void init ( )  
    	{
		Container contentPane = getContentPane();
        	contentPane.setLayout ( new GridLayout ( 2, 2, 5, 5 ) );
		contentPane.setBackground ( Color.black );
   
		panel1 = new ImagePanel ( );
      		panel1.setBackground( Color.blue );
        	contentPane.add ( panel1 );

		panel2 = new BackPanel ( );
      		panel2.setBackground( Color.red );
        	contentPane.add ( panel2 );

		panel3 = new ClickPanel ( );
      		panel3.setBackground( Color.darkGray );
        	contentPane.add ( panel3 );

		panel4 = new DrawPanel ( );
      		panel4.setBackground( Color.lightGray );
        	contentPane.add ( panel4 );
	}
 
	class ImagePanel extends JPanel implements MouseListener 
    	{
		private Image [] image;
		private int value;

		public ImagePanel ( )   
        	{
			value = 0;
			image = new Image[4];		
			for ( int i = 0; i < image.length; i++ )   
            		{
				image[i] = getImage ( getDocumentBase ( ), "pic" + i + ".jpg" );
				WaitForImage ( this, image[i] );
			}
			addMouseListener(this);
		}

		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 void paintComponent ( Graphics g ) 
        	{
			super.paintComponent ( g );
			g.drawImage ( image[value], 0, 0, getSize().width, getSize().height, this );
		}

		public void mousePressed ( MouseEvent e )   
        	{
			value++;
			if ( value == 4 )
				value = 0;
			repaint ( );
		}

		public void mouseReleased ( MouseEvent e )    {}
		public void mouseClicked ( MouseEvent e )    {}
		public void mouseEntered ( MouseEvent e )    {}
		public void mouseExited ( MouseEvent e )    {}
	}
 
	class BackPanel extends JPanel implements MouseListener 
    	{
		private int number;

		public BackPanel ( )   
        	{
			number = 0;
			addMouseListener(this);
		}

		public void paintComponent ( Graphics g ) 
        	{
			switch ( number % 4 )   
            		{
				case 0:  setBackground ( Color.red );      break;
				case 1:  setBackground ( Color.blue );     break;
				case 2:  setBackground ( Color.green );    break;
				case 3:  setBackground ( Color.gray );     break;
			}
			super.paintComponent ( g );
		}

		public void mousePressed ( MouseEvent e )   
        	{
			number++;
			repaint ( );
		}

		public void mouseReleased ( MouseEvent e )    {}
		public void mouseClicked ( MouseEvent e )    {}
		public void mouseEntered ( MouseEvent e )    {}
		public void mouseExited ( MouseEvent e )    {}
	}
 
	class ClickPanel extends JPanel implements MouseListener 
    	{
		private int x, y;

		public ClickPanel ( )   
        	{
			x = y = 0;
			addMouseListener(this);
		}

		public void paintComponent ( Graphics g ) 
        	{
			super.paintComponent ( g );
		}

		public void PlaceCircle ( Graphics g )   
        	{
			g.setColor ( Color.red );
			g.fillOval ( x - 15, y - 15, 30, 30 );
			g.setColor ( Color.black );
			g.drawOval ( x - 15, y - 15, 30, 30 );
		}

		public void mousePressed ( MouseEvent e )   
        	{
			x = e.getX ( );
			y = e.getY ( );
			Graphics gr = getGraphics ( );
			PlaceCircle ( gr );
		}

		public void mouseReleased ( MouseEvent e )    {}
		public void mouseClicked ( MouseEvent e )    {}
		public void mouseEntered ( MouseEvent e )    {}
		public void mouseExited ( MouseEvent e )    {}
	}
 
	class DrawPanel extends JPanel implements MouseListener, MouseMotionListener 
    	{
		private int x, y, oldx, oldy;

		public DrawPanel ( )   
        	{
			x = y = oldx = oldy = 0;
			addMouseListener(this);
			addMouseMotionListener(this);
		}

		public void paintComponent ( Graphics g ) 
        	{
			super.paintComponent ( g );
		}

		public void DrawIt ( Graphics g )   
        	{
			g.setColor ( Color.black );
			g.drawLine ( x, y, oldx, oldy );
			oldx = x;
			oldy = y;
		}

		public void mousePressed ( MouseEvent e )   
        	{
			x = oldx = e.getX ( );
			y = oldy = e.getY ( );
			Graphics gr = getGraphics ( );
			DrawIt ( gr );
		}

		public void mouseReleased ( MouseEvent e )    {}
		public void mouseClicked ( MouseEvent e )    {}
		public void mouseEntered ( MouseEvent e )    {}
		public void mouseExited ( MouseEvent e )    {}

		public void mouseMoved ( MouseEvent e )    {}

		public void mouseDragged ( MouseEvent e )    
        	{
			x = e.getX ( );
			y = e.getY ( );
			Graphics gr = getGraphics ( );
			DrawIt ( gr );
		}
	}
}

Back to Lesson 29 Examples

Back to Java Main Page