Results 1 to 9 of 9
  1. #1
    syarizma is offline Member
    Join Date
    Aug 2009
    Posts
    21
    Rep Power
    0

    Cool 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

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    The constructor for MediaTracker takes a java.awt.Component. Your class doesn't extend Component, so it's giving you the compilation error.

  3. #3
    syarizma is offline Member
    Join Date
    Aug 2009
    Posts
    21
    Rep Power
    0

    Cool

    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.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    Quote Originally Posted by syarizma View Post
    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.
    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.
        }
    }

  5. #5
    syarizma is offline Member
    Join Date
    Aug 2009
    Posts
    21
    Rep Power
    0

    Cool

    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

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    Can you edit that and stick code tags around it? Repaste the code between the tags, since you'ce already lost your formatting.

  7. #7
    syarizma is offline Member
    Join Date
    Aug 2009
    Posts
    21
    Rep Power
    0

    Default

    ok..i already done it.

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    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.

  9. #9
    syarizma is offline Member
    Join Date
    Aug 2009
    Posts
    21
    Rep Power
    0

Similar Threads

  1. How to load image using Media Tracker
    By syarizma in forum Advanced Java
    Replies: 0
    Last Post: 08-05-2009, 05:21 AM
  2. Can not load image in SWT/Eclipse
    By janice in forum SWT / JFace
    Replies: 4
    Last Post: 10-03-2008, 04:53 PM
  3. [SOLVED] Load image into a JSP
    By jazz2k8 in forum New To Java
    Replies: 0
    Last Post: 05-08-2008, 11:33 AM
  4. Help with load image
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 07:16 AM
  5. GPS Tracker 0.2
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-19-2007, 03:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •