Tuesday, November 4, 2008

IndexImageApplet

import java.applet.*;
import java.awt.*;
import java.awt.image.*;


public class IndexImageApplet extends Applet {
Image image;
MemoryImageSource source;

int w = 16*16;
int h = 16;
int pix[] = new int[w * h];

byte[] R =new byte[16];
byte[] G =new byte[16];
byte[] B =new byte[16];

IndexColorModel indexModel;

public void init()
{
buildColorPallet();
setImageColor();

indexModel= new IndexColorModel(4,16,R,G,B);
source = new MemoryImageSource(w, h, indexModel,pix, 0, w);
image = createImage(source);

}


public void paint(Graphics g){
g.drawImage(image,5,20,this);
}


public void buildColorPallet(){
for (byte i=0; i<16; i++){
R[i]=(byte)(16*i);
G[i]=(byte)(16*i);
B[i]=(byte)(16*i);
}
}

public void setImageColor(){
int index=0;
for (int x=0; x < w; x++){
for (int y=0; y < h; y++){
index=y*w+x;
pix[index]=(x/16);
}
}
}

}