/*********************************************************************************************/
/*                                                                                           */
/*  Java Image Tutorials - Tut x.x                                                           */
/*  Auth: bkenwright@xbdev.net                                                               */
/*                                                                                           */
/*  Setting pixels with java 1.1 and later!                                                  */
/*  Added dynamic effect so somethign happens with time.                                     */
/*                                                                                           */
/*********************************************************************************************/

import java.awt.image.*;
import java.awt.*;
import java.applet.*;

public class xor_settingpixels_rect extends Applet implements Runnable
{
	private Thread m_thread = null;
  Image myImage;
		
  int m_iCounter=0;
  
  public void init() 
  {
		int x = 320;
		int y = 320;
		myImage = createImage(x,y);
  }// End of init(..)
  

  public void paint(Graphics g) 
  {
			g.drawImage(myImage,0,0,this);
		
			m_iCounter++;
			g.setColor(Color.red);
			g.drawString("Applet Counter: " + m_iCounter, 20, 25);
  }// End of paint(..)

	public synchronized void update(Graphics g)
	{
	    Pattern(myImage, 320, 320 );
			paint(g);
	}// End update(..)

	
	int xx = 0;
	void Pattern(Image image, int width, int height)    //yay a XOR pattern!!! :D
	{
			int x, y;
			Graphics g = image.getGraphics( );
			//generate a pretty colour background on the screen
      for (y=0; y<height; y++)
      {
            for (x=0; x<width; x++)
            {
                int c = ((x+xx)^y)&0xFF;       
                Color color = new Color(  (c>>16)&0xFF,
  														            (c>>8)&0xFF,
  														            (c)&0xFF );
  							g.setColor( color );
                g.fillRect( x, y, 1, 1 );
            }
      }
      xx++;
	
	}// End Pattern(..)
  
  
  public void run()
	{
			// Remember the starting time
			long thenTime = System.currentTimeMillis();
			while (true) 
			{
			    long nowTime = System.currentTimeMillis();
			    long difTime = nowTime - thenTime;
			    
			    // I've set the delay to 0, so its pushing for the MAX fps, you would
			    // set a value in there to limit for example to 25-50 fps refresh rate
			    if( difTime > 0 )
			    {
			       thenTime = nowTime;
			       // This method provides better updates, instead of filling the
			       // message queue up
			       Graphics g=this.getGraphics();
			       update(g);
			    }
			}// end while loop

	}// End of run() method
	
	
	public void start()
	{
		if(m_thread == null)
    {
			m_thread = new Thread(this);
			m_thread.setPriority(Thread.MAX_PRIORITY);
			m_thread.start();
		}
		System.out.println( "We are about to start.. \nprogram running" );
	}// End of start()
	
}// End of Applet




/*********************************************************************************************/
/*                                                                                           */
/*  For Reference                                                                            */
/*  Not used here, but where used for testing etc in other apps - demonstrate how to build   */
/*  simple setpixel functions.                                                               */
/*                                                                                           */
/*********************************************************************************************/

/*
  public void setPixel(Image image, int x, int y, Color color ) 
  {
    Graphics g = image.getGraphics( );
    g.setColor( color );
    g.fillRect( x, y, 1, 1 );
    g.dispose( );
  }// End of setPixel(..) Color
  
  public void setPixel(Image image, int x, int y, int c ) 
  {
  	Color color = new Color(  (c>>16)&0xFF,
  														(c>>8)&0xFF,
  														(c)&0xFF );
  	setPixel(image, x, y, color);
  }// End of setPixel(..) int
  */

