Graphics Context misaligned on first paint

The AffineTransform associated with the graphics context passed to paintComponent() is not always the identity transform. For reasons that aren’t clear, the m12 entry has the value 38.0 initially and after resizing the enclosing frame. Trivially, one can modify the copy supplied by g.create(). Graphics2D g2d = (Graphics2D) g.create(); AffineTransform at = g2d.getTransform(); at.translate(originX, originY); … Read more

Scaling/Translating a Shape to a given Rectangle using AffineTransform

Note that AffineTransform transformations are concatenated “in the most commonly useful way”, which may be regarded as last in, first-out order. The effect can be seen in this example. Given the sequence below, the resulting Shape is first rotated, then scaled and finally translated. at.translate(SIZE/2, SIZE/2); at.scale(60, 60); at.rotate(Math.PI/4); return at.createTransformedShape(…);

Rotate an image in java

You need to be using trigonometry to determine the correct width/height, using transparency to prevent the black area, and I think the Transform is wrong, which is making it off center. Try this: public static BufferedImage rotate(BufferedImage image, double angle) { double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle)); int w = image.getWidth(), h = image.getHeight(); … Read more

rotating coordinate plane for data and text in Java

One approach is shown in SineTest. In outline, Save the graphics context’s transform. Graphics2D g2d = (Graphics2D) g; AffineTransform at = g2d.getTransform(); Translate the origin to the center. g2d.translate(w / 2, h / 2); Invert the y-axis. g2d.scale(1, -1); Render using cartesian coordinates. Restore the transform for conventional rendering. g2d.setTransform(at);