7 Replies - 3565 Views - Last Post: 16 April 2009 - 09:10 AM Rate Topic: -----

#1 EquinoX  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 19-January 07

Zooming Buffered Image in Java

Posted 19 November 2007 - 10:45 AM

Hi guys,

I am doing a paint program and right now working on the zoom tool. However I don't know how to do this? Can someone please help me on how to deal with this?? thanks
Is This A Good Question/Topic? 0
  • +

Replies To: Zooming Buffered Image in Java

#2 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Zooming Buffered Image in Java

Posted 19 November 2007 - 11:40 AM

Check the java.awt.Graphics2D class (or simply the Graphics class), it has some interesting methods, such as this one:

Quote

public abstract boolean drawImage(Image img,
int x,
int y,
int width,
int height,
Color bgcolor,
ImageObserver observer)

Draws as much of the specified image as has already been scaled to fit inside the specified rectangle.

The image is drawn inside the specified rectangle of this graphics context's coordinate space, and is scaled if necessary. Transparent pixels are drawn in the specified background color. This operation is equivalent to filling a rectangle of the width and height of the specified image with the given color and then drawing the image on top of it, but possibly more efficient.

This method returns immediately in all cases, even if the entire image has not yet been scaled, dithered, and converted for the current output device. If the current output representation is not yet complete then drawImage returns false. As more of the image becomes available, the process that loads the image notifies the specified image observer.

A scaled version of an image will not necessarily be available immediately just because an unscaled version of the image has been constructed for this output device. Each size of the image may be cached separately and generated from the original data in a separate image production sequence.

Parameters:
img - the specified image to be drawn. This method does nothing if img is null.
x - the x coordinate.
y - the y coordinate.
width - the width of the rectangle.
height - the height of the rectangle.
bgcolor - the background color to paint under the non-opaque portions of the image.
observer - object to be notified as more of the image is converted.
Returns:
false if the image pixels are still changing; true otherwise.


Also, check out the method setRenderingHint:

Quote

public abstract void setRenderingHint(RenderingHints.Key hintKey,
Object hintValue)

Sets the value of a single preference for the rendering algorithms. Hint categories include controls for rendering quality and overall time/quality trade-off in the rendering process. Refer to the RenderingHints class for definitions of some common keys and values.

Parameters:
hintKey - the key of the hint to be set.
hintValue - the value indicating preferences for the specified hint category.
to control quality.
Was This Post Helpful? 0
  • +
  • -

#3 EquinoX  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 19-January 07

Re: Zooming Buffered Image in Java

Posted 19 November 2007 - 01:48 PM

View Post1lacca, on 19 Nov, 2007 - 11:40 AM, said:

Check the java.awt.Graphics2D class (or simply the Graphics class), it has some interesting methods, such as this one:

Quote

public abstract boolean drawImage(Image img,
int x,
int y,
int width,
int height,
Color bgcolor,
ImageObserver observer)

Draws as much of the specified image as has already been scaled to fit inside the specified rectangle.

The image is drawn inside the specified rectangle of this graphics context's coordinate space, and is scaled if necessary. Transparent pixels are drawn in the specified background color. This operation is equivalent to filling a rectangle of the width and height of the specified image with the given color and then drawing the image on top of it, but possibly more efficient.

This method returns immediately in all cases, even if the entire image has not yet been scaled, dithered, and converted for the current output device. If the current output representation is not yet complete then drawImage returns false. As more of the image becomes available, the process that loads the image notifies the specified image observer.

A scaled version of an image will not necessarily be available immediately just because an unscaled version of the image has been constructed for this output device. Each size of the image may be cached separately and generated from the original data in a separate image production sequence.

Parameters:
img - the specified image to be drawn. This method does nothing if img is null.
x - the x coordinate.
y - the y coordinate.
width - the width of the rectangle.
height - the height of the rectangle.
bgcolor - the background color to paint under the non-opaque portions of the image.
observer - object to be notified as more of the image is converted.
Returns:
false if the image pixels are still changing; true otherwise.


Also, check out the method setRenderingHint:

Quote

public abstract void setRenderingHint(RenderingHints.Key hintKey,
Object hintValue)

Sets the value of a single preference for the rendering algorithms. Hint categories include controls for rendering quality and overall time/quality trade-off in the rendering process. Refer to the RenderingHints class for definitions of some common keys and values.

Parameters:
hintKey - the key of the hint to be set.
hintValue - the value indicating preferences for the specified hint category.
to control quality.


hmm..I get your idea.. so am I suppose to use the affine transform here? or the rendering hint that you suggest. One more thing I am using buffered image as the image type to zoom here

This post has been edited by EquinoX: 19 November 2007 - 02:14 PM

Was This Post Helpful? 0
  • +
  • -

#4 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Zooming Buffered Image in Java

Posted 19 November 2007 - 07:31 PM

Nope, as the documentation writes you can simply use the drawImage method, because it draws (and zooms/scales if needed) an Image (and BufferedImage is an Image as well).
RenderingHint is just to specify the quality of the scaling, it can make the operation faster, or the quality of the final picture better.
Was This Post Helpful? 0
  • +
  • -

#5 EquinoX  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 19-January 07

Re: Zooming Buffered Image in Java

Posted 20 November 2007 - 07:19 AM

View Post1lacca, on 19 Nov, 2007 - 07:31 PM, said:

Nope, as the documentation writes you can simply use the drawImage method, because it draws (and zooms/scales if needed) an Image (and BufferedImage is an Image as well).
RenderingHint is just to specify the quality of the scaling, it can make the operation faster, or the quality of the final picture better.


if that's so then what should I use the x for?? I can't seem to find that drawImage method with all the parameters in the API....
Was This Post Helpful? 0
  • +
  • -

#6 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Zooming Buffered Image in Java

Posted 20 November 2007 - 07:51 AM

It's defined in java.awt.Graphics and is inherited by Graphics2D.

But boolean drawImage(Image img, int dx1,int dy1,int dx2,int dy2,int sx1,int sy1,int sx2,int sy2,ImageObserver observer)
might be even better:

Quote

public abstract boolean drawImage(Image img,
int dx1,
int dy1,
int dx2,
int dy2,
int sx1,
int sy1,
int sx2,
int sy2,
ImageObserver observer)

Draws as much of the specified area of the specified image as is currently available, scaling it on the fly to fit inside the specified area of the destination drawable surface. Transparent pixels do not affect whatever pixels are already there.

This method returns immediately in all cases, even if the image area to be drawn has not yet been scaled, dithered, and converted for the current output device. If the current output representation is not yet complete then drawImage returns false. As more of the image becomes available, the process that draws the image notifies the specified image observer.

This method always uses the unscaled version of the image to render the scaled rectangle and performs the required scaling on the fly. It does not use a cached, scaled version of the image for this operation. Scaling of the image from source to destination is performed such that the first coordinate of the source rectangle is mapped to the first coordinate of the destination rectangle, and the second source coordinate is mapped to the second destination coordinate. The subimage is scaled and flipped as needed to preserve those mappings.

Parameters:
img - the specified image to be drawn
dx1 - the x coordinate of the first corner of the destination rectangle.
dy1 - the y coordinate of the first corner of the destination rectangle.
dx2 - the x coordinate of the second corner of the destination rectangle.
dy2 - the y coordinate of the second corner of the destination rectangle.
sx1 - the x coordinate of the first corner of the source rectangle.
sy1 - the y coordinate of the first corner of the source rectangle.
sx2 - the x coordinate of the second corner of the source rectangle.
sy2 - the y coordinate of the second corner of the source rectangle.
observer - object to be notified as more of the image is scaled and converted.
Returns:
true if the current output representation is complete; false otherwise.
Since:
JDK1.1

Was This Post Helpful? 0
  • +
  • -

#7 EquinoX  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 19-January 07

Re: Zooming Buffered Image in Java

Posted 20 November 2007 - 11:45 AM

View Post1lacca, on 20 Nov, 2007 - 07:51 AM, said:

It's defined in java.awt.Graphics and is inherited by Graphics2D.

But boolean drawImage(Image img, int dx1,int dy1,int dx2,int dy2,int sx1,int sy1,int sx2,int sy2,ImageObserver observer)
might be even better:

Quote

public abstract boolean drawImage(Image img,
int dx1,
int dy1,
int dx2,
int dy2,
int sx1,
int sy1,
int sx2,
int sy2,
ImageObserver observer)

Draws as much of the specified area of the specified image as is currently available, scaling it on the fly to fit inside the specified area of the destination drawable surface. Transparent pixels do not affect whatever pixels are already there.

This method returns immediately in all cases, even if the image area to be drawn has not yet been scaled, dithered, and converted for the current output device. If the current output representation is not yet complete then drawImage returns false. As more of the image becomes available, the process that draws the image notifies the specified image observer.

This method always uses the unscaled version of the image to render the scaled rectangle and performs the required scaling on the fly. It does not use a cached, scaled version of the image for this operation. Scaling of the image from source to destination is performed such that the first coordinate of the source rectangle is mapped to the first coordinate of the destination rectangle, and the second source coordinate is mapped to the second destination coordinate. The subimage is scaled and flipped as needed to preserve those mappings.

Parameters:
img - the specified image to be drawn
dx1 - the x coordinate of the first corner of the destination rectangle.
dy1 - the y coordinate of the first corner of the destination rectangle.
dx2 - the x coordinate of the second corner of the destination rectangle.
dy2 - the y coordinate of the second corner of the destination rectangle.
sx1 - the x coordinate of the first corner of the source rectangle.
sy1 - the y coordinate of the first corner of the source rectangle.
sx2 - the x coordinate of the second corner of the source rectangle.
sy2 - the y coordinate of the second corner of the source rectangle.
observer - object to be notified as more of the image is scaled and converted.
Returns:
true if the current output representation is complete; false otherwise.
Since:
JDK1.1


I did not understand what dx1, dx2, dy1, dy2 will be used on the zoom??
Was This Post Helpful? 0
  • +
  • -

#8 dkheartsag  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 18-February 09

Re: Zooming Buffered Image in Java

Posted 16 April 2009 - 09:10 AM

View PostEquinoX, on 20 Nov, 2007 - 10:45 AM, said:

View Post1lacca, on 20 Nov, 2007 - 07:51 AM, said:

It's defined in java.awt.Graphics and is inherited by Graphics2D.

But boolean drawImage(Image img, int dx1,int dy1,int dx2,int dy2,int sx1,int sy1,int sx2,int sy2,ImageObserver observer)
might be even better:

Quote

public abstract boolean drawImage(Image img,
int dx1,
int dy1,
int dx2,
int dy2,
int sx1,
int sy1,
int sx2,
int sy2,
ImageObserver observer)

Draws as much of the specified area of the specified image as is currently available, scaling it on the fly to fit inside the specified area of the destination drawable surface. Transparent pixels do not affect whatever pixels are already there.

This method returns immediately in all cases, even if the image area to be drawn has not yet been scaled, dithered, and converted for the current output device. If the current output representation is not yet complete then drawImage returns false. As more of the image becomes available, the process that draws the image notifies the specified image observer.

This method always uses the unscaled version of the image to render the scaled rectangle and performs the required scaling on the fly. It does not use a cached, scaled version of the image for this operation. Scaling of the image from source to destination is performed such that the first coordinate of the source rectangle is mapped to the first coordinate of the destination rectangle, and the second source coordinate is mapped to the second destination coordinate. The subimage is scaled and flipped as needed to preserve those mappings.

Parameters:
img - the specified image to be drawn
dx1 - the x coordinate of the first corner of the destination rectangle.
dy1 - the y coordinate of the first corner of the destination rectangle.
dx2 - the x coordinate of the second corner of the destination rectangle.
dy2 - the y coordinate of the second corner of the destination rectangle.
sx1 - the x coordinate of the first corner of the source rectangle.
sy1 - the y coordinate of the first corner of the source rectangle.
sx2 - the x coordinate of the second corner of the source rectangle.
sy2 - the y coordinate of the second corner of the source rectangle.
observer - object to be notified as more of the image is scaled and converted.
Returns:
true if the current output representation is complete; false otherwise.
Since:
JDK1.1


I did not understand what dx1, dx2, dy1, dy2 will be used on the zoom??

nice post friend,
can i have a sample working code so i can understand the working of this .
thax
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1