Crop Images into pieces for java applet

By using CropImageFilter class, images can be cropped easily with few lines. According to Java docs, parameter of CropImageFilter constructor is

CropImageFilter(int x, int y, int w, int h)

First, load the image from folder. Here’s is the method I prepared.

private Image loadImage(String img){
    return getImage(app.getDocumentBase(),"img/"+img);
}

For eclipse IDE, I put my images file inside the src/img package.

After load the image, let’s say “image_want_chop.png” is loaded and chop it with following code.

Image originalImage = loadImage("image_want_chop.png");
ImageFilter imageFilter = new CropImageFilter(x,y,width,height);
Image choppedImage = createImage(new FilteredImageSource(originalImage.getSource(),                        imageFilter ));

Thank you for reading.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s