Note that there are some explanatory texts on larger screens.

plurals
  1. POGraphics2D transformation result does not match manual transformation
    primarykey
    data
    text
    <p>I am using Java's Graphics2D to draw on a component using AffineTransform's to manipulate my drawing. Graphics2D offers an method transform for this, which takes an AffineTransform.</p> <p>Sometimes I need to manipulate a point manually without using the builtin-transformation. But when I try to transform a point using the same transformation I gave to Graphics2D.transform sometimes the resulting point is not the same.</p> <p>The following code reproduces the problem (It's Scala code, but I think you can imagine the Java code.):</p> <pre><code> var transformationMatrix = new AffineTransform() /* * transformationMatrix is modified throughout the program * ... */ override def paintComponent(g: Graphics2D) = { super.paintComponent(g) /* 1. transform using graphics transform */ g.transform(transformationMatrix) g.setColor(Color.RED) g.fill(new Rectangle(0, 0, 1, 1)) /* 2. transform point manually */ g.setTransform(new AffineTransform) // reset transformation to standard val p0 = new Point(0, 0) val pDest = new Point() transformationMatrix.transform(p0, pDest) g.setColor(Color.BLUE) g.fill(new Rectangle(pDest.x, pDest.y, 1, 1) } </code></pre> <p><strong>Expected behaviour</strong></p> <p>The blue rectangle (manually calculated) overdraws the red one (calculated by transform).</p> <p><strong>Experienced behaviour</strong></p> <p><img src="https://i.stack.imgur.com/RmEnU.png" alt="The blue rectangle has an offset of 1"></p> <p>I admit that my transformationMatrix is not really integer, but that should'nt be the problem, should it?</p> <pre><code> affineTransform = 1.1, 0.0, 520.55 0.0, 1.1, 182.54999999999995 0.0, 0.0, 1.0 </code></pre> <p>Is this a bug or am I missing some deep insight?</p> <p><strong>Edit</strong>: You can <em>reproduce</em> the bug, if you set transformationMatrix to</p> <pre><code>transformationMatrix = new AffineTransform(1.1, 0.0, 0.0, 1.1, 521.55, 183.54999999999995) </code></pre> <p>at the beginning of paintComponent. Please note, that g is of type Graphics2D.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload