Archive

Archive for July 28, 2009

Java Swing Contains Vs Intersects

July 28, 2009 Leave a comment

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");
	}

}

Categories: Swing Tags: ,
Follow

Get every new post delivered to your Inbox.