AreaSubtract.java Source code

Java tutorial

Introduction

Here is the source code for AreaSubtract.java

Source

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;

import javax.swing.JApplet;
import javax.swing.JFrame;

public class AreaSubtract extends JApplet {
    Ellipse2D.Double stem = new Ellipse2D.Double();
    Area st1 = new Area(stem);
    Area st2 = new Area(stem);

    public void init() {
        setBackground(Color.white);
    }

    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        double halfWdith = getSize().width / 2;
        double halfHeight = getSize().height / 2;

        stem.setFrame(halfWdith, halfHeight - 42, 40.0, 40.0);
        st1 = new Area(stem);
        stem.setFrame(halfWdith + 3, halfHeight - 47, 50.0, 50.0);
        st2 = new Area(stem);
        st1.subtract(st2);
        g2.fill(st1);

        g2.setColor(Color.yellow);

    }

    public static void main(String s[]) {
        JFrame f = new JFrame("Pear");
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        JApplet applet = new AreaSubtract();
        f.getContentPane().add("Center", applet);
        applet.init();
        f.pack();
        f.setSize(new Dimension(150, 200));
        f.show();
    }

}