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

// This applet allows the user to select the font type and the size of the
//  displayed text by making a selection from the Choice boxes.  It also
//  allows the user to change the color of the text by choosing an item
//  from the menu Change Color in the menu bar.
public class MenuOptionApplet extends JApplet implements ItemListener,
																			ActionListener
{
	// These are the actual items that the user will be selecting in the menu.
	//  we made them instance variables so that we could use them in init and
	//  acionPerformed
	private JMenuItem red, green, blue;
	
	// This variable holds the current color for the text in the applet.
	//  It's value changes as different menu items are selected.
	private Color currentColor;

	// The following two Choice boxes will hold the various selections that the
	//  user can choose from to change the text's font and font size.
	Choice font, size;
	
	// This holds the current font size of the text
	int currentSize;
	
	// This holds the current font type of the text
	String currentFont;
	
	// init method
	public void init ()
	{
		// Initialize the choice box for font size
		size = new Choice ();
		
		// We will allow the user to choose any font size between 10 and 100, in
		//  increments of 5.
		for (int x = 10; x <= 100; x+=5)
		{
			// We add each possible selection in a String format to our choice box.
			//  The order they are added in does matter.  They will be added in
			//  order from top to bottom.
			size.add ("" + x);
		}
		// Add an item listener to the choice box so that we can be notified
		//  when the user changes the selection
		size.addItemListener (this);
		
		// Initialize the choice box for font type
		font = new Choice ();
		
		// Get the current graphics environment so that we can find out what
		//  fonts are available on the machine which this applet is running on.
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		
		// Call this method from GraphicsEnvironment to get a String array of
		//  all of the avaible fonts on the computer where our applet is running
		//  (i.e. you will probably end up with a different list of available fonts
		//  if you run this applet on different computers).
		String fontFamilyNames[] = ge.getAvailableFontFamilyNames(); 
		
		// Add each of the available fonts (one at a time) to our font choice box
		for (int x = 0; x < fontFamilyNames.length; x++)
		{
			font.add (fontFamilyNames[x]);
		}
		// Add an item listener so that we can tell when the user changes the
		//  font selected.
		font.addItemListener (this);
		
		// Get an alias to our container so that we can add the two choice boxes
		Container c = getContentPane();
		
		// Set the layout to the FlowLayout so that we don't have to set the size
		//  or location of any of our components
		c.setLayout (new FlowLayout());
		
		// Add the two choice boxes to the container
		c.add(font);
		c.add(size);
		
		// Set the initial value of current font to be the first available font
		currentFont = fontFamilyNames[0];
		// Set the initial value of current size to be 10
		currentSize = 10;
		
		// Declare and initialize a menu bar for our applet
		JMenuBar menuBar = new JMenuBar();
		
		// This method call will add the menu bar to the applet.  The interpreter
		//  will take care of the size and placement of the menu bar.
		setJMenuBar (menuBar);
		
		// Declare and initialize a couple of menus that will be added to our
		//  menu bar.  The String parameter basically sets the menu's label
		JMenu colors = new JMenu ("Change Colors");
		JMenu help = new JMenu ("Help");

		// Add the menus to the menu bar (order does matter, they will be added
		//  in order from left-to-right)
		menuBar.add (colors);
		menuBar.add (help);
		
		// Initialize the menu items that will eventually be added to our color
		//  menu.   The String parameter sets the label for the menu item.
		red = new JMenuItem ("Red");
		green = new JMenuItem ("Green");
		blue = new JMenuItem ("Blue");
		
		// Add action listeners to each of our menu items so that we can tell
		//  when the user clicks on them.
		red.addActionListener (this);
		green.addActionListener (this);
		blue.addActionListener (this);
		
		// Add the menu items to one of the menus.  The order they are added does
		//  matter.  They are placed in order from top to bottom in the menu.
		colors.add (red);
		colors.add (green);
		colors.add (blue);
		
		// Set the initial value of current color to be red
		currentColor = Color.red;
	}

	// paint method
	public void paint (Graphics g)
	{
		// Call super class's version of paint to repaint the components
		super.paint (g);
		
		// Set the color to the current color
		g.setColor (currentColor);
		
		// Create a new font based on the current font and current size
		Font font = new Font(currentFont, Font.PLAIN, currentSize); 
		
		// Set the new font
		g.setFont(font);
		
		// Draw a String on the graphics area
		g.drawString ("Good Morning!", 40, 200);

	}

	// itemStateChanged - from ItemListener
	public void itemStateChanged (ItemEvent e)
	{
		// If the source of the change is the font Choice box
		if (e.getSource() == font)
		{
			// Then set the current font to be the new selection.
			//  getItem returns an Object which in the case of Choice boxes can be
			//  casted down to the String that was selected by the user.
			currentFont = (String)e.getItem();
		}
		// otherwise, if it was the size choice box that changed
		else if (e.getSource() == size)
		{
			// Then set the current size to be the int version of the selection
			//  getItem returns an Object which in the case of Choice boxes can be
			//  casted down to the String that was selected by the user, which in
			//  this case can then be parsed as an int value
			currentSize = Integer.parseInt ( (String) e.getItem ());
		}
		
		// Call repaint so that the changes are made visible
		repaint();
	}
	
	// actionPerformed - from ActionListener
	public void actionPerformed (ActionEvent e)
	{
		// If the menu item red was selected change the current color to red
		if (e.getSource () == red)
			currentColor = Color.red;
		// If the menu item blue was selected change the current color to blue
		else if (e.getSource () == blue)
			currentColor = Color.blue;
		// If the menu item green was selected change the current color to green
		else if (e.getSource () == green)
			currentColor = Color.green;
		
		// repaint so that the changes are made visible.
		repaint();
	}
}

Back to Lesson 31 Examples

Back to Java Main Page