/*********************************************************************************************/
/*                                                                                           */
/*  Java Image Demo - XOR Pattern                                                            */
/*  Auth: bkenwright@xbdev.net                                                               */
/*  URL: www.xbdev.net                                                                       */
/*                                                                                           */
/*  XOR Pattern with pixels on java 1.1 and later!                                           */
/*  This is a cool effect, and its simple, and dang do I like simple code :)                 */
/*  The actual code that does the pattern is in the function called Pattern!...the Screen    */
/*  class does our direct rendering to our back buffer :)                                    */
/*                                                                                           */
/*********************************************************************************************/

import java.awt.image.*;
import java.awt.*;
import java.applet.*;

public class xor_pixels extends Applet implements Runnable
{
	private Thread m_thread        = null;
  public Screen scr;


	public synchronized void update(Graphics g)
	{
	    int[] pixels = scr.GetPixels();
	    int w=scr.GetWidth();    int h=scr.GetHeight();
			Pattern(pixels, w, h );
			scr.Render(g);
			//paint(g);
	}// End update(..)


	
	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()
	{
	    scr = new Screen(320,320);
	    
			// 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 > 25 )
			    {
			       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( "Starting XOR Graphics Demo\n" );
	}// End of start()
	
}// End of Applet


/*********************************************************************************************/
/*                                                                                           */
/*  class Screen                                                                             */
/*  Well its the fastest method to plot pixels using MemoryImageSource...and to access your  */
/*  array of pixel data directly...but it sure makes your setup applet code look complicated */
/*  and hides the real reason!  So I've done a sort of back buffer screen class, which we    */
/*  can use to render pixels fastly....for our cool effects...and its all in there...settup  */
/*  and rendering.                                                                           */
/*                                                                                           */
/*********************************************************************************************/
// Few tiny comments
// We need to extend from Applet so we can use, two api's.  createImage(..) and drawImage(..)

class Screen extends Applet
{
   Image m_image;
   int m_pixels[];
   
   int m_width=0;
   int m_height=0;

   MemoryImageSource m_p; 
   public Screen(int width, int height)
   {
     m_pixels = new int[width*height];
     m_width = width;
     m_height = height;
     // Init MemoryImageSource - pixels will be in 32bit ARGB format
		 DirectColorModel dcm=new DirectColorModel(32, 0xff0000, 0xff00, 0xff);
     m_p = new MemoryImageSource(/*width*/m_width,/*height*/m_height,dcm,m_pixels,0,/*width*/m_width);
     m_p.setAnimated(true);
     m_p.setFullBufferUpdates(true);
     m_image = createImage(m_p);
   }// End CScreen(..) constuctor

   public void Render(Graphics g)
   {
      m_p.newPixels(0,0,/*width*/m_width,/*height*/m_height);
      g.drawImage(m_image,0,0, this);
   }// End Render(..)
   
   public int[] GetPixels()
   {
      return m_pixels;
   }// End GetPixels()
   
   
   public int GetWidth() { return m_width; };
   public int GetHeight(){ return m_height; };

}// End Screen(..)


  // Overwrite imageUpdate
  /*
  public boolean imageUpdate(Image image, int i1, int j1, int k, int i2, int j2) {
    return true;
  }
  */
  
  
  

