예제 #1
0
  @Override
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D graphics = (Graphics2D) g;

    graphics.setPaint(Color.GREEN);

    Ellipse2D.Double circle =
        new Ellipse2D.Double(ball.getX(), ball.getY(), ball.getDiameter(), ball.getDiameter());

    // graphics.fillOval(ball.getX(), ball.getY(), ball.getDiameter(), ball.getDiameter());
    graphics.fill(circle);
    if (frame.getWidth() != frameWidth || frame.getHeight() != frameHeight) {
      repaint();
      updateSizes(frame.getWidth(), frame.getHeight());
    }

    try {
      Thread.sleep(7);
    } catch (InterruptedException e) {
    }
    if (begin && !grabBall) {
      moveBall();
    } else if (grabBall) {
      followMouse();
    }
  }
예제 #2
0
 public void updateSizes(int width, int height) {
   frameWidth = width;
   frameHeight = height;
   ball.setX((frameWidth - ball.getDiameter()) / 2);
   ball.setY((frameHeight - ball.getDiameter()) / 2);
   //		System.out.println(this.getWidth() +" " + this.getHeight());
   this.setSize(frameWidth, frameHeight);
 }
예제 #3
0
 public void followMouse() {
   if (draggedMouseX != -1 && draggedMouseY != -1) {
     ball.setX(draggedMouseX - ball.getDiameter() / 2);
     ball.setY(draggedMouseY - ball.getDiameter() / 2);
     // System.out.println(draggedMouseX + " "+ draggedMouseY);
     repaint();
   }
 }
예제 #4
0
  public void moveBall() {
    if (moveRight == true) {
      if (ball.getX() < frameWidth - ball.getDiameter()) {
        ball.setX(ball.getX() + 1);

        if (moveDown == true) {
          if (ball.getY() < this.getWidth() - ball.getDiameter()) {
            ball.setY(ball.getY() + 1);
          } else {
            moveDown = false;
          }
        } else {
          if (ball.getY() >= 0) {
            ball.setY(ball.getY() - 1);
          } else {
            moveDown = true;
          }
        }
      } else {
        moveRight = false;
      }
    } else {
      if (ball.getX() >= 0) {
        ball.setX(ball.getX() - 1);

        if (moveDown == true) {
          if (ball.getY() < this.getWidth() - ball.getDiameter()) {
            ball.setY(ball.getY() + 1);
          } else {
            moveDown = false;
          }
        } else {
          if (ball.getY() >= 0) {
            ball.setY(ball.getY() - 1);
          } else {
            moveDown = true;
          }
        }
      } else {
        moveRight = true;
      }
    }

    repaint();
  }