• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do I display JPEG, crop it, display cropped JPEG and undo crop.

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to learn how to display a JPEG, crop it using a mouse, re display the cropped JPEG and undo the crop. Can anyone refer me to example code or a book or a tutorial that will teach me this. I would like to use JAI.
Thanks,
Warren Bell
Netricks
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Warren,
There is a good discussion of this in the book Java 2D Graphics from O'Reilly. I think I can give you the gist of it:
// you have to import JPEG support along
// with the usual suspects
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;
import java.io.*
// image reference
private BufferedImage myImage;
// a method to import a certain JPEG image
// might look like this
private void loadArrowImage(){
try
{
String filename = "MyPicture.jpg";
FileInputStream in = new FileInputStream( filename );
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder( in );
myImage = decoder.decodeAsBufferedImage();
in.close();
}
catch( ImageFormatException ife )
{
System.out.println("Image source file invalid.");
}
catch( FileNotFoundException fnfe )
{
System.out.println("Could not locate image file.");
}
catch( IOException ioe )
{
System.out.println("Error reading image file.");
}
}

// you display it by passing it to the graphics object
public void paint(Graphics g) {
// cast to Graphics2D object
Graphics2D g2 = (Graphics2D)g;
// call a drawImage method
g2.drawImage( myImage, 0, 0, null);
// or whatever works best for you
}
As far as cropping the image, you just create a Shape object to use as a clipping shape. The call looks like:
// in paint() method
g2.clip( myClippingShape );
g2.drawImage( myImage, 0, 0, null );
Hope this helps!
Darrin
 
Warren Bell
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. I read up on it and basically did what you explained.
Warren Bell
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic