You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Ball.java 844B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package sample.duplicate;
  2. import java.awt.Graphics;
  3. import java.awt.Color;
  4. public class Ball {
  5. private int x, y;
  6. private Color color;
  7. private int radius = 30;
  8. private boolean isBackup = false;
  9. public Ball(int x, int y) {
  10. move(x, y);
  11. changeColor(Color.orange);
  12. }
  13. // This constructor is for a backup object.
  14. public Ball(Ball b) {
  15. isBackup = true;
  16. }
  17. // Adjust the position so that the backup object is visible.
  18. private void adjust() {
  19. if (isBackup) {
  20. this.x += 50;
  21. this.y += 50;
  22. }
  23. }
  24. public void paint(Graphics g) {
  25. g.setColor(color);
  26. g.fillOval(x, y, radius, radius);
  27. }
  28. public void move(int x, int y) {
  29. this.x = x;
  30. this.y = y;
  31. adjust();
  32. }
  33. public void changeColor(Color color) {
  34. this.color = color;
  35. }
  36. }