Java Swing Contains Vs Intersects

Contains: Is it on the boundary line or not
Intersects: Given point is in side the shape or not. If it is line, this is same as contains.
Even better example is available at http://forums.java.net/jive/thread.jspa?threadID=56250&tstart=60

import java.awt.geom.Ellipse2D;

/**
 * This examples helps to understand contains and intersects concepts.
 * 
 * @author Bhavani P Polimetla
 * @since Jul-28-2009
 * 
 */
public class TestPoint {

	public static void main(String[] args) {
		testContains();
		testIntersects();
	}

	public static void testContains() {
		Ellipse2D.Double circle = new Ellipse2D.Double(230, 277, 7, 7);
		System.out.println(circle.x + " " + circle.y);
		boolean isContains = circle.contains(230.0, 277.0);
		if (isContains)
			System.out.println("Point is on the cicle");
		else
			System.out.println("Point is not on the circle");
	}

	public static void testIntersects() {
		Ellipse2D.Double circle = new Ellipse2D.Double(230, 277, 7, 7);
		System.out.println(circle.x + " " + circle.y);
		int x = 230;
		int y = 278; // Inside point
		boolean isContains = circle.intersects(x, y, x, y);
		if (isContains)
			System.out.println("Point is inside the circle");
		else
			System.out.println("Point is Outside the circle");

		x = 0;
		y = 0; // Outside point
		isContains = circle.intersects(x, y, x, y);
		if (isContains)
			System.out.println("Point is inside the circle");
		else
			System.out.println("Point is Outside the circle");
	}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s