/*********************************************************************************************/
/*                                                                                           */
/*  Java Image Tutorials - Tut x.x                                                           */
/*  Auth: bkenwright@xbdev.net                                                               */
/*                                                                                           */
/*  Setting pixels with java 1.1 and later!                                                  */
/*  Demonstrating the FASTEST way to plot pixels and create dynamic effects!                 */
/*                                                                                           */
/*********************************************************************************************/

import java.awt.image.*;
import java.awt.*;
import java.applet.*;

public class xor_settingpixels_other extends Applet implements Runnable
{
	private Thread m_thread        = null;
  Image myImage;
  int pixels[] = new int[320 * 320];

  MemoryImageSource p;  
  public void init() 
  {
     // Init MemoryImageSource - pixels will be in 32bit ARGB format
		 DirectColorModel dcm=new DirectColorModel(32, 0xff0000, 0xff00, 0xff);
     p = new MemoryImageSource(/*width*/320,/*height*/320,dcm,pixels,0,/*width*/320);
     p.setAnimated(true);
     p.setFullBufferUpdates(true);
     myImage = createImage(p);
  }// End of init(..)
  

  int iCounter = 0;
  public void paint(Graphics g) 
  {
	    g.drawImage(myImage,0,0,this);
		  g.setColor(Color.red);
			g.drawString("Applet Counter: " + iCounter , 20, 25);
			iCounter++;		
  }// End of paint(..)

  // Overwrite imageUpdate
  /*
  public boolean imageUpdate(Image image, int i1, int j1, int k, int i2, int j2) {
    return true;
  }
  */
  
	public synchronized void  update(Graphics g)
	{
	    p.newPixels(0,0,/*width*/320,/*height*/320);
			//ClearScreen(pixels, 320, 320, 0xff00ff00); // Clear to green
			Pattern(pixels, 320, 320 );
			paint(g);
	}// End update(..)


  // Simple way to clear the screen!  Not used here, but it shows how to
  void ClearScreen(int pix[], int w, int h, int col)
  {
     for(int i=0; i< w*h; i++ )
     {
        pix[i] = col;
     }//End for loop
  }//End ClearScreen(..)
	
	
	int xx = 0;
	void Pattern(int pix[], int width, int height)
	{
			int x, y;
			//generate a pretty colour background on the screen
      for (y=0; y<height; y++)
      {
            for (x=0; x<width; x++)
            {
                int cc = ((x+xx)^y)&0xFF;
                int r = (cc>>16)&0xFF;
                int g = (cc>>8)&0xFF;
                int b = (cc)&0xFF ;
                cc = ( 0xff<<24 | 
                       r << 16 |
                       g << 8 |
                       b );
                pix[y*width + x] = cc;       //yay a XOR pattern!!! :D
            }// End inner loop
      }// End outer loop
      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


// Excellent reference:
// http://applet3d.free.fr/howto/lesson05.htm


