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;
// 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;
// Panels used to hold components for scrollbar and text entry area
private Panel scrollPanel, entryPanel, menuPanel;
// Latest message added to the canvas
private Text display;
// Indicates whether text is currently visible in window
private boolean textShown;
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();
bottomPanel.setLayout( new GridLayout(3,1));
// Create components for text entry
entryPanel = new Panel();
whatToWrite = new TextField("Enter text to be displayed here");
entryPanel.add(whatToWrite);
bottomPanel.add(entryPanel);
// Construct panel for font control and hide button
menuPanel = new Panel();
// Create menu to control font used
menuPanel.add( new Label("Select Font: "));
fontChooser = new Choice();
fontChooser.addItem("Courier");
fontChooser.addItem("Wingdings");
fontChooser.addItem("Symbol");
fontChooser.addItem("Times");
fontChooser.addItemListener(this);
menuPanel.add(fontChooser);
// Add button used to hide remove last text item
changeText = new Button("Hide last text");
changeText.addActionListener(this);
menuPanel.add(changeText);
bottomPanel.add( menuPanel );
// Create the scrollbar and associated labels.
scrollPanel = new Panel();
fontScrollbarLabel = new Label("Font sizer:");
scrollPanel.add(fontScrollbarLabel);
fontSize = new Scrollbar(Scrollbar.HORIZONTAL,DEFAULTSIZE,0,MINFONTSIZE,MAXFONTSIZE+1);
fontSize.setSize(SCROLLBARWIDTH,SCROLLBARHEIGHT);
fontSize.addAdjustmentListener(this);
scrollPanel.add(fontSize);
fontSizeLabel = new Label("Current font size: " + DEFAULTSIZE);
scrollPanel.add(fontSizeLabel);
bottomPanel.add( scrollPanel ) ;
// add all components below the canvas
add(bottomPanel,BorderLayout.SOUTH);
}
// Hide latest text when button is pressed
public void actionPerformed(ActionEvent e)
{
if ( textShown ) {
display.hide();
changeText.setLabel("Show hidden text");
} else {
display.show();
changeText.setLabel("Hide last text");
}
textShown = ! textShown;
}
// 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());
textShown = true;
changeText.setLabel("Hide last text");
}
}