Drawing a nice circle in Java

后端 未结 5 885
生来不讨喜
生来不讨喜 2021-02-09 18:06

I\'m using Java Graphics and I keep getting \"ugly\" circles.

Here\'s what my Java program makes \"enter

5条回答
  •  终归单人心
    2021-02-09 18:41

    EDIT: Please see Code Guy's answer below for a solution. This is marked correct because it was Joey Rohan who figured it out initially!


    I got smooth edge when i tried out same thing:

      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,    RenderingHints.VALUE_ANTIALIAS_ON);
    

    enter image description here

    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import javax.imageio.ImageIO;
    
    public class DrawSmoothCircle {
        public static void main(String[] argv) throws Exception {
            BufferedImage bufferedImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = bufferedImage.createGraphics();
    
            g2d.setRenderingHint (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setPaint(Color.green);
            g2d.fillOval(10, 10, 50, 50);
            g2d.dispose();
    
            ImageIO.write(bufferedImage, "png", new File("e:\\newimage.png"));
        }
    }
    

    UPDATE:

    After searching alot:

    There is nothing wrong with the code but,

    Well, unfortunately Java 2D (or at least Sun's current implementation) does not support "soft clipping."

    But Also got a trick for the clips: Follow This link,you can achieve what you are asking for.

    (Also, i got a smooth edge, cause i din't use clip stuff,in my above image)

提交回复
热议问题