Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Graphics rotation / translation / affinetransform problem

David Stevens
Greenhorn
Posts: 19
I have a class GridPanel extends JPanel which displays a grid and then sends a listener in another class a Rectangle (in fact it's a square) for each grid position:

In the paintGridPosition method, I am trying to rotate the square multiples of 90 degrees around its centre axis and then draw on it.
In the code below I'm always rotating 90 degrees but this would change depending on other factors.

The problem I'm having is that I'm unable to restore the original transformation, so that for each GridPosition and corresponding rectangle of co-ordinates
over each iteration, I start with the original un-touched transformation. I've tried various things, but I always end up concatenating the transformation instead of restoring the original.
This means that only the first GridPosition displays correctly in the panel.

I'd appreciate any help you can give.

David

Rob Camick
Ranch Hand
Posts: 2639
9
This example may be similiar to what you want. At least it may give you some further ideas.

David Stevens
Greenhorn
Posts: 19
Rob Camick wrote:This example may be similiar to what you want. At least it may give you some further ideas.

Thanks, I'll check it out

David Stevens
Greenhorn
Posts: 19
That example doesn't really help I'm afraid.
Any other ideas please?

Campbell Ritchie
Sheriff
Posts: 50720
83
Always start by copying the Graphics object with its copying method, which I think is called "create". Some transforms, eg translate, are not a problem, but the amount of floating-point arithmetic required for a shear introduces imprecisions which you can never reverse.

What is your listener?

I suggest you try some simple examples first, and see how the different transforms work.
Draw a rectangle, Thread.sleep(2000), translate, then draw the same rectangle again. That way you can watch what happens.

David Stevens
Greenhorn
Posts: 19
Campbell Ritchie wrote:
What is your listener?

My listener / observer is an interface that allows my interested class to get info like the GridPosition clicked or GridPosition mouseEntered / exited. Basically it just converts mouse listener events implemented by my GridPanel class and converts them to GridPositions.

In the paintGridPosition method I showed, it should provide the interested class a way to draw a particular shape (with the desired rotation) at each position on the GridPanel.

Campbell Ritchie wrote: I suggest you try some simple examples first, and see how the different transforms work.
Draw a rectangle, Thread.sleep(2000), translate, then draw the same rectangle again. That way you can watch what happens.

Yes, I'll go back to a simple example and try to figure it out that way.
Thanks for everyone's time.

Campbell Ritchie
Sheriff
Posts: 50720
83
You're welcome and good luck with it. Make sure to tell us how you get on.

David Stevens
Greenhorn
Posts: 19
My problem turned out to be a simple maths error when referring to my rectangle's coordinates, but I thought I'd post the correct code in case this helps someone else:

Because this method receives a copy of Graphics2D, I don't need to keep a copy of the original transfom and restore it.

Cheers,

David

Campbell Ritchie
Sheriff
Posts: 50720
83
I'm sure there is an easier way to do it. There are shear, rotate, translate, etc methods in the Graphics2D class.