• Post Reply Bookmark Topic Watch Topic
  • New Topic

Affinetransform confusion  RSS feed

 
Zukias Trunchbull
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


From reading the Java docs on Affine transforms I expected this code to rotate each ImgObj drawn onto mImage to be rotated 90 degrees around its center, so each image is drawn in the same place, but on their side. But what actually happens is, each images position is completely different (even when theres only 1 image to draw) and some images don't appear on screen at all. Even more strangely, some rotations, such as 60 degrees & 45 degrees throw run time errors: RasterFormatException: Transformed height (-48) is less than or equal to 0.

Is there something I'm misunderstanding about affine transforms?

Any help would be greatly appreciated!
 
Piet Souris
Rancher
Posts: 1943
66
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Zukias,

quite complex here!

What I mistrust are these two lines:

that you use as the center of rotation later.
Are you sure it is okay to mix the imgObj.getX with image.getWidth()?
I don't know what imgObj is, so I can't tell what value getX is.
Try some System.out.println's to inspect the values.

Two remarks:

1) you could create 'mImage' upfront, so that you do not need to recreate
it everytime in 'paintComponent'

2) why use these complex methods? You could simply do the transforms
direcly on g2d, before drawing your images. That would make the code
less complex.

Greetz,
Piet
 
Zukias Trunchbull
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:hi Zukias,

quite complex here!

What I mistrust are these two lines:

that you use as the center of rotation later.
Are you sure it is okay to mix the imgObj.getX with image.getWidth()?
I don't know what imgObj is, so I can't tell what value getX is.
Try some System.out.println's to inspect the values.

Two remarks:

1) you could create 'mImage' upfront, so that you do not need to recreate
it everytime in 'paintComponent'

2) why use these complex methods? You could simply do the transforms
direcly on g2d, before drawing your images. That would make the code
less complex.

Greetz,
Piet


Thanks for the suggestion, using the normal rotate method did indeed work (+ much nicer looking code) The AffineTransform class works in mysterious ways...

One thing with remark 1) though, my mImage only gets created once because of the if(mImage == null), or do you mean something else?
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you would do well to apply affine transforms to copies of the Graphics objects, because some transforms cannot reliably be undone. Rotate translate and scale are usually not too bad, but shear can be a real problem.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes ago, I wrote: . . . some transforms cannot reliably be undone. . . .
I think I meant to say, “cannot readily be undone”.
 
Piet Souris
Rancher
Posts: 1943
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AffineTransforms are great!

What I meant was not so much the creation of 'mImage' itself, but the drawing
of all your rotated imgObj within 'imgObjs'. If 'imgObjs' does not change, or not
change often, then you could fill your mImage in, say, your initialization,
then all you have to do in 'paintComponent' is to directly draw 'mImage'.

But I only made this remark for completeness' sake so that you could
have a look at it and give it a rethinking.

I made a quick version myself this evening, what I did notice was
a truncation of the image being rotated with this method. I wanted
to supply that image in my reply, but I could not manage that.
Do you also suffer from truncations?

Greetz,
Piet
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!