// File: TwoCanvas2.java (Module 10) // // Author: Rahul Simha // Created: November 2, 1998 // // Two canvases - using local classes. import java.awt.*; import java.awt.event.*; class NewFrame extends Frame implements MouseListener { // Data. Button quitb; // Quit button. Button clearb1, clearb2; // Two clear buttons. Canvas c1, c2; // Two canvases. Panel p1, p2; // One panel per canvas. Panel outerpanel; // A panel to contain p1, p2. int c1_current_x=0, // Current mouse-click position c1_current_y=0, // for canvas 1. c2_current_x=0, c2_current_y=0; // Same for canvas 2. // 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)); // Create a local class. QuitActionListener is our name. class QuitActionListener implements ActionListener { public void actionPerformed (ActionEvent a) { System.exit(0); // Action required for quit. } } // Create an instance of the local class // and pass it to the button. quitb.addActionListener (new QuitActionListener()); // Now add the quit button to the frame. 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); c1.addMouseListener (this); // 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 ClearActionListener1 implements ActionListener { public void actionPerformed (ActionEvent a) { c1.setBackground (Color.white); c1.repaint (); c1_current_x = c1_current_y = 0; } } // Pass an instance of the class to the button. clearb1.addActionListener (new ClearActionListener1()); // Now add the button to the panel. 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); c2.addMouseListener (this); // 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); // Define a local class for the second button class ClearActionListener2 implements ActionListener { public void actionPerformed (ActionEvent a) { c2.setBackground (Color.white); c2.repaint (); c2_current_x = c2_current_y = 0; } } // Pass an instance to the (second) clear button. clearb2.addActionListener (new ClearActionListener2()); // Add the button to panel p2. 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); } // These methods are required to implement // the MouseListener interface. public void mouseClicked (MouseEvent m) { int x = m.getX(); int y = m.getY(); // Need to figure out which canvas: if (m.getSource() == c1) { Graphics g = c1.getGraphics(); g.drawLine (c1_current_x, c1_current_y, x, y); c1_current_x = x; c1_current_y = y; } else { Graphics g = c2.getGraphics(); g.drawLine (c2_current_x, c2_current_y, x, y); c2_current_x = x; c2_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 TwoCanvas2 { public static void main (String[] argv) { NewFrame nf = new NewFrame (300, 200); } }