1 import java.awt.*; 2 3 public class mPoint { 4 5 // Instance variables: 6 int x, y, dx, dy; 7 Color color; 8 9 // Constructor: 10 public mPoint(int _x,int _y) { 11 x =_x; y = _y; 12 dx = 1; dy = 1; 13 color = Color.black; 14 } 15 16 // Encapsulate the instance variables: 17 public void setPoint(int _x, int _y) { 18 x = _x; y = _y; 19 } 20 public void setDelta(int _dx, int _dy) { 21 dx = _dx; dy = _dy; 22 } 23 public void setColor(Color _color) { 24 color = _color; 25 } 26 27 // Class methods: 28 public void move(Graphics g) { 29 x += dx; y += dy; 30 paint(g); 31 } 32 public void paint(Graphics g) { 33 g.fillOval(x, y, 3, 3); // not used! 34 } 35 public void checkBoundary(Rectangle rect) { 36 // Calculate new location: 37 int nx = x + dx, ny = y + dy; 38 // Check if new location out of bounds: 39 if ( (nx < rect.x) || (nx >= rect.x + rect.width) ) dx = -dx; 40 if ( (ny < rect.y) || (ny >= rect.y + rect.height) ) dy = -dy; 41 } 42 43 } // end of mPoint class 44