Пример #1
0
  // Called when the mouse wheel is rotated
  public void onMouseWheelMoved(MouseWheelEvent e) {
    // If the wheel rotation is negative it means that it is moved forward.
    // Set move direction = forward, if wheel is moved forward.
    // Otherwise, set move direction = backward
    moveDirection = (e.getWheelRotation() < 0) ? 1 : -1;

    // Set the amount to move = absolute wheel rotation amount * 5 (speed)
    // Here 5 means 5 pixels per wheel rotation step. The higher value, the
    // more speed
    moveAmount += Math.abs(e.getWheelRotation()) * 5;
  }
Пример #2
0
  // Called when the robot must run
  public void run() {

    // Sets the colors of the robot
    // body = black, gun = white, radar = red
    setColors(Color.BLACK, Color.WHITE, Color.RED);

    // Loop forever
    for (; ; ) {
      // Sets the robot to move forward, backward or stop moving depending
      // on the move direction and amount of pixels to move
      setAhead(moveAmount * moveDirection);

      // Decrement the amount of pixels to move until we reach 0 pixels
      // This way the robot will automatically stop if the mouse wheel
      // has stopped it's rotation
      moveAmount = Math.max(0, moveAmount - 1);

      // Sets the robot to turn right or turn left (at maximum speed) or
      // stop turning depending on the turn direction
      setTurnRight(45 * turnDirection); // degrees

      // Turns the gun toward the current aim coordinate (x,y) controlled by
      // the current mouse coordinate
      double angle = normalAbsoluteAngle(Math.atan2(aimX - getX(), aimY - getY()));

      setTurnGunRightRadians(normalRelativeAngle(angle - getGunHeadingRadians()));

      // Fire the gun with the specified fire power, unless the fire power = 0
      if (firePower > 0) {
        setFire(firePower);
      }

      // Execute all pending set-statements
      execute();

      // Next turn is processed in this loop..
    }
  }