-
Loading image in applet
I'm still having the problem of with image. The code below cannot load and get image but i already call the function img = new Image(getClass().getResource("jawi.gif")). I really don't know where are the mistake of this code.
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
public class Binary_Image extends JApplet{
Image img;
int iw,ih;
int pixels[];
int w,h;
int hist[] = new int[256];
int max_hist = 0;
int cnt1=0,cnt=0,cnt2=0,h1,h2,h3,c=0;
public void init()
{
try
{
//img = getImage(getDocumentBase(),getParameter("jawi.img" ));
//img = getImage(getDocumentBase(), "jawi.gif");
img = new Image(getClass().getResource("jawi.gif"));
// add(new JLabel(new Image (img)));
//MediaTracker t = new MediaTracker(this);
//t.addImage(img,0);
//t.waitForID(0);
img = Toolkit.getDefaultToolkit().getImage("jawi.gif");
iw = img.getWidth(this);
ih = img.getHeight(this);
pixels = new int[iw*ih];
PixelGrabber pg = new PixelGrabber(img,0,0,iw,ih,pixels,0,iw);
pg.grabPixels();
} catch(InterruptedException e) {};
for(int i=0;i<iw*ih;i++)
{
int p = pixels[i];
int r = (p>>16) & 0xff;
int g = (p>>8) & 0xff;
int b = (p) & 0xff;
int k = (int)(.56*g + .33*r + .11 * b);
//pixels[i] = (0xff000000 | k<<16 | k<<8 |k);
cnt++;
if(k<36)
{
cnt1++;
pixels[i]=(255 << 24)|(0 << 16)|(0 << 8)|0;
}
else
{
cnt2++;
pixels[i]=(255 << 24)|(255 << 16)|(255 << 8)|255;
}
}
System.out.println("Total no of pixels ="+cnt);
System.out.println("Total no of black pixels ="+cnt1);
System.out.println("Total no of white pixels ="+cnt2);
img = createImage(new MemoryImageSource(iw,ih,pixels,0,iw));
}
public static void main(String[]args)
{
JFrame frame = new JFrame ();
Binary_Image applet = new Binary_Image();
frame.add(applet,BorderLayout.CENTER);
applet.init();
frame.setSize(500,300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setVisible(false);
}
}