How do you crop an image?

I thought I got it and again, I failed.
public void actionPerformed(ActionEvent evt) {
        if (evt.getActionCommand().toLowerCase().trim().equals("crop")) {
            cropPanel.setImage(
                    (BufferedImage)createImage(
                    new FilteredImageSource(cropPanel.getImage().getSource(),
                    new CropImageFilter((int)cropPanel.getClip().getX(),
                    (int)cropPanel.getClip().getY(),
                    (int)cropPanel.getClip().getWidth(),
                    (int)cropPanel.getClip().getHeight()))));
            repaint();
            hasCropped = true;
    }This constantly throws the following exception:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage
        at com.ppowell.tools.imagetools.ImageCropper.actionPerformed(ImageCropper.java:180)I was under the impression that I'm using java.awt.Image, where did sun.awt.image.ToolkitImage come from? I can't figure out how to cast this correctly either, what do I do?
Thanx
Phil

Got it, thanx
         * Perform crop
         * <a href="http://forum.java.sun.com/thread.jspa?threadID=627125&messageID=3587532">Reference</a>
        public void crop() {
            Rectangle r = ImageCropper.this.cropPanel.getClip();
            if (!this.isClipInsideImage(r)) /* AVOID RasterFormatException */ {
                return;
            // offset clip from view to raster model
            int x = (ImageCropper.this.cropPanel.getWidth() -
                    ImageCropper.this.cropPanel.getImage().getWidth()) / 2;
            int y = (ImageCropper.this.cropPanel.getHeight() -
                    ImageCropper.this.cropPanel.getImage().getHeight()) / 2;
            BufferedImage clippedImage =
                    ImageCropper.this.cropPanel.getImage().getSubimage(
                    (int)r.getX() - x,
                    (int)r.getY() - y,
                    (int)r.getWidth(),
                    (int)r.getHeight());
            ImageCropper.this.cropPanel.setImage(clippedImage);
            ImageCropper.this.cropPanel.setup();
            ImageCropper.this.validate();
         * Determine if the {@link java.awt.image.BufferedImage} has a legitimate {@link java.awt.Rectangle} clip
         * To avoid {@link java.awt.image.RasterFormatException}
         * @param r {@link java.awt.Rectangle}
         * @return {@link java.lang.Boolean}
         * <a href="http://forum.java.sun.com/thread.jspa?threadID=627125&messageID=3587532">Reference</a>
        private boolean isClipInsideImage(Rectangle r) {
            int w = ImageCropper.this.cropPanel.getWidth();
            int h = ImageCropper.this.cropPanel.getHeight();
            int imageWidth = ImageCropper.this.cropPanel.getImage().getWidth();
            int imageHeight = ImageCropper.this.cropPanel.getImage().getHeight();
            int x = (w - imageWidth) / 2;
            int y = (h - imageHeight) / 2;
            if ((int)r.getX() >= x &&
                    (int)r.getX() + (int)r.getWidth() <= x + imageWidth &&
                    (int)r.getY() >= y &&
                    (int)r.getY() + (int)r.getHeight() <= y + imageHeight) {
                return true;
            return false;
        }

Similar Messages

  • How do you crop an image, then modify that cropping?

    This takes the cake for simpleton question of the day.
    Cropping an image in Illustrator or in InDesign is a breeze.
    But how do you control it in Fireworks?
    I make a selection with the marquee tool, go to edit and choose crop selected bitmap.
    Now, do you mean to tell me that there is no way to modify that crop after the fact?
    Can you at least delete the crop without deleting the image?
    This cant be this unintuitive.
    Surely Im missing something...

    Masks, yes, masks are great! If you just want to hide parts of your image from view, create a mask. Masks can be other bitmaps or vector objects. Create and position your mask object over your original bitmap. Select both then Modify > Mask > Group as Mask. You can easily undo the masking. White areas in a mask reveal the underlying object. Black or transparent areas in the mask cause your original image to be transparent for those pixels. Grey or semi-transparent produce a proportionally transparent result.
    There's also the Paste Inside option, where you can paste your bitmap into a vector shape. The nice thing there is that you can move your bitmap around. (I'm pretty sure, anyway. I don't use this feature.)
    Anyway. You have lots of options to remove or hide pixels. The approach you take depends on your needs and preferences.
    If you have an image to share and a goal to achieve, we might be able to advise you better.

  • How do you crop and save a cropped image

    How do you crop and save a cropped image in PS Touch?

    This forum does not deal with any of the Touch applications.
    Try here:
    http://forums.adobe.com/community/photoshop_touch_for_phone
    or here:
    http://forums.adobe.com/community/creative_cloud_touch_apps/adobe_photoshop_touch

  • How do I crop an image in photoshop and place it in an illustrator file without having a white backg

    How do I crop and image in photoshop and place it in an illustrator file without having a white background ?

    Could you post the image (or a lores version of it) on this Forum?

  • How do I crop an image to 3.5 x 5

    I'm trying to match the size and aspect ratio of an older photo.  The old photo was printed at 3.5" x 5.0".
    How do I crop an image with Aperture to 3.5x5.0?
    Thanks.

    When you crop, the only control is the aspect ratio. You can crop an image with a custom ratio by selecting "custom" at the bottom of the aspect ratio popup menu in the Crop HUD that appears while in the Crop command.
    The size only matters when you print the image. In the print dialog you can set the size in inches of the printed image. The dpi of the printed photo will depend on the pixel size of the image divided by the printed size in inches.
    If you want to control the exact dpi of the printed image, you'll need a pixel editor such as Photoshop.

  • How do you paste an image into a rectangular marqee forcing it to resize into the marqee?

    How do you paste an image into a rectangular marqee forcing it to resize into the marqee?

    Not sure this would address your issue, but the Crop Tool works that way: set the width and height, plus resolution, and Crop does everything. You could then paste the result into another file if that completes your process.

  • How do you shuffle the image order when creating a new slideshow in apterture 3?  i would like to do this automatically when creating a new slideshow.  i see how you do it when you just play a slideshow.

    how do you shuffle the image order when creating a new slideshow in apterture 3?  i would like to do this automatically when creating a new slideshow.  i see how you do it with presets when you just play a slideshow, but i don't see an option to randomly shuffle the slide order when you create a new slideshow.  i know you can sort it by different fields, but i want it to be random.  thanks.

    If you want to rearrange images in random order you can try an AppleScript:
    retrieve a list of selected images from Aperture
    shuffe the list properly
    create an album in Aperture and add the images from the list to the album (make sure that the album set to be orederd manually)
    Here  is a sample script that shuffles the selected images and displays them in random order in Full Screen Mode:
    on removeItem(ims, i)
      -- remove the item at position "i" from a list ims
              if ims is {} then return {}
              if (length of ims is 1) then return {}
              if i < 2 then return rest of ims
              if (i = length of ims) then return (items 1 thru (i - 1) of ims)
              if i > (length of ims) then return ims -- should be error?
              return (items 1 thru (i - 1) of ims) & (items (i + 1) thru (length of ims) of ims)
    end removeItem
    on shuffle_items(ims)
      -- shuffle the items of the list "ims" randomly
              local numitems, ims_shuffled, nextrest, nextpick, i
              set numitems to length of ims
              if length of ims < 2 then return ims
              set ims_shuffled to {}
              set nextrest to ims
              repeat until nextrest is {}
                        set i to (random number (numitems - 1)) + 1
                        set nextpick to item i of nextrest
                        set beginning of ims_shuffled to nextpick
                        set numitems to numitems - 1
                        set nextrest to removeItem(nextrest, i)
              end repeat
              return ims_shuffled
    end shuffle_items
    on shuffleIms()
      -- retrieve the selected images from Aperture
      -- and display them in random order in full screen mode
              local imageSel, shuffled, i
              tell application "Aperture"
      activate
                        set imageSel to (get selection)
                        set shuffled to my shuffle_items(imageSel)
                        set fullscreen to true
                        if imageSel is {} then error "Please select some images."
                        repeat with i from 1 to count of shuffled
                                  reveal {item i of shuffled}
      delay 3 -- chnage that to the time you want
                        end repeat
                        set fullscreen to false
                        return shuffled
              end tell
    end shuffleIms
    shuffleIms()
    Some more code snippets to go from here:
    To create an album:
                        tell library 1
                                  if not (exists album "shuffledAlbum") then
      make new album with properties {name:"shuffledAlbum", image version:shuffled}
                                  end if
                        end tell
    And to add the images from the shuffled list to the album:
                        repeat with i from 1 to count of shuffled
                                  duplicate item i of shuffled to album "shuffledAlbum"
                        end repeat
    Regards
    Léonie

  • How do you add an image in the signature

    Hi,
    How do you add an image to the signature area.
    The Insert/edit image is not available when in My Settings | Personnel Info | Signature
    Regards
    Ray Farmer

    You need to link the image.  See the example for the Canada flag below.
    <img src="http://forums.ni.com/ni/attachments/ni/130/6908/1/Canada-small.PNG" border=0>
    Replace with the image you like.  This goes into the signature box within the profile tab of the personal settings.
    RayR

  • How can you upload an image to MYSQL using php?

    Ok this is a joint thread with one of my other ones because no one else  was posting on it, so I did not get what I needed, so, How can you  upload an image to mysql so when some one logs on to my site, they see  or can upload there own avatar, now there are loads of problems with  this, 1) many people don't know how, 2) It's not every day you try to  encode a image into mysql, 3) Letting people upload images into mysql  can cause traffic problem with people trying to log in. So that's it.

    Uploading images into MySQL is a bad idea, for more reasons than you mention. You should store the images in a directory on the server instead. But if you want to learn the hard way, here is the method:
    The mysql data type must be blob.
    The form enctype must be multipart, like this: enctype="multipart/form-data"
    The form input field myst be type file, like this: <input type="file" name="upload"/>
    Here is example code that prepares the variable $photodata to be inserted into the database.
      $uploadfile = $_FILES['upload']['tmp_name'];
      $photoname = $_FILES['upload']['name'];
      $uploadtype = $_FILES['upload']['type'];
      if ($uploadfile != "") {
    $tempfile = fopen($uploadfile, 'rb');
    $filesize=filesize($uploadfile);
    $width=getimagesize($uploadfile);
    $widthx=$width[0];
    $heightx=$width[1];
    if (($widthx > 102) or (heightx > 135)){
    $image_too_big = " but the image size was too large and was not submitted.";
    } else {
    $photodata = fread($tempfile, filesize($uploadfile));
    $photodata = addslashes($photodata);

  • How can you add an image in Mail without being an attachment?

    How can you add an image in Mail without being an attachment?
    In otherwords I'd like to put my logo in the email not as an attachment but as an image.
    Is there a simpel solution to this problem?

    PBN1 wrote:
    How can you add an image in Mail without being an attachment?
    You don't.
    It's not possible. The e-mail protocol is designed for text; anything else has to go as an attachment.
    Different mail clients (such as Mail.app, the mail client bundled with Mac OS X) may have different ways of handling such attachments, but they are still attachments. Each mail client has its own rules and methods, so one may display a picture as if it were in the body of the message, but another client may do something completely different.
    A way of faking it is to format your message in HTML (which is a kind of text). The image is hosted on a remote server, not added to the message; instead, you include a link to it in the body of the message, as you would when building a web page. (This is also what the two tips helpfully provided by X423424X do, except that the link to the image is added in the signature, rather than the body of the message.) What exactly happens to it is, again, at the discretion of the mail client. In my case, for instance, displaying images in HTML messages is turned off, and will stay resolutely off.

  • Sorry I'm new to Final Cut Express. How do you make an image (jpeg file for example) move across the screen slowly as I've seen in many films.

    Sorry I'm new to Final Cut Express. How do you make an image (jpeg file for example) move across the screen slowly as I've seen in many films.

    Neil from bristol wrote:
    … How do you make an image … move across the screen …
    by setting KEYFRAMES
    (read, what FC/e's built-in Help-feature tells about it)
    resize and set pic to start position
    set keyframe
    move playhead in timeline to desired length
    set pic to end position
    set keyframe …

  • How do you select an image in keynote?

    how do you select an image in keynote? I am trying to reduces my image file size but when clicking on image to reduce it I seem to be selecting the mask and hence format/image does not provide me with an option to reduce the image

    Click on it to select.
    Click and drag a corner handle to change the image size on screen.
    This does not alter the file size.

  • How do you delete the images in the Iphone images album.  Not the camera roll.  There is an album called Iphone images and there is not a delete option

    How do you delete the images in the Iphone images album.  Not the camera roll.  There is an album called Iphone images and there is not a delete option

    The same way you put them on the phone. Connect the phone to iTunes on your computer, click on the name of the iPhone, then the Photo tab. Uncheck the albums you don't want on the phone and click Sync.

  • How do you show thumbnail images for the contacts?

    How do you show thumbnail images for the contacts on the iPod?
    -Thank You

    In another thread I posted a suggestion to try when the iPhoto app in the Application folder will not work:
    after upgrading the OS, iPhotos (and Aperture for that matter) should still be in the Applications folder...
    And running well in 10.10.3.
    If it stops running in a next "update" do not throw it away: most apps will continue running as follows:
    In the Applications folder "rightclick" (CTL+click) the iPhoto app, then "show package contents", then click the MacOs folder open: you see the real app (dark grey), rightclick this and choose "make alias", an alias is made, move this alias to the Applications folder and use it to run iPhoto.
    Same for Aperture.
    In older OS this survived several upgrades....
    Lex

  • How do you get Raw images form a Canon 60D to open up on Photoshop Elements 9!

    How do you get Raw images form a Canon 60D to open up on Photoshop Elements 9! I tried downloading the Adobe Camera Raw 6.2 update and Elements is not recognizing the file to install!

    Go to Help>Updates in the editor and do it that way instead.

Maybe you are looking for

  • J1IIN Create Factory invoice thr BDC-UserExit  J_1I7_USEREXIT_DUTY_IN_EXIT

    Hello All, Using BDC I am trying to create a factory exicse invoice as below:     PERFORM bdc_dynpro      USING 'SAPMJ1IIN' '1000'.     PERFORM bdc_field       USING 'BDC_OKCODE' '=ECRT'.     PERFORM bdc_dynpro      USING 'SAPMJ1IIN' '0150'.     PERF

  • Help with vidieo? Premiere Elements

    I created a slide show in organizer. Now in Premier Elements I have added a narritive and sound track. Hve burnt a dvd in hvchd and it works fine on my dvd players at home but will not work on some other dvd players I have tried. Also when I down loa

  • Control Center down when logoff from Windows server

    I have started the control Center on a windows server using start <OWB-menu> / Administration / Start Control Center / Start Control Center Service. When I logoff from that server, the Control Center Service goes down. How can I start the Control Cen

  • Smooth .mov - jerky .mp4-

    Ok, this one puzzles me. I have this nice and heavy 900MB lossless mov file. It's been exported from Screenflow at the best possible quality (lossless, 1280x720, 48kHz audio-) It plays well on this mac. Except on this machine I don't have the sources

  • How i can rewrite this message to my language

    hii , all how i rewrite this message to my language ,this message appear when i insert a wrong data into my block .im using oracle forms 10g http://imageshack.us/photo/my-images/577/rewritez.jpg/