Oval.java
上传用户:sdzznc
上传日期:2022-07-23
资源大小:51k
文件大小:1k
- // Copyright by Scot Drysdale
- package cn.edu.nju.software.grapheditor.shape;
- import java.awt.*;
- public class Oval extends Shape {
- public int x,y,width,height;
- public Oval(Color c) {
- super(c);
- // TODO Auto-generated constructor stub
- }
- public boolean containsPoint(Point p) {
- return pointInOval(p,x,y,width,height);
- }
- public void drawShape(Graphics page) {
-
- page.fillOval(x,y,width,height);// TODO Auto-generated method stub
-
- }
- public Point getCenter() {
- return new Point( (int)(x+width/2),(int)(y+height/2));
- }
- public void move(int deltaX, int deltaY) {
- x+=deltaX;
- y+=deltaY;
-
- }// YOU FILL IN INSTANCE VARIABLES AND METHODS.
- // 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;
- }
- }