// File: TwoCanvas6.java (Module 10) // // Author: Rahul Simha // Created: November 2, 1998 // // Two canvases - using anonymous classes. import java.awt.*; import java.awt.event.*; class NewFrame extends Frame { // Data. Button quitb; Button clearb1, clearb2; Canvas c1, c2; Panel outerpanel, p1, p2; Point c1_current = new Point (0,0); Point c2_current = new Point (0,0); // Constructor. public NewFrame (int width, int height) { // Set the title and other frame parameters. this.setTitle ("Two canvas example"); this.setResizable (true); this.setBackground (Color.cyan); this.setSize (width, height); // this.setLayout (new BorderLayout()); // Create a quit button for the whole frame. quitb = new Button ("Quit"); quitb.setBackground (Color.red); quitb.setFont (new Font ("Serif", Font.PLAIN | Font.BOLD, 15)); // Using an anonymous class: quitb.addActionListener ( new ActionListener() { public void actionPerformed (ActionEvent a) { System.exit(0); } } ); this.add (quitb, BorderLayout.SOUTH); // Create a Panel for the first canvas p1 = new Panel (); p1.setLayout (new BorderLayout()); // Create a white canvas. c1 = new Canvas (); c1.setBackground (Color.white); c1.setForeground (Color.blue); // Create a MouseListener class: class CanvasMouseListener extends MouseAdapter { Canvas c; Point current; // Constructor. public CanvasMouseListener (Canvas c, Point current) { this.c = c; this.current = current; } // Handle a mouse click. public void mouseClicked (MouseEvent m) { int x = m.getX(); int y = m.getY(); Graphics g = c.getGraphics(); g.drawLine (current.x, current.y, x, y); current.x = x; current.y = y; } } c1.addMouseListener (new CanvasMouseListener(c1, c1_current)); // Add canvas to frame in the center. p1.add (c1, BorderLayout.CENTER); // Create a clear button. clearb1 = new Button ("Clear"); clearb1.setBackground (Color.green); // First create a listener class. class ClearActionListener implements ActionListener { Canvas c; Point current; // Constructor. public ClearActionListener (Canvas c, Point current) { this.c = c; this.current = current; } // Handle button-press. public void actionPerformed (ActionEvent a) { c.setBackground (Color.white); c.repaint (); current.x = current.y = 0; } } clearb1.addActionListener (new ClearActionListener(c1, c1_current)); p1.add (clearb1, BorderLayout.NORTH); // Create a Panel for the second canvas. p2 = new Panel (); p2.setLayout (new BorderLayout()); // Create a second white canvas. c2 = new Canvas (); c2.setBackground (Color.white); c2.setForeground (Color.blue); // Add a new instance of the MouseListener. c2.addMouseListener (new CanvasMouseListener(c2, c2_current)); // Add canvas to frame in the center. p2.add (c2, BorderLayout.CENTER); // Create a second clear button. clearb2 = new Button ("Clear"); clearb2.setBackground (Color.green); // Pass a new instance of the action listener. clearb2.addActionListener (new ClearActionListener(c2, c2_current)); p2.add (clearb2, BorderLayout.NORTH); // A panel to hold the smaller panels. outerpanel = new Panel (); outerpanel.setLayout (new GridLayout (1,2,5,5)); outerpanel.add(p1); outerpanel.add(p2); // Now add the panel to the frame. this.add (outerpanel, BorderLayout.CENTER); // Show the frame. this.setVisible (true); } } // End of class "NewFrame" public class TwoCanvas6 { public static void main (String[] argv) { NewFrame nf = new NewFrame (300, 200); } }