Java Assignment 12 -- Spray Paint

Hold Down (Left) Mouse Button and Move Mouse to Control Spray Paint Pattern
Change Angle of "spray can" with scroll bars

In this assignment you will write an applet that allows a user to paint with a "spray" pattern.

Mouse Events

Java has two interfaces for Mouse Events: 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".

Starting the assignment

You will want to use a 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

The Spray Pattern

One Way to simulate a spray pattern is to generate a random angle and distance from the mouse position and place a dot at that spot. The following code will calculate a new x and y that are at that random angle and distance from the mouse:


               //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));

Painting

It's a good idea to use a boolean member variable to keep track of whether you should be painting. In the 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.

Overriding update

To keep the screen from erasing the paint pattern each time the screen is repainted, we will need to override the update function with the following code:


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.