Using the mouse in animation programs

There are two main things that we want to detect and react to:
1. mouse clicks
2. mouse motion

Mouse clicks

First, you need to put gc.enableMouse(); somewhere in the initialization (setup) part of your program.

Then you need to detect if the mouse has been clicked. You need to do this in the main game loop, or some method called by it, so that you are checking repeatedly.

if (gc.getMouseClick() > 0) {
	//do stuff
}
getMouseClick() tells you if the mouse has been clicked. It returns the number of clicks. Theoretically, this can be used to see if it's a single or a double click, but I don't know if this actually works or not

NOTE: getMouseClick() also resets the number of clicks. So in any important program, you'll want to put gc.getMouseClick(); once in the intialisation code (to get rid of any clicks that were stored before the program began).

To get the location of where on the screen the mouse was clicked, use the following:

int mx = gc.getMouseX();
int my = gc.getMouseY();
I call these variable mx, my because they are the x and y of the mouse (and it's shorter than writing mouseX, mouseY).

You will almost always want to create mx,my as global variables so that they can be accessed all throughout your program, thus:
int mx,my; in global variable section
and
mx = gc.getMouseX(); my = gc.getMouseY();

Detecting Mouse Motion

First, you need to put gc.enableMouseMotion(); somewhere in the initialization (setup) part of your program.

The location of the mouse is then continuously updated in gc.getMouseX() and gc.getMouseY(). Yes, it's the same methods as detecting locations of mouse clicks.

int mx = gc.getMouseX();
int my = gc.getMouseY();

Which button was clicked?

gc.getMouseButton(n) will return a true or false if button 'n' has been pressed. n=0,1, or 2. The left button is 0. Use it like this:

if (gc.getMouseButton(2)) { //right click
	gc.setBackgroundColor(Color.ORANGE);
}

NOTE: the only way that it works in combination with gc.getMouseClick() is if it comes first like so:

if (gc.getMouseButton(2) && gc.getMouseClick() > 0 ) {

Other Mouse stuff

There is also a gc.enableMouseWheel() -- I have not used this yet. The methods getMouseWheelRotation() and getMouseWheelUnitsToScroll() are used with this

Mouse Dragging has been implemented, but I need to write a sample program to illustrate this.

 
I'm not sure how useful this next stuff is:

The gc.enableMouse() will also let you handle all of these things:
mouseClicked
mouseEntered (going over an object, or entering the HSA2 window)
mouseExited (exiting the above)
mousePressed
mouseReleased

The gc.enableMouseMotion() lets you handle these things:
mouseDragged
mouseMoved

Sample program to illustrate mouse interactions

import java.awt.Color;
import java.awt.Rectangle;

import hsa2.GraphicsConsole;

public class Mouse1 {

	public static void main(String[] args) {
		new Mouse1();
	}
	
	GraphicsConsole gc = new GraphicsConsole();
	Rectangle paddle = new Rectangle(300,450,80,10);
	Rectangle r1 = new Rectangle(100,100,100,50);
	
	Mouse1() {
		gc.setBackgroundColor(Color.BLACK);
		gc.clear();
		gc.setLocationRelativeTo(null);
		gc.setTitle("Click on the Green Rectangle");
		
		//this enables mouse clicking
		gc.enableMouse();
		
		//this line lets the program respond to the motion of the mouse
		gc.enableMouseMotion();
		
		while(true) {
			
			//This line makes the paddle move with the x location of the mouse. 
			//TODO: You'll have to fix it to get the mouse in the middle of the paddle though.
			paddle.x = gc.getMouseX();
			//paddle.y = gc.getMouseY();	//you probably don't want to do this. 
			
			if (gc.getMouseClick() > 0) {	
				if (r1.contains(gc.getMouseX(), gc.getMouseY())) {
					r1.x +=50;
					r1.y +=50;
				}
			}
			synchronized(gc) {
				gc.clear();
				
				gc.setColor(Color.GREEN);
				gc.fillRect(r1.x, r1.y, r1.width, r1.height);
				gc.setColor(Color.YELLOW);
				gc.fillRect(paddle.x, paddle.y, paddle.width, paddle.height);
			}
			gc.sleep(5);
			
		} //end of while loop
		
	}
}