Prac 15 – Mouse events

advertisement
Practical Exercise 15 – Mouse events
The purpose of this practical is to learn how to use the MouseListener and MouseMotionListener
in your applets.
Mouse Events: MouseListener
To use the mouse buttons you must:
1. import java.awt.event.*
2. add implements MouseListener to the class header
3. include addMouseListener(this) in init()
4. add all of the following methods (whether you use them or not):
o
mousePressed
– mouse pressed down
o
mouseClicked
– mouse pressed down and allowed to click up again
o
mouseReleased
– mouse was down and is released
o
mouseEntered
– mouse is in the Applet window
o
mouseExited – mouse is outside the Applet window.
C Standard
The following program simply tells you the coordinates of the cursor in the Applet window.
First, enter and run this program to check that it works.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class MouseDemo extends Applet implements MouseListener
{
int x, y;
//coordinates of mouse
public void init()
{
addMouseListener(this);
}
public void paint (Graphics g)
{
g.drawString("Mouse is at (" + x + "," + y + ")", x, y);
}
public void mousePressed (MouseEvent e)
{
x = e.getX();
y = e.getY();
repaint();
}
public void mouseClicked (MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
Claremont College 2014, adapted from Rosny College 2009
Page 1 of 4
public void mouseEntered (MouseEvent e)
{
}
public void mouseExited
{
(MouseEvent e)
}
}
Now, add to this program to make use of the other mouse methods, so that the position of the
mouse, as well as the type of event is displayed for any event.
For example, here is what mousePressed() and mouseEntered() might look like…
String event;
public void mousePressed(MouseEvent e)
{
x=e.getX();
y=e.getY();
event="Pressed";
repaint();
}
public void mouseEntered(MouseEvent e)
{
showStatus("Mouse entered Applet area");
}
C+ Standard
Create a simple drawing program using the following tips.
Mouse Events: MouseMotionListener
MouseMotionListener includes the following methods:

mouseDragged tells if the mouse is being moved with its button pressed

mouseMoved tells if the mouse is being moved with its button not pressed
To use these methods you must implement MouseMotionListener by including it in the class
header and you must add MouseMotionListener in init().
For example:
public class Mouse12C3 extends Applet implements MouseListener, MouseMotionListener
{
public void init()
{
addMouseMotionListener (this);
addMouseListener (this);
}
Below is the code that you can add to the previous program to listen to mouse movements:
//mouseDragged indicates if mouse is being dragged (ie moved with button
pressed)
public void mouseDragged (MouseEvent e)
{
Claremont College 2014, adapted from Rosny College 2009
Page 2 of 4
x = e.getX();
y = e.getY();
event = "dragging";
repaint();
}
//mouseMoved indicates mouse being moved with button not pressed
public void mouseMoved (MouseEvent e)
{
x = e.getX();
y = e.getY();
event = "moving";
repaint();
}
Using the mouse to draw on the screen
The methods mouseMoved and mouseDragged enable you to draw in the Applet window using
the mouse. To do this you need to use the “update” method using the code:
public void update (Graphics g)
{
paint(g);
}
Normally, when repaint() is called it completely clears the screen of all previous graphics and
paints the new information onto the resulting clean screen. If you use the update method,
repaint() leaves previous graphics on the screen. So, if you are drawing a line using the mouse
the Applet window will remember the previous lines drawn on the screen.
Include the following code which allows you to draw a series of circles in the window which
follow the motion of the mouse.
//draw small circle at position of mouse
public void paint (Graphics g)
{
g.fillOval(x - 2, y - 2, 4, 4);
}
The distance between successive circles depends on how fast the
mouse is moving, and on how frequently the particular computer
can call the repaint() method.
Use mouseListener and mouseMotionListener and the
mouseDragged() method.
Output could look like this - if you were clever enough!!!
Claremont College 2014, adapted from Rosny College 2009
Page 3 of 4
B Standard
Write a program which will let the user draw a continuous line in
the Applet window when the mouse is dragged.
You will need to re-initialise the x and y co-ordinates when the
mouse is pressed (which corresponds to starting a new section of
drawing) and store the previous x and y values.
Use mouseListener and mouseMotionListener and include an
update method.
To draw a continuous line, you can use the following code:
public void paint (Graphics g)
{
g.drawLine(oldX, oldY, newX, newY);
}
Where:
 newX and newY give the current x and y coordinates of the mouse (as obtained using the
method getX(), getY() in mousePressed).
 oldX and oldY give the previous x and y coordinates: the program draws short lines from
the old to the new coordinates.
B+ Standard
Write a program which will let the user draw a series of
connected straight lines in the Applet window.
You will need to use mouseListener, and you will need to store
the previous x and y values.
You will need to use the continuous line code shown in the
previous example.
A Standard
Create your own media player with a play and a stop button (and a loop if you wish).
Your buttons will be a square shape and when the mouse is clicked in this area you play a sound
clip or you stop play.
You may like to have two colours for the button – one to represent when it’s “on”, and another
when not active.
Claremont College 2014, adapted from Rosny College 2009
Page 4 of 4
Download