Question

Je suis en train de l'échelle / translater un java.awt. Forme AffineTransform afin de tirer dans un sens bondissant rectangle.

De plus, je veux peindre dans une zone de dessin ayant un ' Zoom paramètre.

J'ai essayé plusieurs concaténations de AffineTransform mais je ne pouvais pas trouver la séquence correcte. Par exemple, la solution était erronée suivante:

double zoom=(...);/* current zoom */
Rectangle2D viewRect=(...)/** the rectangle where we want to paint the shape */
Shape shape=(...)/* the original shape that should fit in the rectangle viewRect */
Rectangle2D bounds=shape.getBounds2D();

double ratioW=(viewRect.getWidth()/bounds.getWidth());
double ratioH=(viewRect.getHeight()/bounds.getHeight());


AffineTransform transforms[]=
    {
    AffineTransform.getScaleInstance(zoom, zoom),
    AffineTransform.getTranslateInstance(-bounds.getX(),-bounds.getY()),
    AffineTransform.getTranslateInstance(viewRect.getX(),viewRect.getY()),
    AffineTransform.getScaleInstance(ratioW, ratioH)
    };


AffineTransform tr=new AffineTransform();
for(int i=0;i< transforms.length;++i)
    {
    tr.concatenate(transforms[i]);
    }

Shape shape2=tr.createTransformedShape(shape);
graphics2D.draw(shape2);

Toute idée de la AffineTransform correcte?

Merci beaucoup

Pierre

Était-ce utile?

La solution

Notez que AffineTransform transformations sont concaténés "de la manière le plus souvent utile", ce qui peut être considéré comme dernier , first-out pour. L'effet peut être vu dans cette exemple . Compte tenu de la séquence ci-dessous, la Shape résultant est d'abord tourné, puis mis à l'échelle et enfin traduit.

at.translate(SIZE/2, SIZE/2);
at.scale(60, 60);
at.rotate(Math.PI/4);
return at.createTransformedShape(...);

Autres conseils

Inspiré par la réponse de trashgod, la séquence correcte est:

AffineTransform transforms[]=
{
AffineTransform.getScaleInstance(zoom, zoom),
AffineTransform.getTranslateInstance(viewRect.getX(),viewRect.getY()),
AffineTransform.getScaleInstance(ratioW, ratioH),
AffineTransform.getTranslateInstance(-bounds.getX(),-bounds.getY())
};



AffineTransform tr=new AffineTransform();
for(int i=0;i< transforms.length;++i)
 {
 tr.concatenate(transforms[i]);
 }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top