Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;

import javax.imageio.ImageIO;

public class Main {

    public static void main(String[] args) throws Exception {
        URL url = new URL("http://www.java2s.com/style/download.png");
        BufferedImage image = ImageIO.read(url);

        int w = image.getWidth();
        int h = image.getHeight();
        Ellipse2D.Double ellipse1 = new Ellipse2D.Double(10, 10, 20, 30);
        Ellipse2D.Double ellipse2 = new Ellipse2D.Double(15, 15, 20, 30);
        Area circle = new Area(ellipse1);
        circle.subtract(new Area(ellipse2));

        BufferedImage result = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = result.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
        g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g.setClip(circle);
        g.drawImage(image, 0, 0, null);
        g.dispose();

        ImageIO.write(result, "png", new File("result.png"));
    }
}