Texter applet Page

Here's the applet.

Student: Sean Spalding
Due Date April 22, 1997

Go somewhere else

or proceed down the page to view the source for this dazzling piece of contemporary art.



------------------------------------------- snip ---------------------------------------------

// Name:      Texter.java
// Author:    Sean Spalding
// Reason:    Like there are any reasons in life! OK, it was for an assignment.


import java.applet.Applet;
import java.awt.*;

public class Texter extends Applet
{
	Button blackButton, yellowButton, blueButton, redButton;	// variables for Buttons
	TextField textLabel, textField = null;					// variables for TextFields
	Choice fontChoice;								// variable for dropdown list
	Font font = null;								// variable for font settings
	int style = Font.PLAIN, size = 12;					// variables for font attributes
	Checkbox cb1, cb2; 		// variables for type style checkboxes
	Checkbox cb3, cb4; 		// variables for type size radio buttons
	CheckboxGroup cbg; 		// variable for the group for the radio buttons

	public void init()
	{
		//first time running program

		// create the variables for panels needed to hold the GUI components
		Panel labelPanel, fieldPanel, stylePanel, sizePanel, fontPanel, middlePanel, bottomPanel, buttonPanel;

        // instantiate the default font settings
		font = new Font("Courier", Font.PLAIN, 12);

		// instantiate the uneditable textfield
		textLabel = new TextField("Enter the line of text:", 25);
		textLabel.setEditable(false);

		// panel to hold the non-editable text field
		labelPanel = new Panel();
		labelPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		labelPanel.add(textLabel);

		// instantiate the textfield
		textField = new TextField(35);

		// panel to hold the editable text field
		fieldPanel = new Panel();
		fieldPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		fieldPanel.add(textField);

		// instantiate the style checkboxes
		cb1 = new Checkbox("Italic");
		cb2 = new Checkbox("Bold");

		// panel to hold the style checkboxes
		stylePanel = new Panel();
		stylePanel.setLayout(new GridLayout(2,1));
		stylePanel.add(cb1);
		stylePanel.add(cb2);

		// instantiate the size checkboxes and their checkboxgroup
		cbg = new CheckboxGroup();
		cb3 = new Checkbox("size 12", cbg, false);
		cb4 = new Checkbox("size 14", cbg, false);

		// panel to hold the size radio buttons
		sizePanel = new Panel();
		sizePanel.setLayout(new GridLayout(2,1));
		sizePanel.add(cb3);
		sizePanel.add(cb4);

		// instantiate the drop down list box and its items
		fontChoice = new Choice();
		fontChoice.addItem("TimesRoman");
		fontChoice.addItem("Courier");
		fontChoice.addItem("Helvetica");
		fontChoice.addItem("Arial");

		// panel to hold the list box
		fontPanel = new Panel();
		fontPanel.setLayout(new GridLayout(0,1));
		fontPanel.add(fontChoice);

		// panel to hold the checkbox, radio button and list panels
		middlePanel = new Panel();
		middlePanel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
		middlePanel.add(stylePanel);
		middlePanel.add(sizePanel);
		middlePanel.add(fontPanel);

		// instantiate the buttons
		blackButton = new Button();
		blackButton.setLabel("Black");
		yellowButton = new Button();
		yellowButton.setLabel("Yellow");
		blueButton = new Button();
		blueButton.setLabel("Blue");
		redButton = new Button();
		redButton.setLabel("Red");

		buttonPanel = new Panel();		// panel to hold the buttons
		buttonPanel.setLayout(new GridLayout(0,4));
		buttonPanel.add(blackButton);
		buttonPanel.add(yellowButton);
		buttonPanel.add(blueButton);
		buttonPanel.add(redButton);

		bottomPanel = new Panel();		// panel to hold the buttonPanel
		bottomPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 10));
		bottomPanel.add(buttonPanel);

		// add the components to the applet
		setLayout(new GridLayout(5,1));
		add(labelPanel);
		add(fieldPanel);
		add(middlePanel);
		add(bottomPanel);

		validate();
	}

	public boolean action(Event e, Object arg)		// an event has taken place
	{
		Object target = e.target;		// define variable to hold target 'component' value

        if (target instanceof Checkbox)		// was it a checkbox that was clicked?
		{
			if ((target == cb3) || (target == cb4))		// was it a size checkbox?
			{
				Checkbox selected = (cbg.getCurrent());		// variable of type checkbox

				if (selected == cb3)		// was it 'size 12' that was checked?
				{
					size = 12;
					repaint();
					return true;    // event handled
				}

				if (selected == cb4)		// was it 'size 14' that was checked?
				{
					size = 14;
					repaint();
					return true;    // event handled
				}
			}
			
			if ((target == cb1) || (target == cb2))		// was it a style checkbox that was clicked?
			{
				if	(cb1.getState() && cb2.getState())	// first check if that makes both boxes checked
				{
					style = Font.ITALIC + Font.BOLD;	// change the setting
					repaint();							// update the screen
					return true;    			// event handled
				}
				else if (cb1.getState())				// not both boxes, is it just 'italic'?
				{
					style = Font.ITALIC;
					repaint();
					return true;    // event handled
				}
				else if (cb2.getState())				// not 'italic' is it 'bold'?
				{
					style = Font.BOLD;
					repaint();
					return true;    // event handled
				}
				else				// no boxes checked, must be 'plain'
				{
					style = Font.PLAIN;
					repaint();
					return true;    // event handled
				}
			}
		}

		if (target instanceof Choice)		// was a choice made from the list?
		{	 
			repaint();
			return true;
		}

		if (target instanceof Button)		// was it a button that was clicked?
		{
			if (target == blackButton)		// was it the 'black' button?
			{
				textField.setForeground(Color.black);	// change fore colour setting of text
				repaint();								// update the textfield
				return true;    // event handled
			} 

			if (target == yellowButton)
			{
				textField.setForeground(Color.yellow);
				repaint();
				return true;
			}

			if (target == blueButton)
			{
				textField.setForeground(Color.blue);
				repaint();
				return true;
			}

			if (target == redButton)
			{
				textField.setForeground(Color.red);
				repaint();
				return true;
			}
		}
		return false;
	}

	public void paint(Graphics g)		// contains code to update font in textField
	{
		font = new Font(fontChoice.getSelectedItem(), style, size);
		textField.setFont(font);
	}
}

------------------------------------------- snip ---------------------------------------------


1