Results 1 to 1 of 1
Thread: Loading image in applet
- 08-06-2009, 09:02 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 21
- Rep Power
- 0
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);
}
}
Similar Threads
-
Dynamic image loading in jsp
By ramakrishna k m in forum JavaServer Pages (JSP) and JSTLReplies: 5Last Post: 10-10-2011, 06:08 AM -
Rambling about image loading...
By Zamppa in forum Java AppletsReplies: 0Last Post: 04-14-2009, 03:55 PM -
Need Help!!! Loading Image Into A Jframe
By lolshooter in forum New To JavaReplies: 3Last Post: 01-06-2009, 07:14 AM -
loading image into applet
By balaram in forum Java AppletsReplies: 1Last Post: 11-06-2008, 02:13 PM -
Loading An Image Help Please!
By shaungoater in forum Java 2DReplies: 2Last Post: 01-09-2008, 08:14 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks