Using PixelGrabber


The html file should have the applet tag for the class file

a1.html

<applet code="zzz" width=350 height=250></applet>


The import statements for the program codes are

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


zzz.java
public class zzz extends Applet
{       Image b;
        public void init()
        {       b=getImage(getDocumentBase(), "borderex1.gif");
        }
        public void update(Graphics g)
        {       paint(g);
        }
        public void paint(Graphics g)
        {       g.drawImage(b, 0, 0, this);
        }
}
zzz.java
public class zzz extends Applet
{       Image o;
        Graphics s;
        public void init()
        {       o=createImage(200,300);
                s = o.getGraphics();
                s.setColor(Color.red);
                s.drawString("hello",1,10);
        }
        public void update(Graphics g)
        {       paint(g);
        }
        public void paint(Graphics g)
        {       g.drawImage(o, 0, 0, this);
        }
}
zzz.java
public class zzz extends Applet
{       Image b,o;
        Graphics s;
        public void init()
        {       o=createImage(200,300);
                b=getImage(getDocumentBase(), "borderex1.gif");
                s = o.getGraphics();
        }
        public void update(Graphics g)
        {       paint(g);
        }
        public void paint(Graphics g)
        {       s.drawImage(b,0,0,this);
                g.drawImage(o,0,0,this);
        }
}
zzz.java
public class zzz extends Applet
{       Image b,o;
        Graphics s;
        int a[]=new int[800*800];
        PixelGrabber r;
        int j;
        public void init()
        {       b = getImage(getDocumentBase(), "borderex1.gif");
        }
        public void paint(Graphics g)
        {       g.drawImage(b,0,0,this);
                if ( j == 1 )
                        g.drawImage(o,250,200,this);
        }
        public boolean mouseDown(Event f,int x,int y)
        {       for(int i=0;i<160000;i++)
                        a[i]=0;
                r=new PixelGrabber(b,0,0,x,y,a,0,x);
                b.getSource().addConsumer(r);
                try r.grabPixels();
                catch(Exception e)
                {       System.out.println("err");
                }
                o=createImage(new MemoryImageSource(x,y,a,0,x));
                j=1;
                repaint();
                return true;
        }
}
zzz.java
public class zzz extends Applet
{       int ctr;
        int a[]=new int[800*800];
        Image b,o;
        Graphics s;
        int m,n,q,p;
        PixelGrabber r;
        int j;
        public void init()
        {       b = getImage(getDocumentBase(), "borderex1.gif");
        }
        public void paint(Graphics g)
        {       g.drawImage(b,0,0,this);
                if ( j==1)
                        g.drawImage(o,250,200,this);
        }
        public boolean mouseDown(Event f,int x,int y)
        {       if (ctr == 0)
                {       ctr++;
                        m = x;
                        n = y;
                        j=0;
                }
                else
                {       q = x;
                        p = y;
                        for(int i=0;i<160000;i++)
                                a[i]=0;
                        r=new PixelGrabber(b,m,n,q-m,p-n,a,0,x);
                        b.getSource().addConsumer(r);
                        try r.grabPixels();
                        catch(Exception e)
                        {       System.out.println("err");
                        }
                        o=createImage(new MemoryImageSource(x,y,a,0,x));
                        j=1;
                        repaint();
                        ctr = 0;
                }
                return true;
        }
}

Your comments , feedback , suggestions are most welcomed. Let us know how we can improve the tutorial.

Moving on to the next chapter which is on how to capture an area of the image (ImageMap) and do a certain job

Back to the Java Page


Vijay Mukhi's Computer Institute
B-13, Everest Building, Tardeo, Bombay 400 034, India.
http://www.neca.com/~vmis
e-mail : vmukhi@giasbm01.vsnl.net.in
1