CIS-257 Home: http://www.c-jump.com/bcc/c257c/c257syllabus.html

Custom 2D rendering and mouse events


  1. Introduction
  2. Adding mouse events, step-by step
  3. DrawingBox2D.java
  4. Download

Introduction



Adding mouse events, step-by step


  1. Open DrawingBox2D.java

  2. Add MouseListener and MouseMotionListener interfaces to the DrawingBox2D class:

    
    public class DrawingBox2D extends javax.swing.JPanel
            implements MouseListener, MouseMotionListener {
    
    
  3. Modify DrawingBox2D class constructor to add mouse listeners:

    
        public DrawingBox2D() {
            initComponents();
            addMouseMotionListener( this );
            addMouseListener( this );
        }
    
    
  4. Add mWindow object reference to the attributes of the DrawingBox2D class:

    
    public class DrawingBox2D extends javax.swing.JPanel {
    
        private int degrees = 0;
        private FrameDemo2D mWindow;
        //...
    
    

    The reason for this is that mouse events often need to tell the rest of the window about user clicks and mouse coordinates. In such case the methods of DrawingBox2D will be aable to call public methods of the mWindow object as necessary.

  5. Add implementation of the mouse event handlers to the DrawingBox2D class:

    
    public class DrawingBox2D extends javax.swing.JPanel
            implements MouseListener, MouseMotionListener {
        //...
    
        // MouseListener implementation
        @Override
        public void mousePressed(MouseEvent evt) {
        }
    
        @Override
        public void mouseReleased(MouseEvent evt) {
        }
    
        @Override
        public void mouseEntered(MouseEvent evt) {
        }
    
        @Override
        public void mouseExited(MouseEvent evt) {
        }
    
        @Override
        public void mouseClicked(MouseEvent evt) {
            // the mouse coordinates are
            // evt.getX()
            // evt.getY()
        }
        
        // MouseMotionListener implementation
        @Override
        public void mouseMoved(MouseEvent evt)
        {
            // the mouse coordinates are
            // evt.getX()
            // evt.getY()
        }
    
        @Override
        public void mouseDragged(MouseEvent e)
        {
        }
    
    
  6. Add setWindow() method to the DrawingBox2D class:

    
        // operations
        public void setWindow( FrameDemo2D aWindow )
        {
            mWindow = aWindow;
        }
    
    
  7. Open FrameDemo2D.java

  8. Modify FrameDemo2D constructor to call setWindow() method of the drawing box:

    
         public FrameDemo2D() {
            initComponents();
            setTitle( "2D Graphics Demo" );
            DrawingBox2D drawingBox = (DrawingBox2D)pnlDrawingBoard;
            drawingBox.setWindow( this );
        }
           //...
    
    
  9. To test the mouse listener, try mouse clicking that changes the background color of the drawing box. First, add backgroundColor attribute to the DrawingBox2D class:

    
    public class DrawingBox2D extends javax.swing.JPanel
            implements MouseListener, MouseMotionListener {
        //...
    
        private Color backgroundColor;
    
    

    Then, modify your mouse event handlers as follows:

    
    public class DrawingBox2D extends javax.swing.JPanel
            implements MouseListener, MouseMotionListener {
        //...
    
        // MouseListener implementation
        @Override
        public void mousePressed(MouseEvent evt) {
            backgroundColor = getBackground();
            setBackground( Color.GREEN );
            repaint();
        }
    
        @Override
        public void mouseReleased(MouseEvent evt) {
            setBackground( backgroundColor );
            repaint();
        }
    
    

    Compie and run the application. You should see the background color change while you press the left mouse button.

     


DrawingBox2D.java



Download