// Skeleton of a Java interactive program import java.applet.*; import java.awt.*; import java.awt.event.*; public class Skeleton extends Applet { // Applet's global "state" of the interaction int lastX = 0; // first click's x-y coordinates int lastY = 0; int clickNumber = 0; // most recent click since last user choice. Choice choice; TextArea echoArea; TextField typing; // Initializing the Frame public void init() { // Set the background color and listen for the mouse setBackground(Color.white); addMouseListener(new MouseHandler()); // Create a button and add it to the Frame. Button clearButton = new Button("Clear"); clearButton.setForeground(Color.black); clearButton.setBackground(Color.lightGray); add(clearButton); clearButton.addActionListener(new ClearButtonHandler()); // Create a menu of user choices and add it to the Frame. choice = new Choice(); choice.addItem("Nothing"); choice.addItem("Rectangle"); choice.addItem("Message"); add(choice); choice.addItemListener(new ChoiceHandler()); // Create a TextField and a TextArea and add them to // the Frame. typing = new TextField(40); add(typing); typing.addActionListener(new TextHandler()); echoArea = new TextArea(2, 40); echoArea.setEditable(false); add(echoArea); } // Handling events that occur in the frame // Called when the user clicks the mouse button private class MouseHandler extends MouseAdapter { public void mouseClicked(MouseEvent e) { int x = e.getX(); int y = e.getY(); echoArea.setText("Mouse Clicked at " + e.getX() + ", " + e.getY() + "\n"); Graphics g = getGraphics(); if (choice.getSelectedItem().equals("Rectangle")) { clickNumber = clickNumber + 1; // is it the first click? if (clickNumber % 2 == 1) { echoArea.append("Click to set lower right corner of the rectangle"); lastX = x; lastY = y; } // or the second? else g.drawRect(lastX, lastY, Math.abs(x-lastX), Math.abs(y-lastY)); } else if (choice.getSelectedItem().equals("Message")) // for a message, display it g.drawString(typing.getText(), x, y); } // mouseClicked } // Called when the user selects clear Button private class ClearButtonHandler implements ActionListener { public void actionPerformed (ActionEvent e) { echoArea.setText("Clear button selected "); repaint(); } } // Called when the user selects TextArea private class TextHandler implements ActionListener { public void actionPerformed (ActionEvent e) { echoArea.setText("Text entered: " + typing.getText()); if (choice.getSelectedItem().equals("Message")) echoArea.append("\nNow click to place this message"); } } // Called when the user selects a Choice private class ChoiceHandler implements ItemListener { public void itemStateChanged (ItemEvent e) { String c = (String) (e.getItem()); echoArea.setText("Choice selected: " + c); // prepare to handle first mouse click for this choice clickNumber = 0; if (c.equals("Rectangle")) echoArea.append("\nClick to set upper left corner of the rectangle"); else if (c.equals("Message")) echoArea.append("\nEnter a message in the text area"); } } }