import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
 
public class MoreCards2 extends JApplet implements MouseListener   
{
	private CardLayout cards;
	private Container c;
	private Viewpanel vpanel;
	private Randompanel rpanel;

	public void init ( )  
	{
		c = this.getContentPane();
		cards = new CardLayout ( );
		c.setLayout ( cards );
		c.setBackground( Color.black );
   
		vpanel = new Viewpanel ( );
      		vpanel.setBackground( Color.blue );
        	c.add ( vpanel, "PANEL1" );
		vpanel.addMouseListener(this);

		rpanel = new Randompanel ( );
      		rpanel.setBackground( Color.red );
        	c.add ( rpanel, "PANEL2" );
		rpanel.addMouseListener(this);
	}

	public void mousePressed(MouseEvent evt) 
	{
		if ( evt.isShiftDown() )
			cards.first ( c );
		else if ( evt.isMetaDown() )
			cards.previous ( c );
		else
			cards.next ( c );
	}

	public void mouseEntered(MouseEvent evt) { }
	public void mouseExited(MouseEvent evt) { }
	public void mouseClicked(MouseEvent evt) { }
	public void mouseReleased(MouseEvent evt) { }
	
	public class Viewpanel extends JPanel 
	{
		private ImagePanel panel1;
		private AllButtonsPanel panel2;
		private JSlider bottom;
		private JScrollBar left;
		private JRadioButton one, two, three, four; 
		private int width, height, value;
		private boolean yessnow, yesgrid;
	 
		public Viewpanel ( )  
		{
			yessnow = yesgrid = false;
			width = 300;
			height = 600;
	        	this.setLayout ( new GridLayout ( 1, 2, 5, 5 ) );
			this.setBackground ( Color.black );
	   
			panel1 = new ImagePanel ( );
	      		panel1.setBackground( Color.blue );
	        	this.add ( panel1 );
	
			panel2 = new AllButtonsPanel ( );
	      		panel2.setBackground( Color.red );
	        	this.add ( panel2 );
		}
	 
		class ImagePanel extends JPanel implements ChangeListener, AdjustmentListener   
		{
			private Image [] image;
	
			public ImagePanel ( )   
			{
				this.setLayout ( new BorderLayout ( ) );
	
				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] );
				}
	
	       			bottom = new JSlider ( JSlider.HORIZONTAL, 0, 300, 300 );
	       			this.add ( bottom, BorderLayout.SOUTH );
	       			bottom.addChangeListener ( this );
	
				left = new JScrollBar ( JScrollBar.VERTICAL, 600, 0, 0, 600 );
				left.addAdjustmentListener ( this );
				this.add ( left, BorderLayout.WEST );
			}
	
			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, width, height, this );
				if ( yesgrid )  
				{
					g.setColor ( Color.white );
					for ( int i = 0; i < 10; i++ )
						g.fillRect ( (int)(i * (double)width / 10), 0, 2, height );
					for ( int i = 0; i < 10; i++ )
						g.fillRect ( 0, (int)(i * (double)height / 10), width, 2 );
				}
				if ( yessnow )  
				{
					g.setColor ( Color.white );
					for ( int i = 0; i < 200; i++ )   
					{
						int x = (int)(Math.random() * width);
						int y = (int)(Math.random() * height);
						g.fillOval ( x, y, 6, 6 );
					}
				}
			}
	
	   		public void stateChanged ( ChangeEvent evt )   
			{
	       			width = bottom.getValue();
				this.repaint ( );
	   		}
	
			public void adjustmentValueChanged( AdjustmentEvent e)
			{
				height = left.getValue();
				this.repaint();
			}
		}
	 
		class AllButtonsPanel extends JPanel 
		{
			RadioPanel rpanel;
			ButtonPanel bpanel;
	
			public AllButtonsPanel ( )   
			{
	        		this.setLayout ( new GridLayout ( 2, 1, 5, 5 ) );
	
				rpanel = new RadioPanel ( );
	      			rpanel.setBackground( Color.yellow );
	        		this.add ( rpanel );
	
				bpanel = new ButtonPanel ( );
	      			bpanel.setBackground( Color.darkGray );
	        		this.add ( bpanel );
			}
	
			public void paintComponent ( Graphics g ) 
			{
				super.paintComponent ( g );
			}
		} 
	
		class RadioPanel extends JPanel implements ActionListener   
		{
			public RadioPanel ( )   
			{
	  			this.setLayout( new GridLayout ( 4, 1 ) );
	      
	      			ButtonGroup imageGroup = new ButtonGroup();
	      
	     	 		one = new JRadioButton ( "FIRST IMAGE" );
	      			imageGroup.add ( one );
	      			one.addActionListener ( this );
	      			this.add ( one );
	
	     	 		two = new JRadioButton ( "SECOND IMAGE" );
	      			imageGroup.add ( two );
	      			two.addActionListener ( this );
	      			this.add ( two );
	
	     	 		three = new JRadioButton ( "THIRD IMAGE" );
	      			imageGroup.add ( three );
	      			three.addActionListener ( this );
	      			this.add ( three );
	
	     	 		four = new JRadioButton ( "FOURTH IMAGE" );
	      			imageGroup.add ( four );
	      			four.addActionListener ( this );
	      			this.add ( four );
	
			}
	
			public void paintComponent ( Graphics g ) 
			{
				super.paintComponent ( g );
			}
	
	   		public void actionPerformed ( ActionEvent evt ) 
			{
	      			if ( one.isSelected() )
	         			value = 0;
	      			else if ( two.isSelected() )
					value = 1;
	      			else if ( three.isSelected() )
					value = 2;
	      			else if ( four.isSelected() )
					value = 3;
				panel1.repaint ( );
			}
		}
	
		class ButtonPanel extends JPanel implements ActionListener   
		{
			JButton snow, grid, reset;
	
			public ButtonPanel ( )   
			{
	  			this.setLayout( new FlowLayout ( FlowLayout.LEFT, 140, 50 ) );
	
				snow = new JButton ( "SNOW" );
	       			snow.addActionListener ( this );  
	       			this.add ( snow );
	
				grid = new JButton ( "GRID" );
	       			grid.addActionListener ( this );  
	       			this.add ( grid );
	
				reset = new JButton ( "RESET" );
	       			reset.addActionListener ( this );  
	       			this.add ( reset );
	
				one.setSelected(true); 
			}
	
			public void paintComponent ( Graphics g ) 
			{
				super.paintComponent ( g );
			}
	
	   		public void actionPerformed ( ActionEvent evt ) 
			{
	      			String command = evt.getActionCommand();
	
				if ( command.equals ( "SNOW" ) )   
				{
					yessnow = true;
					panel1.repaint ( );
				}
				else if ( command.equals ( "GRID" ) )   
				{
					yesgrid = true;
					panel1.repaint ( );
				}
				else if ( command.equals ( "RESET" ) )   
				{
					value = 0;
					width = 300;
					height = 600;
					yessnow = yesgrid = false;
					left.setValue ( 600 );
					bottom.setValue ( 300 );
					one.setSelected(true); 
					panel1.repaint ( );
				}
	   		}
	
		}
	}
	 
	public class Randompanel extends JPanel 
	{
		private SmallButtonPanel panel1;
		private BigButtonPanel panel2;
		private SliderPanel panel3;
		private DrawPanel panel4;
		private int panelchoice = 0, y = 0;
	 
		public Randompanel ( )  
		{
			setBackground ( Color.black );
	        	this.setLayout ( new GridLayout ( 2, 2, 5, 5 ) );
	   
			panel1 = new SmallButtonPanel ( );
	        	this.add ( panel1 );
	
			panel2 = new BigButtonPanel ( );
	        	this.add ( panel2 );
	
			panel3 = new SliderPanel ( );
	        	this.add ( panel3 );
	
			panel4 = new DrawPanel ( );
	        	this.add ( panel4 );
		}
	
		public Insets getInsets ( ) 
		{
			return new Insets ( 5, 5, 5, 5 );
		}
	 
		class SmallButtonPanel extends JPanel implements ActionListener   
		{
			public SmallButtonPanel ( )   
			{
				setBackground ( Color.blue );
	       			JButton small = new JButton ( " Random Rectangle " );
	       			small.addActionListener ( this );  
	       			this.add ( small );
			}
	
	   		public void actionPerformed ( ActionEvent evt ) 
			{
	      			String command = evt.getActionCommand();
	               
	      			if ( command.equals ( " Random Rectangle " ) )   
				{
	 	        		panelchoice = 1;
					panel4.repaint ( );
				}
	   		}
		}
	 
		class BigButtonPanel extends JPanel implements ActionListener   
		{
			public BigButtonPanel ( )   
			{
				setLayout(new BorderLayout());
				setBackground ( Color.cyan );
	       			JButton big = new JButton ( " Random Line " );
	       			big.addActionListener ( this );  
	       			this.add ( big, BorderLayout.CENTER );
			}
	
	   		public void actionPerformed ( ActionEvent evt ) 
			{
	      			String command = evt.getActionCommand();
	               
	      			if ( command.equals ( " Random Line " ) )   
				{
	 	        		panelchoice = 2;
					panel4.repaint ( );
				}
	   		}
		}
	 
		class SliderPanel extends JPanel implements ChangeListener   
		{
			JSlider slide;
	
			public SliderPanel ( )   
			{
				setLayout(new BorderLayout());
				setBackground ( Color.magenta );
				slide = new JSlider ( 0, 225, 100 );
				slide.setOrientation ( JSlider.VERTICAL );
				slide.addChangeListener ( this );
				this.add ( slide, BorderLayout.EAST );
			}
	
			public void stateChanged ( ChangeEvent e )   
			{
				if ( e.getSource() == slide )   
				{
					panelchoice = 3;
		            		y = slide.getValue ( );
					panel4.repaint ( );
				}
			}
		}
	 
		class DrawPanel extends JPanel 
		{
			private int x, otherx, othery;
	
			public DrawPanel ( )   
			{
				setBackground ( Color.yellow );
			}
	
			public void paintComponent ( Graphics g ) 
			{
				super.paintComponent ( g );
				if ( panelchoice == 1 )   
				{
					x = (int)(Math.random ( ) * ( getWidth ( ) - 20 ) );
					y = (int)(Math.random ( ) * ( getHeight ( ) - 20 ) );
					g.setColor ( Color.green );
					g.fillRect ( x, y, 20, 20 );
				}
				else if ( panelchoice == 2 )   
				{
					x = (int)(Math.random ( ) * getWidth ( ) );
					y = (int)(Math.random ( ) * getHeight ( ) );
					otherx = (int)(Math.random ( ) * getWidth ( ) );
					othery = (int)(Math.random ( ) * getHeight ( ) );
					g.setColor ( Color.blue );
					g.drawLine ( x, y, otherx, othery );
				}
				else if ( panelchoice == 3 )   
				{
					x = getWidth ( ) / 2;
					g.setColor ( Color.black );
					g.fillOval ( x, 225 - y, 20, 20 );
				}
			}
		}
	}
}

Back to Lesson 30 Examples

Back to Java Main Page