email me at borlaj@portlandschools.org
notes previous (09/07/06) submit the dump links  
 

Mouse Motion:

 

If we want to track where the mouse is exactly

We need to also implement MouseMotionListener

public class classname implements MouseListener, MouseMotionListener {

Add to your init()

addMouseMotionListener(this)

You then need to define all seven mouse methods

mouseMoved, mouseDragged, mouseClicked, mousePressed, mouseReleased, mouseEntered, mouseExited

mouseMoved will be called each time the mouse makes a significantly large movement

mouseDragged will be called instead if the mouse button is pressed and the mouse makes a significantly large movement

 

For more on the difference between them, click here.

 

Below is starter code for motion and clicks.

 
/*
 * Test class to practice mouse movement by drawing a circle where the cursor is.
 *
 * @author Jeff Borland
 * @version 1.11
 */

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

public class MouseMovement extends JApplet implements MouseListener, MouseMotionListener,ActionListener 
{  
    public void init()
    {
        addMouseListener(this);
        addMouseMotionListener(this);
        Container screen = getContentPane();
        screen.setBackground(Color.white);
        screen.setLayout (new FlowLayout() );

    }

    public void paint (Graphics g)
    {
        super.paint(g);  
    }

    //when someone presses the mouse button
    public void mousePressed(MouseEvent e)
    {       

    }

    //when someone releases the mouse button
    public void mouseReleased(MouseEvent e)
    {       

    }

    // when the mouse enters the applet
    public void mouseEntered(MouseEvent e)
    {       

    }

    //when the mouse leaves the applet
    public void mouseExited(MouseEvent e)
    {       

    }

    //when the mouse button is clicked
    public void mouseClicked(MouseEvent e)
    {

    }

    //the mouse button is pressed and the mouse makes a significantly large movement
    public void mouseDragged(MouseEvent e)
    {

    }

    //the mouse makes a significantly large movement
    public void mouseMoved(MouseEvent e)
    {
        int x=e.getX();
        int y=e.getY();
        Graphics g = getGraphics();
        g.fillOval(x,y,25,25); 
    }
    
    public void actionPerformed(ActionEvent thisEvent)
    {
        Object source = thisEvent.getSource();

        //now have if statements seeing finding out where the action occured

    }

}

Easiest way of clearing screen:

//clear a rect as large as you want
g.clearRect(0,0,500,500);
            

 

Create these applets: 3 points each - do 4 of 5

  1. In an applet called Gradient that when the mouse is moved left or right, it will adjust the amount of red in the background. If it goes up or down, it will adjust the amount of green.
  2. In an applet SnowMan that has a small SnowMan follow the cursor around (have the screen clear each time).
  3. In an applet AnnoyTheUser have a circle that appears at a random location. When the user moves the cursor close to the circle (say within 30 pixels), the circle will move to a new random location. Fun. (if you want, copy findRandom)
    • Code for finding distance to center of circle
                      
          public double distanceToCenter(int centerX, int centerY, int mouseX, int mouseY)
          {
              int a=centerX-mouseX;
              int b=centerY-mouseY;
              return Math.sqrt(a*a+b*b);
          }
                      
  4. In an applet Rainbow have a rainbow of colors appear when the user moves the mouse. It will be beautiful; kind of like a rainbow.Use g.setColor(Color.blue) or any color Have a reset button.
  5. In an applet drawDynamicRect that when the user presses down and then drags their mouse it will constantly draw (and erase) a rectangle that is that size. When they release the mouse, it will finalize that rectangle. (code will be in mousepressed, mousedragged and mousereleased) [advanced - have it work in any direction]
 		//Code to get a random color
        int red=(int)(Math.random()*256);
        int blue=(int)(Math.random()*256);
        int green=(int)(Math.random()*256);
        Color randomColor=new Color(red,blue,green); 
        g.setColor(randomColor);            
            

 

Advanced

  1. In an applet Operation, there will be a square inside a square. The idea of the game is that the user must move their mouse around the inside of the square without hitting the walls. It will keep track of how much time it takes for them to accomplish this goal. Put this inside paint:
            g.drawRect(50,50,400,400);
            g.drawRect(60,60,380,380);                 
                    
    You probably will want to have a global int corner; Where whenever they make it to a corner it will change which corner you need to hit next. This is hard, but oh what a fun game it would be. 
    Timer here
    That part is required. Additional ideas:
    • give them 2 seconds to get in the safe region originally.
    • put buzzer sound when they hit wall
    • flash red when they hit wall.
    • Have a timer keeping track of how long it takes
    • keep track of best time.