TextPlay.java
/*
Program to play with GUI components by writing a text item on the
screen and changing its characteristics.
Updated: March 2002, JDT
*/
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import objectdraw.*;
public class TextPlay extends WindowController implements ActionListener,
AdjustmentListener,
ItemListener
{
// contants for font size scrollbar
private final static int MINFONTSIZE = 8;
private final static int MAXFONTSIZE = 48;
private final static int DEFAULTSIZE = 10;
// physical dimensions of the scrollbar
private final static int SCROLLBARWIDTH = 190;
private final static int SCROLLBARHEIGHT = 10;
// dimensions of panel to hold controls
private final static int CONTROLPANELWIDTH = 300;
private final static int CONTROLPANELHEIGHT = 100;
// Scrollbar used to control font size
private Scrollbar fontSize;
// Label to identify scrollbar on screen
private Label fontScrollbarLabel;
// Label used to display current font size
private Label fontSizeLabel;
// Field where user can enter text to be placed on canvas
private TextField whatToWrite;
// Menu used to select font style
private Choice fontChooser;
// Menu used to select text color
private Choice colorChoice;
// Button used to instruct program to hide latest text item
private Button changeText;
// Panel used to collect components at bottom of screen
private Panel bottomPanel;
// Latest message added to the canvas
private Text display;
public void begin() {
// Create an initial text item so that "display" is defined
display = new Text("",0,0,canvas);
display.hide();
// Create panel to hold components
//bottomPanel = new Panel(); // doesn't quite work
bottomPanel = new SizeablePanel(CONTROLPANELWIDTH, CONTROLPANELHEIGHT);
// Create components for text entry
whatToWrite = new TextField("Enter text to be displayed here");
bottomPanel.add(whatToWrite);
// Create menu to control font used
bottomPanel.add( new Label("Select Font: "));
fontChooser = new Choice();
fontChooser.addItem("Courier");
fontChooser.addItem("Wingdings");
fontChooser.addItem("Symbol");
fontChooser.addItem("Times");
fontChooser.addItemListener(this);
bottomPanel.add(fontChooser);
// Add button used to hide remove last text item
changeText = new Button("Hide last text");
changeText.addActionListener(this);
bottomPanel.add(changeText);
// Create the scrollbar and associated labels.
fontScrollbarLabel = new Label("Font sizer:");
bottomPanel.add(fontScrollbarLabel);
fontSize = new Scrollbar(Scrollbar.HORIZONTAL,DEFAULTSIZE,0,
MINFONTSIZE,MAXFONTSIZE+1);
fontSize.setSize(SCROLLBARWIDTH,SCROLLBARHEIGHT);
fontSize.addAdjustmentListener(this);
bottomPanel.add(fontSize);
fontSizeLabel = new Label("Current font size: "+DEFAULTSIZE+" ");
bottomPanel.add(fontSizeLabel);
// add all components below the canvas
add(bottomPanel,"South");
}
// Hide latest text when button is pressed
public void actionPerformed(ActionEvent e)
{
display.hide();
}
// Change size of text displayed when scrollbar is adjusted
public void adjustmentValueChanged(AdjustmentEvent e)
{
display.setFontSize(fontSize.getValue());
fontSizeLabel.setText("Current font size: "+fontSize.getValue());
}
// Change font used when menu item changes.
public void itemStateChanged(ItemEvent e)
{
display.setFont(fontChooser.getSelectedItem());
}
// Place text in canvas wherever mouse is clicked.
public void onMouseClick(Location point)
{
display = new Text(whatToWrite.getText(),point,canvas);
display.setFont(fontChooser.getSelectedItem());
display.setFontSize(fontSize.getValue());
}
}