// This class illustrates some objects that can be placed in the user interface // and some interactions that can take place with them. There is a TextArea // object, a TextField object, a Button object, and a Menu object. Actions // that are handled are mouse clicks, menu selections, typing in the text area, // and button selections. All actions are echoed in the TextArea. import java.applet.*; import java.awt.*; import java.awt.event.*; public class Interact extends Applet implements MouseListener, ActionListener, ItemListener { // Variables used by the Applet int last_x = 0; int last_y = 0; Button clear_button; Choice user_choices; TextArea echo_area; TextField user_typing; // Initializing the Frame public void init() { // Set the background color and listen for the mouse setBackground(Color.white); addMouseListener(this); // Create a button and add it to the Frame. clear_button = new Button("Clear"); clear_button.setForeground(Color.black); clear_button.setBackground(Color.lightGray); add(clear_button); clear_button.addActionListener(this); // Create a menu of user choices and add it to the // Frame. user_choices = new Choice(); user_choices.addItem("North"); user_choices.addItem("East"); user_choices.addItem("South"); user_choices.addItem("West"); add(user_choices); user_choices.addItemListener(this); // Create a TextArea and a TextField and add them to // the Frame. echo_area = new TextArea(5, 40); echo_area.setEditable(false); add(echo_area); user_typing = new TextField(40); add(new Label("User input: ")); add(user_typing); user_typing.addActionListener(this); } // Called when the user depresses the mouse button public void mousePressed (MouseEvent e) { echo_area.setText(echo_area.getText() + "Mouse Down at " + e.getX() + ", " + e.getY() + "\n"); Graphics g = this.getGraphics(); g.drawOval(e.getX(), e.getY(), 2, 2); // echo that point in the Frame last_x = e.getX(); last_y = e.getY(); } // Called when the user releases the mouse button public void mouseReleased (MouseEvent e) { echo_area.setText(echo_area.getText() + "Mouse Up at " + e.getX() + ", " + e.getY() + "\n"); } public void mouseClicked(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } // Called when the user selects a Button or TextArea public void actionPerformed (ActionEvent e) { Graphics g = this.getGraphics(); // If the Clear button was clicked, handle it. if (e.getSource() == clear_button) { Rectangle r = this.bounds(); g.setColor(this.getBackground()); g.fillRect(r.x, r.y, r.width, r.height); echo_area.setText(echo_area.getText() + "Clear button selected " + "\n"); } // Otherwise if some text was typed, handle that else if (e.getSource() == user_typing) { String s = user_typing.getText(); echo_area.setText(echo_area.getText() + "User Typing: " + s + "\n"); } } // Called when the user makes a Choice selection public void itemStateChanged (ItemEvent e) { echo_area.setText(echo_area.getText() + "Choice selected: " + e.getItem() + "\n"); } }