Mouse & Graphics Demo

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MouseGraphics
{
   int x = 0, y = 0; // used for coordinates of mouse click
   Drawing draw = new Drawing(); // an inner class where we will do our drawing
   NTJFrame frame = new NTJFrame("Mouse & Graphics"); // the main window for our application
   ImageIcon toronto = new ImageIcon("toronto.gif"); // an image of the Leafs
   
   public MouseGraphics()
   {
      frame.getContentPane().add(draw);
      draw.addMouseListener(new MouseListen());
      frame.setVisible(true);
   }
   
   public static void main(String[] args)
   {
      MouseGraphics mg = new MouseGraphics();
   }
   
   /**************  Drawing  *********************************************
   * A class that handles the drawing for this program                   *
   **********************************************************************/
   class Drawing extends JComponent
   {
      /***  paint  ***********************************************************
      * Purpose: Does the drawing in this application.  It is like the       *
      *          paint method in the applet we used in the house assignment. *
      * Paramenter: g - the object we use to do the drawing                  *
      ***********************************************************************/
      public void paint(Graphics g)
      {
         int width = frame.getWidth();
         int height = frame.getHeight();

         g.setColor(Color.white);
         g.fillRect(0, 0, width, height);
         g.setColor(Color.black);
         for (int row = 100; row < height; row += 100)
            g.drawLine(0,row, width, row);
         for (int col = 100; col < width; col += 100)
            g.drawLine(col, 0, col, height);
         if (x == 0 && y == 0)
            g.drawString("Click the mouse", 10, 15);
         else
         {
            int left = x / 100 * 100;
            int top = y / 100 * 100;
            g.drawImage(toronto.getImage(), left, top, 100, 100, this);
         }
      }
   }
   
   /**************  MouseListen  *****************************************
   * A class that lets us know where the mouse is clicked                *
   **********************************************************************/
   class MouseListen extends MouseAdapter
   {
      /***  mouseReleased  ***************************************************
      * Purpose: Is called automatically when the mouse button is released   *
      *          (usually when the mouse is clicked but also after a drag)   *
      * Paramenter: e - contains information about the mouse (we're          *
      *                 interested in where the mouse is clicked)            *
      ***********************************************************************/
      public void mouseReleased(MouseEvent e)
      {
         x = e.getX();
         y = e.getY();
         draw.repaint();
      }
   }
}