import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class mm10 extends Applet implements Runnable, MouseListener, MouseMotionListener {

//Move Mouse to drag picture across screen
//two moving images and a background image
//<applet code="mm(number).class" height=520 width=760>
//<param name="myImage" value="your_name.gif">
//<param name="myImage1" value="your_name1.gif">
//<param name="myBkg" value="your_bkg.jpg">
//<param name="iSize" value="200"></applet>
//you can leave param blank and use the default
//sf.gif (saint francis) 200 wide x 200 tall
//background
//lpb60.jpg luffenpuff beach 760 wide 520 tall

//declare parameter names... String and int required for numbers
String myImg; 
String myImg1; 
String myBkg; 
String iSizeText;
int iSizeNum = 200;
int cp; //center of moving image

   int mx, my;  // the mouse coordinates
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 boolean isLeftButtonPressed = false;
 boolean isRightButtonPressed = false;
int whichButton;
//e.getButton() returns 1 for left button and 3 for right button
int unicorn=0;

 private volatile Thread runner;

        private Image Buffer;
        private Graphics gBuffer;
        Image background, snail,snail1;
      Dimension appletSize;  

        //Init is called first, do any initialization here
        public void init()
        {
                //create graphics buffer, the size of the applet
        setBackground(Color.black);
        appletSize = this.getSize();  

        Buffer = this.createImage(appletSize.width, appletSize.height);
        gBuffer=Buffer.getGraphics();
        //background=getImage(getCodeBase(),"lpb60.jpg");

  addMouseListener( this );
  addMouseMotionListener( this );

//get parameters from webpage
myImg = getParameter("myImage");
if(myImg == null){myImg="sf.gif";}
snail=getImage(getCodeBase(),myImg);

myImg1 = getParameter("myImage1");
if(myImg1 == null){myImg="sf1.gif";}
snail1=getImage(getCodeBase(),myImg1);

myBkg = getParameter("myBackground");
if(myBkg == null){myBkg="myDefaultBkg.jpg";}
background=getImage(getCodeBase(),myBkg);

//get parameters for integer 
//first read String value
//then check for null
//then convert to an integer number
iSizeText=getParameter("iSize");
if(iSizeText == null){iSizeText="200";}
iSizeNum = Integer.parseInt(iSizeText);
cp=iSizeNum/2;//half the size of the moving images
//----------------------
        }

   public void mouseEntered( MouseEvent e ) { }
   // called when the pointer enters the applet's rectangular area
  
   public void mouseExited( MouseEvent e ) { }
      // called when the pointer leaves the applet's rectangular area

   public void mouseClicked( MouseEvent e ) { }
      // called after a press and release of a mouse button
      // with no motion in between
      // (If the user presses, drags, and then releases, there will be
      // no click event generated.)
  
     public void mousePressed( MouseEvent e ) {  // called after a button is pressed down
    
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
whichButton=e.getButton();
if(whichButton==1){isLeftButtonPressed=true;}
if(whichButton==3){isRightButtonPressed=true;}
      repaint();
      // "Consume" the event so it won't be processed in the
      // default manner by the source which generated it.
      e.consume();
   }

   public void mouseReleased( MouseEvent e ) {  // called after a button is released
     
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
whichButton=e.getButton();
if(whichButton==1){isLeftButtonPressed=false;}
if(whichButton==3){isRightButtonPressed=false;}
      repaint();
      e.consume();
   }
   public void mouseMoved( MouseEvent e ) {  // called during motion when no buttons are down
      mx = e.getX();
      my = e.getY();
      showStatus( "Mouse at (" + mx + "," + my + ")" );
      repaint();
      e.consume();
   }
   public void mouseDragged( MouseEvent e ) {  // called during motion with buttons down
      mx = e.getX();
      my = e.getY();
      showStatus( "Mouse at (" + mx + "," + my + ")" );
      repaint();
      e.consume();
   }

       public void start()
       {
              if (runner == null)
               {
                          runner = new Thread (this);
                         runner.start();
               }
       }

    public void stop() {
        runner = null;
    }

 public void run() {
        Thread thisThread = Thread.currentThread();
        while (runner == thisThread) {
            try {
                thisThread.sleep(25);
            } catch (InterruptedException e){}
           

if(unicorn<2){gBuffer.drawImage(background,0,0,this);}
unicorn=unicorn+1;
if(unicorn>1000){unicorn=0;}

                        //move the snail picture
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if( isLeftButtonPressed == true){
gBuffer.drawImage(snail,mx-cp,my-cp,this);
}
if( isRightButtonPressed == true){
gBuffer.drawImage(snail1,mx-cp,my-cp,this);
}
            repaint();
        }
    }


   //is needed to avoid erasing the background by Java
   public void update(Graphics g)
   {
                paint(g);
   }

   public void paint(Graphics g)
   {
                g.drawImage (Buffer,0,0, this);
   }
}