/* 
	Simple.java

	java basics, 15-19.11.1999, juhuu@katastro.fi
*/

import java.applet.Applet;
import java.awt.*;
import java.net.*;

public class Simple extends Applet implements Runnable {
  Image		imageDoubleBuffer;
  Graphics	gDoubleBuffer;
  Thread	engine = null;

  int		x, y;
  int		mousex, mousey;
  boolean 	mouse_down;
  Image 	imgPic;
  Image 	imgBackground;

  Image		imageTextBuffer;
  Graphics	gTextBuffer;
  String[]	sTexts = {"kala", "fisu", "hippo norsu"};
  int		textindex;
  boolean	text_printed;

  public void init() {
      // initialize double buffering
      imageDoubleBuffer = createImage(size().width, size().height);
      gDoubleBuffer = imageDoubleBuffer.getGraphics();
      gDoubleBuffer.setColor(Color.white);
      gDoubleBuffer.fillRect(0, 0, size().width, size().height);

      mousex 	= 0;
      mousey 	= 0;

      x 	= 0;
      y 	= 0;

      mouse_down = false;

      // let's load an image

      imgPic = null;

      String sBackground = "background.jpg";
      String sPic = "test.gif";

      MediaTracker med = new MediaTracker(this);

      imgPic = getImage(getDocumentBase(), sPic);
      med.addImage(imgPic, 0);

      imgBackground = getImage(getDocumentBase(), sBackground);
      med.addImage(imgBackground, 1);

      try
      {
  	  med.waitForAll();
      }	catch (InterruptedException e) {
      }

      // initialize text buffer
      imageTextBuffer = createImage(size().width, size().height);
      gTextBuffer = imageTextBuffer.getGraphics();
      gTextBuffer.setColor(Color.white);
      gTextBuffer.fillRect(0, 0, size().width, size().height);

      // set font for the text graphics buffer
      int nStyle = Font.BOLD;
      int nSize = 20;
      String nFont = "Arial";
      Font font = new Font(nFont, nStyle, nSize);
      gTextBuffer.setFont(font);

      // print the first text
      textindex = 0;
      printnext();
      text_printed = true;
  }

//----

  public void printnext() {
	if (imgBackground != null)
	{
		gTextBuffer.drawImage(imgBackground, 0, 0, null);
	} else {
	    	gTextBuffer.setColor(Color.white);
	    	gTextBuffer.fillRect(0, 0, size().width, size().height);
	}

    	gTextBuffer.setColor(Color.yellow);

	gTextBuffer.drawString(sTexts[textindex], 50, 100);

	textindex++;
	if(textindex >= sTexts.length)
	{
		textindex = 0;
	}
  }

//----------------------------------------------------

  public void paint(Graphics g) {
	// clear the screen
//    	gDoubleBuffer.setColor(Color.white);
//    	gDoubleBuffer.fillRect(0, 0, size().width, size().height);

	// print text
	gDoubleBuffer.drawImage(imageTextBuffer, 0, 0, null);

	if(mouse_down)
	{
		if(!text_printed)
		{
			printnext();
		}

		text_printed = true;
	} else {
		text_printed = false;
	}

	// draw all the stuff from the double buffer to the screen
	g.drawImage(imageDoubleBuffer, 0, 0, null);				
  }


//----------------------------------------------------


  // start the applet
  public void start() {
      if (engine == null) {
          engine = new Thread(this);
          engine.start();
      }
      showStatus(getAppletInfo());
  } 

  // stop the applet
  public void stop() {
     if (engine != null && engine.isAlive())
     {
         engine.stop();
     } 
     engine = null; 
  }

  public void run()
  {                  
     while (true)
     {
         try
         {
             repaint();
             Thread.sleep(10);
         } catch (InterruptedException e) {
             stop();
         }
     }                                  
  }

  public void update(Graphics g) {    
	paint(g);
  }

  // print to the console & browserstatus
  private void print(String s) {
    System.out.println(s); 		// console
    showStatus(s);         		// browser status  
  }

//---

  public boolean mouseDown(Event e, int x, int y) {
	mouse_down = true;
	return true;  
  }

  public boolean mouseUp(Event e, int x, int y) {
	mouse_down = false;
	return true;  
  }

  public boolean mouseMove(Event e, int x, int y)
  {
	mousex = x;
	mousey = y;
	return true;  
  }

  public boolean mouseDrag(Event e, int x, int y)
  {
	mousex = x;
	mousey = y;
	return true;  
  }


//---

}
