// Marty Stepp, CSE 142, Autumn 2008 // This file defines a new class of objects named Point. // // All the code in the class's {} braces represents what contents // should be replicated inside of each Point object. This includes the methods // written today: each Point object has a copy of each method that can access // that Point object's fields. // import java.awt.*; // for Graphics public class Point { int x; // EACH Point object should have a variable int y; // inside it named x, and a variable named y // Draws a point on a DrawingPanel. public void draw(Graphics g) { g.fillOval(x, y, 3, 3); g.drawString("(" + x + ", " + y + ")", x, y); } // Shifts the point's x/y coordinates by the given amounts. public void translate(int dx, int dy) { x += dx; y += dy; // or, setLocation(x + dx, y + dy); } // Sets the point's x/y coordinates to be the given values. public void setLocation(int newX, int newY) { x = newX; y = newY; } // Computes the distance between this point and the given other point p2. public double distance(Point p2) { int dx = x - p2.x; int dy = y - p2.y; double distance = Math.sqrt(dx*dx + dy*dy); return distance; } // Computes the distance between this point and the origin (0, 0). public double distanceFromOrigin() { Point origin = new Point(); // 0, 0 return distance(origin); } }