Search code examples
javaswinggraphics2d

java - image is getting jagged when painting on Graphics2D


I am trying to paint a simple image on a Graphics2D. However, when I rotate the image, it shows some edges, and ignores rendering hints.

what is the solution for it? for 45 deg, etc it works fine. but for others, it messes up!

Thanks

enter image description here enter image description here

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import javax.imageio.ImageIO;

public class Canvas {

    public static void main(String args[]) {
        try {
            BufferedImage file = ImageIO.read(new URL("https://live.staticflickr.com/8710/28233783223_2387e00f93_b.jpg"));
            BufferedImage bi = new BufferedImage(1000, 500, BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D g2 = (Graphics2D) bi.getGraphics();
            g2.addRenderingHints(new RenderingHints(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR));
            g2.addRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
            g2.setColor(Color.WHITE);
            g2.fillRect(0, 0, 1000, 500);
            AffineTransform ax = new AffineTransform();
            ax.translate(200, 200);
            ax.rotate(Math.PI * (-318) / 180, 500, 250);
            g2.drawImage(file, ax, null);
            g2.dispose();
            ImageIO.write(bi, "png", new File("text.png"));
        } catch (Exception ex) {
            // error
        }
    }
}

Solution

  • It appears that image painting isn't subject to anti-aliasing. Drawing rectangles is however. By using a TexturePaint with file as the underlying image, we can simply paint a rectangle the size of the image. The resulting image won't have any jagged edges.

    import java.awt.*;
    import java.awt.geom.AffineTransform;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.net.URL;
    import javax.imageio.ImageIO;
    
    class Canvas {
    
        public static void main(String args[]) {
            try {
                BufferedImage file = ImageIO.read(new URL("https://live.staticflickr.com/8710/28233783223_2387e00f93_b.jpg"));
                BufferedImage bi = new BufferedImage(1000, 500, BufferedImage.TYPE_4BYTE_ABGR);
                Graphics2D g2 = (Graphics2D) bi.getGraphics();
                g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                g2.setColor(Color.WHITE);
                g2.fillRect(0, 0, 1000, 500);
                AffineTransform ax = new AffineTransform();
                ax.translate(200, 200);
                ax.rotate(Math.PI * (-318) / 180, 500, 250);
                // g2.drawImage(file, ax, null);
                g2.setPaint(new TexturePaint(file, new Rectangle(0,0, file.getWidth(), file.getHeight())));
                g2.transform(ax);
                g2.fillRect(0,0 , 1000, 500);
                
    g2.dispose();
                ImageIO.write(bi, "png", new File("text.png"));
            } catch (Exception ex) {
                // error
            }
        }
    }