// File: TestAwt23.java (Module 9) // // Author: Rahul Simha // Created: October 21, 1998 // // Handling mouse clicks. import java.awt.*; import java.awt.event.*; // Needed for ActionListener. class NewFrame extends Frame implements ActionListener, MouseListener { // Constructor. public NewFrame (int width, int height) { // Set the title and other frame parameters. this.setTitle ("Button example"); this.setResizable (true); this.setBackground (Color.cyan); this.setSize (width, height); // Create a quit button. Button b = new Button ("Quit"); b.setBackground (Color.red); b.setFont (new Font ("Serif", Font.PLAIN | Font.BOLD, 15)); b.addActionListener (this); // this.setLayout (new BorderLayout()); this.add (b, BorderLayout.SOUTH); // Add mouse-listening to the frame itself. this.addMouseListener (this); // Show the frame. this.setVisible (true); } // This method is required to implement the // ActionListener interface. public void actionPerformed (ActionEvent a) { // Button must have been pressed - so really quit. System.exit (0); } int current_x=0, current_y=0; // These methods are required to implement // the MouseListener interface. public void mouseClicked (MouseEvent m) { System.out.println ("mouseClicked event: " + m.paramString()); int x = m.getX(); int y = m.getY(); Graphics g = this.getGraphics(); g.drawLine (current_x, current_y, x, y); current_x = x; current_y = y; } // We need to implement these methods, but // don't actually have to do anything inside. public void mouseEntered (MouseEvent m) {} public void mouseExited (MouseEvent m) {} public void mousePressed (MouseEvent m) {} public void mouseReleased (MouseEvent m) {} } // End of class "NewFrame" public class TestAwt23 { public static void main (String[] argv) { NewFrame nf = new NewFrame (300, 200); } }