Solved

Graphics2D.setClip() not working?

Posted on 2006-11-07
8
566 Views
Last Modified: 2010-03-31
I have the following function which sends an image and the rectangle size I would like to have clipped:

RenderedImage clippedImage = GraphicUtilities.clipImage(image, new java.awt.geom.Rectangle2D.Double(0, 0, 80, 60));

The clipImage method looks like this:

      public static RenderedImage clipImage(Image image, Shape shape) {
            // Create a buffered image in which to draw
        BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
        // Need to get image into bufferedImage
        bufferedImage.getGraphics().drawImage(image, 0, 0, null);
            Graphics2D g2 = bufferedImage.createGraphics();
            // Clip the image
            if (bufferedImage != null) {
                  g2.setClip(shape);
                  g2.drawImage(bufferedImage, 0, 0, null);
                  g2.dispose();
            }
            return bufferedImage;
      }

Any ideas as to why the returned image is the exact same size as the original image and isn't actually clipped at all? How do I correct this?
0
Comment
Question by:deckmagic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 92

Expert Comment

by:objects
ID: 17896024
>         bufferedImage.getGraphics().drawImage(image, 0, 0, null);

because the 1st thing u do is paint it all to the BI
0
 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 17896657
  Hi

Here are some clip examples to look at and compare to what you are
doing : http://www.java2s.com/Code/Java/2D-Graphics-GUI/Clip.htm

Regards,
  Tomas Helgi
0
 

Author Comment

by:deckmagic
ID: 17898414
Okay, so I rewrote the method so now the image appears to be clipped (an 80 x 60 rectangle in the upper-left corner), but the full size of the image is still 480x360. I thought clipping the image would also change the size of the image returned, how do I get the returned image to be 80x60?

      public static RenderedImage clipImage(Image image, Shape shape) {
            // Create a buffered image in which to draw
        BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
            Graphics2D g2 = bufferedImage.createGraphics();
            // Clip the image
            if (bufferedImage != null) {
                  g2.setClip(shape);
                  g2.drawImage(image, 0, 0, null);
                  g2.dispose();
            }
            return bufferedImage;
      }
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 17898677
Create a new Buffered Image with the desired size (80x60) and
copy the old BufferedImage to the new BufferedImage and return that.

Regards,
  Tomas Helgi
0
 
LVL 92

Expert Comment

by:objects
ID: 17902242
> I thought clipping the image would also change the size of the image returned,

No it does not affect the size

> how do I get the returned image to be 80x60?

create it that size in the first place
0
 

Author Comment

by:deckmagic
ID: 17902734
Okay, I'm confused. I have the following method that I iterate thru 6 times, the shape changes by 80 on each iteration:

public static RenderedImage clipImage(Image image, Shape shape) {
                BufferedImage bufferedImage = new BufferedImage(shape.getBounds().width, shape.getBounds().height, BufferedImage.TYPE_INT_RGB);
                Graphics2D g2 = bufferedImage.createGraphics();
      g2.setClip(shape);
      g2.drawImage(image, 0, 0, null);
      return bufferedImage;
}

The first time through, I get an image that is 80x60 that looks like the upper-left corner of image.
The second - sixth time through, I get a completely blank image.

From what I understood I thought this code did the following:
1. Create a new BufferedImage of the same size as the shape (80x60)
2. Assign the graphics context of this BufferedImage to g2
3. Clip the image according to the shape bounds
4. Draw this clipped portion into bufferedImage

If this is wrong, how would I rewrite it to work correctly? I basically have a sheet of 6 images that are 80x60 in size. I want to loop through this method, clip out each image, and return that image to be saved to a seperate file.
0
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 17903958
u don't need to setClip() to do that, u just want to paint the portion of the image u require each time to the new image.

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Graphics.html#drawImage(java.awt.Image,%20int,%20int,%20int,%20int,%20int,%20int,%20int,%20int,%20java.awt.image.ImageObserver)
0
 

Author Comment

by:deckmagic
ID: 17905335
That did it. Here is the code I used if anyone is interested:

      public static RenderedImage clipImage(Image image, Shape shape) {
            // Destination rectangle
            int x1,y1,x2,y2;
            x1 = 0;
            y1 = 0;
            x2 = shape.getBounds().width;
            y2 = shape.getBounds().height;
            // Source rectangle
            int x3,y3,x4,y4;
            x3 = shape.getBounds().x;
            y3 = shape.getBounds().y;
            x4 = x3 + shape.getBounds().width;
            y4 = y3 + shape.getBounds().height;
            
            // Create a buffered image in which to draw
        BufferedImage bufferedImage = new BufferedImage(shape.getBounds().width, shape.getBounds().height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2 = bufferedImage.createGraphics();
            // Clip the image
            if (bufferedImage != null) {
                  g2.drawImage(image, x1, y1, x2, y2, x3, y3, x4, y4, null);
                  g2.dispose();
            }
            return bufferedImage;
      }
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question