// Oval.java // Class for an oval. // Written by THC for CS 5 Lab Assignment 3. import java.awt.*; public class Oval extends Shape { private int left, top; // left and top of Oval private int width, height; // Oval's height and width // Constructor just saves the parameters in the instance variables. public Oval(int left, int top, int width, int height, Color color) { super(color); this.left = left; this.top = top; this.width = width; this.height = height; } // Have the Oval draw itself. public void drawShape(Graphics page) { page.fillOval(left, top, width, height); } // Return true if the Oval contains Point p, false otherwise. public boolean containsPoint(Point p) { return pointInOval(p, left, top, width, height); } // Helper method that returns whether Point p is in an Oval with the given // top left corner and size. private static boolean pointInOval(Point p, int left, int top, int width, int height) { double a = width / 2.0; // half of the width double b = height / 2.0; // half of the height double centerx = left + a; // x-coord of the center double centery = top + b; // y-coord of the center double x = p.x - centerx; // horizontal distance between p and center double y = p.y - centery; // vertical distance between p and center // Now we just apply the standard geometry formula. // (See CRC, 29th edition, p. 178.) return Math.pow(x / a, 2) + Math.pow(y / b, 2) <= 1; } // Have the Oval move itself. public void move(int deltaX, int deltaY) { left += deltaX; top += deltaY; } // Return the Oval's center. public Point getCenter() { return new Point(left + (width / 2), top + (height / 2)); } }