I am using Java Graphics2D to draw on a component using AffineTransform to control my drawing. Graphics2D offers a method conversion for this that accepts AffineTransform.
Sometimes I need to manipulate a point manually without using the inline transform. But when I try to transform a point using the same transformation that I gave Graphics2D.transform, sometimes the resulting point is not the same.
The following code reproduces the problem (Scala code, but I think you can submit Java code.):
var transformationMatrix = new AffineTransform() override def paintComponent(g: Graphics2D) = { super.paintComponent(g) g.transform(transformationMatrix) g.setColor(Color.RED) g.fill(new Rectangle(0, 0, 1, 1)) g.setTransform(new AffineTransform)
Expected Behavior
The blue rectangle (calculated manually) crosses out the red (calculated by conversion).
Experienced behavior
I admit that my transformMatrix is not an integer, but that should not be a problem, right?
affineTransform = 1.1, 0.0, 520.55 0.0, 1.1, 182.54999999999995 0.0, 0.0, 1.0
Is this a mistake or am I lacking in-depth understanding?
Change You can reproduce the error if you set the transformMatrix parameter to
transformationMatrix = new AffineTransform(1.1, 0.0, 0.0, 1.1, 521.55, 183.54999999999995)
at the beginning of paintComponent. Note that g is of type Graphics2D.