//:Panorama.java

/** Very simple applet that scrolls a panoramic image accross the
  * another server and then sends the server response back to the

  * @author Mark Crocker
  * @author http://www.markcrocker.com/~mcrocker/
  * @version 0.91
  * Derived from lessons in IBM's Java Applet Workshop
  * (http://www.ibm.com/java/education/buildapplet/)
  */

//<applet code=Panorama width=600 height=130>
//</applet>

import java.awt.*;
import java.awt.event.*;

public class Panorama extends java.applet.Applet
                      implements Runnable, MouseListener {


    Image img, buf;
    MediaTracker mt;
    int iw, ih;
    int shift = 10;
    Graphics bufg, bufb;
    Thread t;
    boolean userPause = false;

    public void init() {

//	System.out.println("In Panorama.init");

	String imgName = getParameter("image");  // from applet tags in html document
	if (imgName==null) imgName = "Panorama.gif";
	img = getImage(getDocumentBase(),imgName);

	mt = new MediaTracker(this);
	mt.addImage(img,0);
	try {
	    mt.waitForAll();
	} catch(InterruptedException e) {
	    e.printStackTrace();
	}

	iw = img.getWidth(null);
	ih = img.getHeight(null);
	buf = createImage(iw+Math.abs(shift),ih);
	bufg = buf.getGraphics();
	bufg.drawImage(img, 0, 0 ,null);

	int ch = 60;
	int cw = ch;
	int cx = 300;
	int cy = 0;
	int eh = ch/5;
	int ew = eh;
	int as = 225;
	int aa = 90;

	// Happy Face
        bufg.setColor(Color.green);
        bufg.drawOval(cx-cw/2, cy+ch/2, ch, cw);
        bufg.fillOval(cx-cw/4-ew/2, cy+ch/2+eh, eh, ew);
        bufg.fillOval(cx+cw/4-ew/2, cy+ch/2+eh, eh, ew);
        bufg.drawArc(cx-3*cw/8, cy+5*ch/8, 3*ch/4, 3*cw/4, as, aa);

//        bufg.drawArc((int) (((double)cx)+Math.cos(as*Math.PI/360.0)*ch),
//		     (int) (((double)cx)+Math.sin(as*Math.PI/360.0)*ch),
//		     3*ch/2, 3*cw/2, as, aa);


	// In case the user reverses direction before the first advance
        // Building a single advance into the init, stops it from leaving a
	// blank space in the image.
//	advance(); // Don't need to do this with properly designed advance

	// Pay attention to mouse actions
	addMouseListener(this);

    }

    public void start() {
//	System.out.println("In Panorama.start");
	t = new Thread(this);
	t.start();
    }

    public void paint(Graphics g) {
//	System.out.println("In Panorama.paint");
	g.drawImage(buf, 0, 0, null);
    }

    public void advance() {
//	System.out.println("In Panorama.advance");
	if (shift > 0) {
	    bufg.copyArea(0, 0, iw, ih, shift, 0);
	    bufg.copyArea(iw, 0, shift, ih, -iw, 0);
	} else {
	    bufg.copyArea(0, 0, Math.abs(shift), ih, iw, 0);
	    bufg.copyArea(Math.abs(shift), 0, iw, ih, -Math.abs(shift), 0);
	}
    }

    public void run() {
//	System.out.println("In Panorama.run");
	while(true) {
	    advance();
	    repaint();
	    try {
		Thread.sleep(33);
	    } catch(InterruptedException e) {
		e.printStackTrace();
		return;
	    }
	}
    }

    public String getAppletInfo() {
//	System.out.println("In Panorama.getAppletInfo");
        String info =  "Title: Panorama\n";
        info = info + "Description: Produces a scrolling panoramic view\n";
        info = info + "Author: Mark Crocker (http://www.markcrocker.com/~mcrocker)\n";
        info = info + "Derived from lessons in IBM's Java Applet Workshop";
        info = info + "(http://www.ibm.com/java/education/buildapplet/)";
	return info;
    }

    public String[][] getParameterInfo() {
//	System.out.println("In Panorama.getParameterInfo");
	String pinfo[][] = {
	    {"dummy",    "n/a",    "no paramters used by this applet"},
	};
	return pinfo;
    }


    public void update(Graphics g) {
//	System.out.println("In Panorama.paint");
	paint(g);
    }

    public void stop() {
//	System.out.println("In Panorama.stop");
	t.stop();
	t = null;
    }

    public void destroy() {
//	System.out.println("In Panorama.destroy");
    }


    /**
     * Pause the thread when the user clicks the mouse in the applet.
     * If the thread has stopped (as in a non-repeat performance),
     * restart it.
     */
    public void mousePressed(MouseEvent event) {
        if (userPause) {
	    t.resume();
	    shift = -shift;
	} else {
	    t.suspend();
	}
	userPause = !userPause;
    }

    public void mouseClicked(MouseEvent event) {
    }

    public void mouseReleased(MouseEvent event) {
    }

    public void mouseEntered(MouseEvent event) {
    }

    public void mouseExited(MouseEvent event) {
    }

}