package jbgui;
/*
	Joseph Bergin, Nov. 1997
	Version 3, December 2000
*/

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Color;
import java.awt.Event;
import java.awt.Font;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.BorderLayout;
//import java.awt.PrintJob;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.KeyEvent;
import java.awt.event.InputEvent;
//import java.net.*;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.JApplet;
import javax.swing.KeyStroke;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
//import javax.swing.ImageIcon;

public class AllenApp extends JApplet
{	public AllenApp(JFrame myFrame){ this.myFrame = myFrame;}
	private JFrame myFrame;
	
	public void init() 
	{	resize(640, 480);
		getContentPane().setBackground(Color.yellow); 
		
		// Get a menu bar.
		JMenuBar mb = myFrame.getJMenuBar();
		if(mb == null)
		{ 	mb = new JMenuBar();
			getRootPane().setJMenuBar(mb);
		}
		// Create a menu for the menu bar.
		activitiesMenu = new JMenu("Activities");
		activitiesMenu.setMnemonic(KeyEvent.VK_A);
		mb.add(activitiesMenu);
		//popup = new PopupMenu("Actiities");
		//myFrame.add(popup);
		// Add menu items to the menu.  Give each an ActionListener for its action.
		JMenuItem item = new JMenuItem("Cyan");
		item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, Event.CTRL_MASK));
		item.setMnemonic(KeyEvent.VK_C);
		activitiesMenu.add(item);
		//popup.add(item);
		item.addActionListener(new CyanListener());

		item = new JMenuItem("Yellow...");
		activitiesMenu.add(item);
		item.setMnemonic(KeyEvent.VK_Y);
		//popup.add(item);
		item.addActionListener(new YellowListener());
		
		item = new JMenuItem("Beep");
		item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK));
		item.setMnemonic(KeyEvent.VK_E);
		activitiesMenu.add(item);
		item.addActionListener(new BeepListener());

		item = new JMenuItem("Box");
		item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, Event.CTRL_MASK));
		item.setMnemonic(KeyEvent.VK_B);
		activitiesMenu.add(item);
		item.addActionListener(new BoxListener());
		
		item = new JMenuItem("Picture");
		item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, Event.CTRL_MASK));
		item.setMnemonic(KeyEvent.VK_P);
		activitiesMenu.add(item);
		item.addActionListener(new PictureListener());
		
		item = new JMenuItem("Circle");
		item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, Event.CTRL_MASK));
		item.setMnemonic(KeyEvent.VK_R);
		activitiesMenu.add(item);
		item.addActionListener(new CircleListener());
		
		item = new JMenuItem("Info...");
		item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, Event.CTRL_MASK));
		item.setMnemonic(KeyEvent.VK_I);
		activitiesMenu.add(item);
		item.addActionListener(new InfoListener());
		
				
		// Create a modeless dialog box with two buttons.  
		yellowDialog = new JDialog(myFrame, "Your Wish is our command.",false);
		yellowDialog.setSize(230, 100);
		yellowDialog.setLocation(50, 50);
		yellowDialog.getContentPane().add(new JLabel("Restore Yellow Background?"), BorderLayout.CENTER);
		JPanel dialogPanel = new JPanel();
		yellowDialog.getContentPane().add(dialogPanel, BorderLayout.SOUTH);
		JButton yesButton = new JButton( "Yes");
		yesButton.addActionListener( new YesListener() );
		JButton noButton = new JButton("No");
		noButton.addActionListener(new NoListener() );
		dialogPanel.add(yesButton, BorderLayout.WEST);
		dialogPanel.add(noButton, BorderLayout.EAST);
		
		// Create a modal dialog box with one button. 
		infoDialog = new JDialog(myFrame, "Data on Screen and Current ViewPort.",true);
		infoDialog.setLocation(50, 50);
		infoPanel = new JPanel();
		infoPanel.setFont(new Font("SanSerif", Font.PLAIN, 9));
		infoDialog.getContentPane().add(infoPanel);
		//Define the panel that will show the info in this dialog box. 
		infoPanel.add("North", new JLabel("Size of the screen and the window in pixels:"));
		myToolkit = getToolkit();
		Dimension d = myToolkit.getScreenSize(); // Doesn't change as we run.
		String lab = "screen's length = " + d.width + ", screen's height = " + d.height;
		infoPanel.add(new JLabel( lab ), BorderLayout.CENTER);
		infoDialog.setSize(100 + infoPanel.getFontMetrics(infoPanel.getFont()).stringWidth(lab), 120);
		windowSize = new JLabel(""); // Will show current window size.  
		infoPanel.add(windowSize, BorderLayout.SOUTH);
		JButton okButton = new JButton("OK");
		infoDialog.getContentPane().add(okButton, BorderLayout.SOUTH);
		okButton.addActionListener(new OkListener());
		
		// Handle mouse clicks in our window.  
		addMouseListener(new MouseWatcher());
		
		// Get the image displayed by menu Picture. 
		image = myToolkit.getImage("JBergin.jpg");
	}
	
	public boolean imageUpdate(Image image, int flags, int x, int y, int w, int h)
	{	if((flags & ALLBITS) != 0) 
		{	Graphics g = getGraphics();
			g.drawImage(image, 100, 100, AllenApp.this);
			g.dispose();
		}
		return true;
	}

/*	
	public void print()
	{	PrintJob printing = myToolkit.getPrintJob(myFrame, "Testing", null);
		if(printing == null)
			System.out.println("No print job");
		else
		{	Graphics drawer = printing.getGraphics();
			if(drawer == null) 
				System.out.println("No Graphics.");
			else
			{	paint(drawer);
				drawer.dispose();
			}
			printing.end();
		}
	}
	
	public void paint(Graphics g)
	{	super.paint(g);  // Paint the background and the menubar...
		g.drawString("Hello World", 100, 100);
	}
*/
	
	private JMenu activitiesMenu;
	private JDialog yellowDialog, infoDialog;
	private JPanel infoPanel;
	private JLabel windowSize;
	private Toolkit myToolkit;
	private Image image;
	//PopupMenu popup;
	
	// Inner Classes
	
	class CyanListener implements ActionListener // Menu Cyan
	{	public void actionPerformed(ActionEvent e)
		{	getContentPane().setBackground(new Color(200, 255, 255)); //pale Cyan (RGB)
			repaint();
		}
	}

	class YellowListener implements ActionListener // Menu Yellow...
	{	public void actionPerformed(ActionEvent e)
		{	yellowDialog.setVisible(true);
		}
	}

	class BeepListener implements ActionListener // Menu Beep
	{	public void actionPerformed(ActionEvent e)
		{	myToolkit.beep();
		} // We could actually play an audio clip here. 
	}

	class BoxListener implements ActionListener // Menu Box
	{	public void actionPerformed(ActionEvent e)
		{	Graphics g = getGraphics();
			g.drawRect(120, 30, 90, 50); // x, y, width, height
			g.dispose();
		}
	}

	class PictureListener implements ActionListener // Menu Picture
	{	public void actionPerformed(ActionEvent e)
		{	Graphics g = getGraphics();
			g.drawImage(image, 100, 100, AllenApp.this);			
			g.dispose();
			// Alternatively: 
		//	JLabel joe = new JLabel(new ImageIcon("JBergin.jpg"));
		//	getContentPane().add(joe);
		}
	}

	class CircleListener implements ActionListener // Menu Circle
	{	public void actionPerformed(ActionEvent e)
		{	Graphics g = getGraphics();
			g.setColor(Color.green);
			g.drawOval(120, 80, 200, 200);
			g.dispose();
//		print();
		}
	}

	class InfoListener implements ActionListener // Menu Info
	{	public void actionPerformed(ActionEvent e)
		{	Rectangle r = getBounds();		
			windowSize.setText("window's length = " + r.width + ", window's height = " + r.height);
			infoDialog.setVisible(true);
		}
	}

	class OkListener implements ActionListener // OK button for Info dialog.
	{	public void actionPerformed(ActionEvent e)
		{	infoDialog.setVisible(false);
		}
	}

	class YesListener implements ActionListener // Yes button for Yellow... dialog.
	{	public void actionPerformed(ActionEvent e)
		{	yellowDialog.setVisible(false);
			getContentPane().setBackground(Color.yellow);
			repaint();
		}
	}

	class NoListener implements ActionListener // No button for Yellow... dialog.
	{	public void actionPerformed(ActionEvent e)
		{	yellowDialog.setVisible(false);
		}
	}

	class MouseWatcher extends MouseAdapter // Mouse click handler.
	{	public void mouseClicked(MouseEvent e)
		{	Graphics g = getGraphics();
			int x = e.getX(), y = e.getY();
			if(e.getModifiers() == InputEvent.BUTTON3_MASK) // Right button (Ctrl click on Mac).
			{	Font font = new Font("Serif", Font.ITALIC, 10);
				g.setFont(font);
				g.setColor(Color.red);
				g.drawString("Right Click at:(" + x + "," + y + ")", x, y);
			}
			else // Other buttons.
				g.drawString("Left Click", x, y);
			g.dispose();
		}
		
/*		public void mousePressed(MouseEvent e)
		{	Graphics g = getGraphics();
			int x = e.getX(), y = e.getY();
			if(e.isPopupTrigger()) popup.show(myFrame, x, y);
			g.dispose();
		}
*/
	}
}
