// Skeleton of an interactive program for Lab 9 import java.applet.*; import java.awt.*; import java.awt.event.*; public class Skeleton extends Applet implements MouseListener, ActionListener, ItemListener { // Variables used by the Applet -- this is the global "state" of the interaction int last_x = 0; // first click's x-y coordinates int last_y = 0; int click_number = 0; // most recent click since last user choice. String currentChoice = ""; // most recent user choice String currentMessage = ""; // most recent user typing Button clear_button; Choice user_choices; TextArea echo_area; TextField user_typing; // Initializing the Frame -- this establishes the objects and their listeners 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("Nothing"); user_choices.addItem("Rectangle"); user_choices.addItem("Message"); add(user_choices); user_choices.addItemListener(this); // Create a TextField and a TextArea and add them to // the Frame. user_typing = new TextField(40); add(user_typing); user_typing.addActionListener(this); echo_area = new TextArea(2, 40); echo_area.setEditable(false); add(echo_area); } // Hendling events that occur in the frame -- these methods change the // state and the appearance of the frame // Called when the user clicks the mouse button public void mouseClicked(MouseEvent e) { int x = e.getX(); int y = e.getY(); echo_area.setText("Mouse Clicked at " + e.getX() + ", " + e.getY() + "\n"); Graphics g = this.getGraphics(); // was it the first mouse click since the last choice was made? if (click_number==0) { click_number = 1; if (currentChoice.equals("Message")) g.drawString(currentMessage, x, y); // for a rectangle, save it and prepare for the second mouse click else if (currentChoice.equals("Rectangle")) { echo_area.setText("Click on lower right corner of the rectangle"); last_x = x; last_y = y; } } // or the second? else { click_number = 0; if (currentChoice.equals("Rectangle")) g.drawRect(last_x, last_y, Math.abs(x-last_x), Math.abs(y-last_y)); } } public void mouseExited(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mousePressed (MouseEvent e) { } public void mouseReleased (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; clear the frame. if (e.getSource() == clear_button) { echo_area.setText("Clear button selected "); Rectangle r = this.bounds(); g.setColor(this.getBackground()); g.fillRect(r.x, r.y, r.width, r.height); } // Otherwise if some text was typed, handle that; save the text else if (e.getSource() == user_typing) { currentMessage = user_typing.getText(); echo_area.setText("Text entered: " + currentMessage); if (currentChoice.equals("Message")) echo_area.setText(echo_area.getText() + "\nNow click to place this message"); } } // Called when the user makes a Choice selection public void itemStateChanged (ItemEvent e) { currentChoice = (String) (e.getItem()); echo_area.setText("Choice selected: " + currentChoice); click_number = 0; // prepare to handle first mouse click for this choice if (currentChoice.equals("Rectangle")) echo_area.setText(echo_area.getText() + "\nClick on upper left corner of the rectangle"); if (currentChoice.equals("Message")) echo_area.setText(echo_area.getText() + "\nEnter a message in the text area"); } }