Problem using AffineTransform

R

Rick

Hi,

I'm drawing a hexagon in the center of a JPanel. I am then drawing
squares on each side of the polygon. To map the edges of the square onto
the sides of the polygon, I am transforming the co-ordinates of the
squares and rotating them so that their sides map onto the sides of the
polygon. When I try doing this in a JFrame where there's only one
JPanel, everything works fine.. however, when I put this JPanel onto one
side of a JSplitpane (or even if I add another JPAnel onto the JFrame),
the transformed squares fall elsewhere, on the left side of the polygon
when they should actually transform and map onto the sides of the polygon.

It seems that the AffineTransform.getRotateInstance() method is taking
the wrong set of co-ordinates. It is taking the center of the "JFrame"
for some reason. Can anyone please help me identify the problem here?
Why isnt AffineTransform.getRotateInstance() not working properly?
Eventhough I'm giving it the same set of co-ordinates I used for drawing
the polygon (in the center of the JPanel - which seems to work fine), it
just won't transform the squares at the right place. If I comment out
the following line where I am setting the transform object:

g2.setTransform(rotate45);

The squares are drawn at the correct co-ordinates, except that they
aren't rotated. How can I fix this? thanks a lot!

Cheers

Rick
 
M

Marco Parmeggiani

The squares are drawn at the correct co-ordinates, except that they
aren't rotated. How can I fix this? thanks a lot!

static AffineTransform getRotateInstance(double theta);

static AffineTransform getRotateInstance(double theta, double x, double
y);

with the second function you can specify the pivot point of the
rotation.

HTH,
ciao
 
T

Thomas Weidenfeller

Rick said:
polygon. When I try doing this in a JFrame where there's only one
JPanel, everything works fine.. however, when I put this JPanel onto one
side of a JSplitpane (or even if I add another JPAnel onto the JFrame),
the transformed squares fall elsewhere, on the left side of the polygon
when they should actually transform and map onto the sides of the polygon.

I would guess that one of two things (or both :)) happens:

1. You get a Graphics that has a different origin (location of (0,0))
than you expect. In fact, you can see rather interesting configurations
of the Graphics object when e.g. a partial redraw of a component should
happen. These configurations are not wrong, just not as simple as one
would like it. Print out all the data of the Graphics object to get
some idea of what might be different.

2. You don't reset the Graphics to the original transformation once you
are done inside one paintComponent(). Swing passes the very same
Graphics object to all the lightweight components down in a component
hierarcy (this is Swing's double buffer mechnism). So if you change the
Graphics object in one component's paintComponent() method the change
might influence the later painting in another component. Get the
Graphic's transformation at the beginning of a paintComponent()
implementation and reset the Graphics to the transformation before you
exit the method.


HTH

/Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top