Results 1 to 9 of 9
- 08-05-2009, 05:29 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 21
- Rep Power
- 0
How to load image using Media Tracker
i have problem with my code.Plz somebody help me. the error of this code is cannot find constructor MediaTracker.
Java Code:import java.awt.*; import java.awt.image.*; import java.awt.Color; import java.awt.Image; import java.awt.Graphics; import javax.swing.*; import java.lang.Object; import java.util.*; import java.awt.BorderLayout; import java.awt.MediaTracker; import java.awt.Toolkit; public class Binary_Image { public static void main(String[]arg) { Image img; int iw,ih; int pixels[]; int hist[] = new int[256]; int max_hist = 0; int cnt1=0,cnt=0,cnt2=0,h1,h2,h3,c=0; { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int screenWidth = screenSize.width; int screenHeight = screenSize.height; try { MediaTracker tracker = new MediaTracker(this); tracker.addImage(img,0); try {tracker.waitForID(0);} catch (InterruptedException e){} img= Toolkit.getDefaultToolkit().getImage("jawi.gif"); iw = img.getWidth(null); ih = img.getHeight(null); 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); } } }Last edited by Fubarable; 08-05-2009 at 02:50 PM. Reason: code tags added
- 08-05-2009, 10:36 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
The constructor for MediaTracker takes a java.awt.Component. Your class doesn't extend Component, so it's giving you the compilation error.
- 08-06-2009, 05:56 AM #3
Member
- Join Date
- Aug 2009
- Posts
- 21
- Rep Power
- 0
Thanks Toll for your advise.I really appreciate. But i have another error when i extends component. The error is the non-static variable cannot references from a static content.
- 08-06-2009, 09:06 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
I assume the error has a bit more detail than that (like what non-static variable, and which line), but it's the error you get when you reference a non-static from a static method. Something like:
Java Code:public class Blah { private String blah; // non-static variable. public static void doSomething() // static method { blah = "This won't work"; // This will give you a compilation error as blah is non-static. // Can only be accessed by an instance of Blah. } }
- 08-06-2009, 11:03 AM #5
Member
- Join Date
- Aug 2009
- Posts
- 21
- Rep Power
- 0
i'm try to do another way.. it can run properly but cannot invoke the image..i already put the jawi.gif in same folder of binary_image class.thanks.
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.image.*; import java.awt.Image.*; 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 = Toolkit.getDefaultToolkit().getImage("jawi.gif"); iw = img.getWidth(null); ih = img.getHeight(null); 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); 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(newMemoryImageSource(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_CLOSE); frame.setVisible(true); } }Last edited by syarizma; 08-06-2009 at 12:14 PM. Reason: editing the format of code
- 08-06-2009, 11:17 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Can you edit that and stick code tags around it? Repaste the code between the tags, since you'ce already lost your formatting.
- 08-06-2009, 12:15 PM #7
Member
- Join Date
- Aug 2009
- Posts
- 21
- Rep Power
- 0
ok..i already done it.
- 08-06-2009, 12:26 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
You want to be using this.getClass().getResource(<filename>), which will give you a URL which will point to the file. Use the URL in the getImage() method. If the file is located with the Binary_Image.class then it should find it.
- 08-07-2009, 04:12 AM #9
Member
- Join Date
- Aug 2009
- Posts
- 21
- Rep Power
- 0
Similar Threads
-
How to load image using Media Tracker
By syarizma in forum Advanced JavaReplies: 0Last Post: 08-05-2009, 05:21 AM -
Can not load image in SWT/Eclipse
By janice in forum SWT / JFaceReplies: 4Last Post: 10-03-2008, 04:53 PM -
[SOLVED] Load image into a JSP
By jazz2k8 in forum New To JavaReplies: 0Last Post: 05-08-2008, 11:33 AM -
Help with load image
By trill in forum New To JavaReplies: 1Last Post: 08-01-2007, 07:16 AM -
GPS Tracker 0.2
By JavaBean in forum Java SoftwareReplies: 0Last Post: 07-19-2007, 03:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks