Win a copy of Penetration Testing Basics this week in the Security forum!

# Graphics2D AffineTransform

Nitesh Nirala
Greenhorn
Posts: 8

I am trying to create GeneralPath as shown in above image but I am unable to apply Affine Transformation properly. I want to output something as second figure which is letter 'F'. I can scale it but when I rotate it disappears (i think something is wrong with my logic). I know how to apply stroke and change to yellow color, so will need help with transformation only. My output so far is shown

. Please see my code below:

image2.jpg

Piet Souris
Rancher
Posts: 1398
29
• 2
hi Nitesh,

well, your rotation does work, but since the rotation is about the origin (which is (0,0)
and in this case it is the upper left pixel of your JPanel), it disappears out of view.

To make the best use of modeling, is to do two things:

first, for each object that you want to draw, like the 'F' in your program, define it in a
coordinate system that is natural for the figure you want to draw. So, for instance, define
a coordinate system for your F that has its left lowest point at coordinate (0,0), has a height of one
unit, et cetera.

next, define a coordinate system for your JPanel, that will make it easy to draw your figure.

So, knowing that your F is one unit long, define the coordinate system of the JPanel such that
it occupies the coordinates (-1, 1) - (1, -1). So the origin (0,0) will be in the center of the panel.
Having set this coordinatesystem, then draw your F in its natural coordinates.

This all involves some linear algebra. but foremost do read the documentation of AffineTransforms,
so you know what to do and how to use it.

A very nice chapter about Java 2D Graphics can be found here:

http://math.hws.edu/graphicsnotes/c1/index.html

Greetings,
Piet

Nitesh Nirala
Greenhorn
Posts: 8
Thanks for replying and sharing the link. I have read the chapter but still having hard time with changing coordinate for JPanel. Will read the code and try to understand it again. Thanks for your help