// File: TestAwt28.java (Module 9) // // Author: Rahul Simha // Created: October 21, 1998 // // Update. 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 ("Paint and Update"); 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; int new_x, new_y; // These methods are required to implement // the MouseListener interface. public void mouseClicked (MouseEvent m) { new_x = m.getX(); new_y = m.getY(); Graphics g = this.getGraphics(); paint (g); } public void mouseEntered (MouseEvent m) {} public void mouseExited (MouseEvent m) {} public void mousePressed (MouseEvent m) {} public void mouseReleased (MouseEvent m) {} public void paint (Graphics g) { System.out.println ("Paint: cx=" + current_x + ",cy=" + current_y + ", nx=" + new_x + ",ny=" + new_y); g.drawLine (current_x, current_y, new_x, new_y); current_x = new_x; current_y = new_y; } // Override update() and make sure the // background is not cleared. public void update (Graphics g) { paint (g); } } // End of class "NewFrame" public class TestAwt28 { public static void main (String[] argv) { NewFrame nf = new NewFrame (300, 200); } }