User Tools

Site Tools


lang:judo:english:tutorial-en-008

[ Intro | Begin | if/else | Loops | Arrays | Graphics | Animation | Mouse | Game | Real | Methods | Class | Class 2 | Applet | MouseClick | Thread | Button ]


Java For Kids tutorial 8

Java Mouse Capture

Methods:

Return Value Method Name Description
boolean getMouseEvent() Returns true if there was a mouse event (mouse button pressed, mouse button released, mouse moved, or mouse dragged)
int getMouseX() Returns the x location of the mouse (use after getMouseEvent() returns true)
int getMouseY() Returns the y location of the mouse (use after getMouseEvent() returns true)
boolean getMouseButton (int buttonNumber) Returns true if the specified mouse button is down (use after getMouseEvent() returns true)

Java Mouse Move

N.B. You will need the version JUDO 1.3 from judo.sourceforge.net.

Mouse move capture is first set up by the use of the getMouseEvent() method. This method will return true if the mouse is moving..

Next we get the location of the X coordinate by using getMouseX().

And finally find the Y coordinate by using getMouseY().

Lets set up an algorithm for the first part…

    // initialize variables
    // start an infinite loop
    // check if mouse move returns true
    // get the location of the mouse
    // print the location of the mouse
    // restart infinite loop

Lets do some code to flesh out the above algorithm:

    // initialize variables
    int mx = 0;
    int my = 0;
 
     // start an infinite loop
     while (true)
     {
         // check if mouse move returns true
         if (getMouseEvent())
         {
             // get the location of the mouse
             mx = getMouseX();
             my = getMouseY();
 
             // print the location of the mouse
             printLine("x= "+ mx + "y = "+ my);
         }
         // restart infinite loop
     }

Beautiful. Did you see all those numbers streaming down in the bottom textarea?

That was all the positions of the mouse being printed.

Next up we want to draw a circle to follow where the mouse goes. Instead of printing the locations of mouseX and mouseY we will use those numbers to draw the circle at the x and y of the mouse.

So now just change the line “printline(…)” to:

    setColor(red);
    fillCircle(mx, my, 20);

What happened?… Do you only want one red ball to be drawn on the screen?

Well, we will have to clear the screen and set a delay. Remember the last lesson where we set a delay and cleared the screen for animation. Same story…

Lets add:

    delay(0.10);
    clearDrawing();

Yep it worked, but it needs one minor adjustment. The mouse pointer is located at the top right of the red ball and I want it in the center of the ball.

So we need to subtract what?

Half the radius of the red ball in the x and y coordinates.

So change the fillCircle code:

    fillCircle(mx-10, my-10, 20);

Here is the complete code..

    void main() {
        // initialize variables
        int mx = 0;
        int my = 0;
 
        // start an infinite loop
        while (true)
        {
            // check if mouse move returns true
            if (getMouseEvent())
            {
                // get the location of the mouse
                mx = getMouseX();
                my = getMouseY();
 
                // draw circle at mouse posn
                setColor(red);
                fillCircle(mx-10, my-10, 20);
 
                // set animation delay and clear screen
                delay(0.10);
                clearDrawing();
            }
        // restart infinite loop
        }
    }

Well how did that go… there are a few problems. The animation is jerky. And when the mouse is not moving, there is no ball painted. As an exercise, see if you can solve those problems.

Java Mouse Click

The method that will be used mainly for capturing mouse clicks will be getMouseButton(int buttonNumber) . This method returns true if the corresponding number for a mouse button is clicked.

  • 1 - Left Mouse click
  • 2 - Center Mouse button clicked
  • 3 - Right Mouse clicked

Lets try it out!

    void main() {
        int mx = 0;
        int my = 0;
 
        // start infinite loop
        while (true)
        {
            if (getMouseEvent())
            {
                // get mouse posns
                mx = getMouseX();
                my = getMouseY();
 
                // check if left button clicked
                if (getMouseButton(1)) {
                    printLine("Left mouse clicked at " + mx + " " + my);
                }
 
                // check if right button clicked
                if (getMouseButton(3)) {
                    printLine("right mouse clicked at " + mx + " " + my);
                }
            }
        }
    }

Right! Now lets see if we can have the red ball be a defender in a game of Aliens Invasion. We need to move the red ball back and forth along the bottom whenver we move the mouse.

Lets write an alogorithm first:

    // initialize x and y posn variables
    // start loop
    // check for mouse move or click
    // move red ball in tune with mouse
    // end loop

Copy the algorithm into our judo text editor and fill in the code. Try it out… What did you get? Anything like this?

    void main() {
        // initialize x and y posn variables
        int mx = 0;
        int my = 0;
 
        // start loop
        while (true) {
            // check for mouse move or click
            if (getMouseEvent()) {
                mx = getMouseX();
                my = getMouseY();
 
                // move red ball in tune with mouse
                setColor(red);
                fillCircle(mx-10, getDrawingHeight()-20, 20);
                delay(0.025);
                clearDrawing();
            }
        // end loop
        }
    }

Now we want the defender to fire a bullet or missile. And we want the bullet to fire from the position that the defender is when the left mouse is clicked.

So we need 2 new variables and call them clickX and clickY… and also we need an amount that the bullet goes each time the loop goes round.

Dont forget we have a delay which is our pretend framerate.

Lets do some test code to see what we can do:

    void main()
    {
        int mx = 0;
        int my = 0;
        int clickx = 0;
        int clicky = getDrawingHeight() - 20;
        int speed = 5;				// 5 pixels per 0.025 seconds
        boolean clicked = false;
 
        // infinite loop
        while (true) {
            if (getMouseEvent()) {		// check mouse event
                mx = getMouseX();
                my = getMouseY();
                setColor(red);
                fillCircle(mx-10, getDrawingHeight()-20, 20);
                if (getMouseButton(1)) {	// left clik
                    clicked = true;
                    clickx = getMouseX();
                }
            }
            if (clicked) {			// set when left click
                setColor(green);
                fillCircle(clickx, clicky, 10);
                if (clicky > 0) {
		    clicky = clicky - speed;
		} else {
		    clicked = false;
		    clicky = getDrawingHeight();
		}
                delay(0.025);
                clearDrawing();
            }
        }
    }

We are getting somewhere but still lots of problems. The red ball only gets erased when the button is pressed. The bullet only fires once. The clickx position is changing as we drag the mouse to left or right. Try and have a go at solving these problems.

In the next lesson we will go on to designing our space invaders game and use the real Java compiler…

lang/judo/english/tutorial-en-008.txt · Last modified: 2007/02/08 08:14 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki