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

Mouse Input

 

There are two parts of a mouse - clicks and motion. They are treated as two seperate parts of input in Java.

 

Below are the following events that automatically get triggered if someone uses a button.

 

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

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

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

 

To use one or all of these methods, put the method in to your program.

 

When one of those methods are activated (ie when someone clicks somewhere in the applet, the method is called. We are given the coordinates of where they clicked. The coordinates are sent down as e.

They are:

e.getX() //for the x coordinate

e.getY() //for the y coordinate

For more on the difference between them, click here.

 

To use it in bluej, open the GUI Builder and drag MouseListener in.

 

 

 

 

HWJ8:

Create these programs (start with the Gui_Builder and drag mouse in above for each example):

If working alone - do 4. If working with a partner 5. If advanced - do atleast 1 more than required.

  1. Create FindLocation that will display the location of where the mouse clicks and it will display that in 2 textfields (one for x, one for y)
  2. Create SquareDraw with a textfield that will allow the user to put in a size of a square. When the user clicks, it will draw that size square centered around the cursor.
  3. Create LineDraw where a line will be drawn between where the user presses the button down to where they release the button.
  4. Create TopHalf that will change the applet to green if the mouse clicks on the top half and black if they click the bottom half. Use g.setColor and then g.fillRect to do that (or setBackground).
  5. Create RectangleDraw where an rectangle will be drawn between where the user presses the button down to where they release the button. (Advanced people - make it work from any direction)
  6. Create RectClicked that in the paint method add this rectangle g.fillRect(200,200,100,100) - SEE BELOW - that will draw a rectangle in the middle of the screen. Then count the number of times the user clicks in the region and display this in a textfield
  7. Create DottedLine that when the user clicks it will draw a dotted lines from the center to where the user clicked. Advanced - display the distance of the line in the textfield, you will need the distance formula

Super advanced: Bring in timer and make a game out of it. (Like have an object appear and the user has to click it - keep tracker)

 

For those doing RectClicked:

 
public void paint(Graphics g)
{
	g=getG();
    g.drawRect(....);
    drawScreen();
}