Mouse Input
There are two parts of a mouse - clicks and motion. They are treated
as two seperate parts of input in Java.
In an applet (JApplet), it involves importing the following classes:
import java.awt.event.*;
then we need to added the line after our class declaration implements
MouseListener This tells we will be listening for a mouse
action.
Now we addMouseListener(this);
to our init() method. This line is saying we are listening to a
mouse action.
Now whenever we implement MouseListener, we need
to declare the following methods (whether we use them or not)
public void mousePressed(MouseEvent e) { } //when
someone presses the mouse button
public void mouseReleased(MouseEvent e) { } //when
someone releases the mouse button
public void mouseEntered(MouseEvent e) { } // when
the mouse enters the applet
public void mouseExited(MouseEvent e) { } //when the
mouse leaves the applet
public void mouseClicked(MouseEvent e) {} //when the
mouse button is clicked
To use one or all of these methods, put code in method.
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.
Heres is a sample:
/*
* Test class to practice mouse inputs by drawing a circle where the user clicks.
*
* @author Jeff Borland
* @date version 1.11
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class OvalDraw extends JApplet implements MouseListener,ActionListener
{
public void init()
{
Container screen = getContentPane();
screen.setBackground(Color.white);
screen.setLayout (new FlowLayout() );
addMouseListener(this);
}
public void paint (Graphics g)
{
super.paint(g);
g.fillOval(0,0,25,25);
}
//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)
{
// MouseEvent e is the variable, which we can get
// the coordinates of where the mouse is, by
// using the methods e.getX(); or e.getY();
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
}
}
HWJ8:
Create these applets (start with the code above for each example):
If working alone - do 4. If working with a partner 5. If advanced - do atleast 1 more than required.
- Create an applet 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)
- Create an applet 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.
- Create an applet LineDraw where a line will be drawn between
where the user presses the button down to where they release the
button.
- Create an applet 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.
- Create an applet 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)
- Create an applet RectClicked that in the paint method add this rectangle g.fillRect(200,200,100,100) 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
- Create an applet 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)
|