/*
* Created on Oct 31, 2004
* Modified: Nov 2, 2004.
* 
* Modified to illustrate BorderLayout, GridLayout, Panel, and ImageIcon.
*
* CS 178 Programming with Multimedia Objects
* 
* Objective: Learn to layout GUI objects using BorderLayout, GridLayout, and Panel.
* JButtons and ImageIcon are also introduced.
* Example: Display pictures of proteins and enzymes.
* 
* This program does not use exception handling. Try adding exception handling to statements that import
* images and sound.

/**
* @author Aditya Mathur
*
* 
*/

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;

public class PanelIllustrator  extends Applet implements ActionListener{

	
	// Create button for the user to indicate data is ready for input.

JButton bCenter=new JButton ("\nWelcome. Students of CS 178 and visitors.\n");

//Picture buttons. Click to see the picture.

JButton left1=new JButton("Acetylcholine Esterase");
JButton left2=new JButton("Acyl Carrier Protein, E.Coli");
JButton left3=new JButton("DNA Polimerase, E.Coli");
TextArea description=new TextArea(4,10);

//Define images and the corresponding description strings.

Image rajaImage, accholesImage, ecoliImage, dnapolymImage; // Will be displayed.

ImageIcon raja,  accholes, ecoli, dnapolym;

// Define descriptions.

String descrAccholes=new String("An enzyme, found in the synaptic clefts of cholinergic synapses, that cleaves the neurotransmitter acetylcholine \n into its constituents, acetate and choline, thus limiting the size and duration of the postsynaptic potential. \n Many nerve gases and insecticides are potent acetylcholine esterase inhibitors, and thus prolong the timecourse of postsynaptic potentials.");
String descrACP=new String(" Acyl carrier protein (ACP) performs the essential function of shuttling the intermediates between the enzymes that \n constitute the type II fatty acid synthase system.");
String descrDnapolym=new String("Enzyme DNA polimerase III (pol III) is large and complex e (~600kDa)  composed of ten \n different polypeptides (, b, c, d, d , g, q, t, and, y ");

TextField welcome=new TextField("Protein and Enzyme Picture Viewer and Java  GUI Layout Illustrator.");


// Audio versions of each description for the visually impaired.

AudioClip accholesClip=null;
AudioClip ecoliClip=null;
AudioClip dnapolymClip=null;


//Create a BorderLayout manager.
BorderLayout bLayout=new BorderLayout();

public void init(){

	
	// Load images, set icons, and load sounds.
	
	loadImagesAndSetIcons();
	loadSounds();
	
	
	// Set the font of the welcome message.
	
	welcome.setFont(new Font("SansSerif", Font.BOLD, 20));
	
	// Cteate two GridLayout managers for left and top panels.
	
	GridLayout leftPanelLayout=new GridLayout(3, 1);
	GridLayout topPanelLayout=new GridLayout(1, 3);
	
	// Creaate two panels and set their respective layout managers.
	
	Panel pTop=new Panel(topPanelLayout);
	Panel pLeft=new Panel(leftPanelLayout);
	

// Set the Applet layout  to BorderLayout.


this.setLayout(bLayout);


//Set properties of the center button. Initally place the raja icon in the center button.

bCenter.setIcon(raja);


//Add button and panels to the layout.

add( BorderLayout.CENTER, bCenter); 
add(BorderLayout.WEST,pLeft);


//Set up welcome message.

add( BorderLayout.NORTH, welcome); 


// Set left panel with buttons for pictures. Note that this panel has a GridLayout manager.

pLeft.add(left1);
pLeft.add(left2);
pLeft.add(left3);

//Now add ac tion listener to each  button.

left1.addActionListener(this);
left2.addActionListener(this);
left3.addActionListener(this);


// description is placed SOUTH.

add(BorderLayout.SOUTH, description);

setBackground(Color.orange);
	
} // End of init() method.
//Load images and create icons for use.

public void loadImagesAndSetIcons()
{

	// Note that getDocumentBase() returns the URL of the specified  file.
	
	Image rajaImage, accholesImage, ecoliImage, dnapolymImage;
	
	
	// Load the images of the planets and create icons from the images.
	// The icons will be later added to a JBUtton for display when requested.
	
	
	try{
		accholesImage=getImage(getDocumentBase(), "images/accholes.jpg");
		accholes=new ImageIcon(accholesImage);
	}
	catch(Exception e){
		description.append("accholes image not available.");
	}
		try{
		ecoliImage=getImage(getDocumentBase(),"images/acp.gif");
		ecoli=new ImageIcon(ecoliImage);
		}
		catch(Exception e){
			description.append("ACP image not available.");
		}
		
		try{
		dnapolymImage=getImage(getDocumentBase(),"images/dnapolym.gif");
		dnapolym=new ImageIcon(dnapolymImage);
	}
	catch(Exception e){
		description.append("dnapolym image not available.");
	}
	try{
		rajaImage=getImage(getDocumentBase(), "images/raja.jpg");
		raja=new ImageIcon(rajaImage);
	}
	catch(Exception e){
		description.append("raja image not available. "+ e.getMessage());
	}

}


// Load sounds associated with each description.
public void loadSounds()
{	
	
try{
		accholesClip=getAudioClip(getCodeBase(), "audio/accholes.au"); // Not available.
	}
	catch(Exception e){
		description.append("No audio available for accholes.");
	}
	try{
	ecoliClip=getAudioClip(getCodeBase(), "audio/ecoli.au"); // Not available.
	}
	catch(Exception e){
		description.append("No audio available for ACP.");
	}
	try{
	dnapolymClip=getAudioClip(getCodeBase(), "audio/dnapolymau/dnapolym");
	}
	catch(Exception e){
		description.append("No audio available for dnapolym.");
	}
}
public void actionPerformed(ActionEvent e){
	
	Object source=e.getSource();
	if(source==left1){
		bCenter.setText("");
	 bCenter.setIcon(accholes);
	 description.setText(descrAccholes);
	 description.append("\n (Audio unavailable.)");
	}
	else if(source==left2){
		bCenter.setText("");
		bCenter.setIcon(ecoli);
		description.setText(descrACP);
		description.append("\n (Audio unavailable.)");
	}
	else if(source==left3){
		bCenter.setText("");
		bCenter.setIcon(dnapolym);
		description.setText(descrDnapolym);
		dnapolymClip.play();
	}
	
		}// End of actionPerformed() method.



}




