The Java – graphics2d conversion result does not match the manual conversion

I use Java's graphics2d to manipulate my drawing on a component using affinetransform

Sometimes I need to manually operate a point without using built-in conversion But when I tried to convert a point using the same transformation, I gave graphics2d Sometimes the result of transform is different

The following code reproduces this problem (it's Scala code, but I think you can imagine java 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,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)
   }

Expected behavior

Blue rectangle (calculated manually) through red rectangle (calculated by transformation)

Experienced behavior

I admit that my transformation matrix is not a real integer, but that shouldn't be a problem, should it?

affineTransform = 1.1,0.0,520.55
                     0.0,1.1,182.54999999999995
                     0.0,1.0

Is this a mistake or did I miss some insights?

Edit: if the transformation matrix is set to, the error can be reproduced

transformationMatrix = new AffineTransform(1.1,521.55,183.54999999999995)

At the beginning of paintcomponent Note that G is a graphics2d type

Solution

Well, you did two different things

In (1), you want to shape the transformation that produces decimal coordinates (independent of rectangle instead of rectangle 2D. Double) It's just aliased because you don't set specific rendering hints (renderinghints.key_animation – > renderinghints.value_antias_on and renderinghints.key_stroke_control – > renderinghints.value_stroke_pure)

In (2), you put a point in the transformation and cast it into alias coordinates (point instead of point2d. Double) Then a rectangle is constructed continuously from that point

Obviously, very different things can happen behind the scenes. I don't expect to convert to integer points at all, but drawing floating-point shapes in the context of aliasing graphics will produce the same results

(no test) I guess the effective equivalence statement of (1) is

g.fill(transformationMatrix.createTransformedShape(new Rectangle(0,1)))
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>