In this assignment you will write an applet that allows a user to paint with a "spray" pattern.
MouseListener
and MouseMotionListener
. For this assignment your applet will implement both.
The MouseListener
interface requires the following methods:
public void mousePressed(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
The MouseMotionListener
interface requires the following methods:
public void mouseDragged(MouseEvent e)
{
}
public void mouseMoved(MouseEvent e)
{
}
Just like a button, we need to specify what class will "listen" for mouse events. In this assignment, we only have one
class. So we'll put addMouseListener(this)
and addMouseMotionListener(this)
in our program.
You will want to have two integer data members in your applet class to hold the x and y of the mouse. You can then update the position of the mouse with following member
functions of MouseEvent
:
nMouseX = e.getX();
nMouseY = e.getY();}
For this assignment you will probably only want to use mousePressed, mouseReleased
and mouseDragged
. You can leave the other methods
as empty "stubs".
thread
for this assignment just as you did with Pong, so start with the basic outline given in the previous
assignment. This assignment will need only one class, the "applet" class. It will implement the following interfaces: Runnable, MouseListener,
MouseMotionListener, ItemListener
//generate a random distance from 0 to 19
int nRand = (int)(Math.random() * 20);
//generate a random angle from 0 to 2 pi
double dTheta = Math.random() * 628 / 100.0;
//find x and y of random dot
int nX = (int)(nMouseX + nRand * Math.cos(dTheta));
int nY = (int)(nMouseY + nRand * Math.sin(dTheta));
paint
method you might have something like:
if(bPainting)
{
for(int nPaint = 0; nPaint < 25; nPaint++)
{
//code to make spray pattern
}
You can set bPainting
to true
in mousePressed
. You should update nMouseX
and nMouseY
with the current mouse coordinates in mousePressed
and mouseDragged
. You should set bPainting
to
false
in mouseReleased
.
public void update(Graphics g)
{
paint(g);
}
You will need a web page to display your applet. Your homepage should have a link to the web page for this assignment.
Submit the URL of your applet along with your Java code (the .java
file(s)) in an email message
to mrsimon@lycos.com.