Here is some i scooped of the internet... supply a "test.gif" (small one, it displays the cropped pic at X = 70 and Y = 70, you can change that in the paint method if you'd like)
import java.applet.Applet;
import java.awt.*;
import java.awt.image.*;
public class CropImage extends Applet implements Runnable {
Image the_picture, cropped_picture;
boolean image_ready;
Thread the_thread;
MediaTracker the_tracker;
public void start() {
if (the_thread == null) {
the_thread = new Thread(this);
the_thread.start();
}
}
public void stop() {
the_thread.stop();
}
public void run() {
the_thread.setPriority(Thread.MIN_PRIORITY);
while (true) {
repaint();
try {
the_thread.sleep(3000);
} catch(InterruptedException e) {};
}
}
public void init() {
CropImageFilter c_i_filter;
//Define a MediaTracker to monitor the completion status of an image
//You need to supply a component as the argument.
//In this case the applet
// is used
the_tracker = new MediaTracker(this);
image_ready = false;
the_picture = getImage(getDocumentBase(),"test.gif");
//tell MediaTracker to monitor the progress of the image and
//assign the image
//the index of 0--the index value is arbitrary
the_tracker.addImage(the_picture,0);
//Define an image cropping filter
c_i_filter = new CropImageFilter(0,0,32,32);
//Apply the filter to the image and make a new image of the cropped area
cropped_picture = createImage(new FilteredImageSource(
the_picture.getSource(),c_i_filter));
//Monitor the cropped image to see when it's done
the_tracker.addImage(cropped_picture,1);
}
public void paint (Graphics g) {
//Wait till both images are ready
if(the_tracker.checkID(0,true) & the_tracker.checkID(1,true)) {
image_ready = true;
//g.drawImage(the_picture,0,0,this);
g.drawImage(cropped_picture,70,70,this);
} else {
System.out.println("Waiting for image to fully load");
}
}
}