Skip to main content

[JAVA2D] Graphics context and AffineTransform

1 reply [Last post]
Anonymous

Is there some documentation/tutorial etc. that discusses the fact that
the AffineTransform attached to the Graphics param of a Swing
component's paintComponent(Graphics g) will not always be the identity
transform?

For example, if my JPanel is in a container with another component to
its left which is 150 pixels wide, this is the value of the
AffineTransform that gets passed in.

[[1.0, 0.0, 150.0], [0.0, 1.0, 0.0]]

I would have expected the identity transform to always be passed in, but
its not.

Any information on what is going on would be appreciated.

Thanks,

Ted Hill

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".
[att1.html]

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Dmitri Trembovetski

Hi Ted,

On Thu, Jul 07, 2005 at 02:41:30PM -0500, Ted Hill wrote:
> Is there some documentation/tutorial etc. that discusses the fact that
> the AffineTransform attached to the Graphics param of a Swing
> component's paintComponent(Graphics g) will not always be the identity
> transform?
>
> For example, if my JPanel is in a container with another component to
> its left which is 150 pixels wide, this is the value of the
> AffineTransform that gets passed in.
>
> [[1.0, 0.0, 150.0], [0.0, 1.0, 0.0]]
>
> I would have expected the identity transform to always be passed in, but
> its not.
>
> Any information on what is going on would be appreciated.

This is the way swing does painting, they set the transform and some
other graphics attributes (like clip) and pass it to the components
so they can paint themselves.

This is why you must contatenate your transform with the existing
one instead of replacing it.

Also, you'll need to restore the original transform on the graphics
object (or any other attributes you change) once you're done,
otherwise other swing components's painting will be screwed.

The best way to do this is to clone the original graphics context
and use it instead of the one passed in:
public void paintComponent(Graphics g) {
Graphics g2d = g.create();
// now you can work with the g2d context

// append a new transform if you wish
g2d.rotate(...);
g2d.translate(...);

// do some renering
g2d.drawImage(...);

g2d.dispose();
// no need to restore the attributes of the passed in 'g'
// object
}

Thank you,
Dmitri

>
> Thanks,
>
> Ted Hill
>
>
>
>
> ===========================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in the body
> of the message "signoff JAVA2D-INTEREST". For general help, send email to
> listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".