/**
   * Cálculo de quanto tempo transcorreu entre duas chamadas ao update. Na criação do JFrame foi
   * denifido um atributo chamado time. Ele será usado para representar o momento em que o método
   * update() foi chamado pela ultima vez.
   */
  @Override
  public void processLogics() {
    // Calcula o tempo entre dois updates
    long time = System.currentTimeMillis() - previous;

    // Chama o update dos sprites, no caso, só a bola
    ball.update(time);

    // Grava o tempo na saída do método
    previous = System.currentTimeMillis();
  }
  /** @see java.awt.Container#paint(java.awt.Graphics) */
  public void paint(Graphics graphics) {
    // create an offscreen buffer to render the map

    System.out.println();

    if (buffer == null) {
      buffer = new BufferedImage(600, 600, BufferedImage.TYPE_INT_ARGB);
    }
    Graphics g = buffer.getGraphics();

    g.clearRect(0, 0, 600, 600);
    g.translate(50, 50);

    // cycle through the tiles in the map drawing the appropriate
    // image for the terrain and units where appropriate
    for (int x = 0; x < map.getWidthInTiles(); x++) {
      for (int y = 0; y < map.getHeightInTiles(); y++) {
        g.drawImage(tiles[map.getTerrain(x, y)], x * 16, y * 16, null);
        if (map.getUnit(x, y) != 0) {
          g.drawImage(tiles[map.getUnit(x, y)], x * 16, y * 16, null);
        } else {
          if (path != null) {
            if (path.contains(x, y)) {
              g.setColor(Color.BLUE);
              g.fillRect((x * 16) + 4, (y * 16) + 4, 7, 7);
            }
          }
        }
      }
    }

    /* Movimentação dos Objetos */
    if (move) {
      for (int i = 0; i < path.getLength(); i++) {
        try {
          g.drawImage(
              tiles[map.getUnit(selectedx, selectedy)], path.getX(i) * 16, path.getY(i) * 16, null);
          graphics.drawImage(buffer, 0, 0, null);
          Thread.sleep(100);
        } catch (InterruptedException e) {
          System.out.println("Método Paint! " + e.getMessage());
          e.printStackTrace();
        }
      }

      move = false;
    }

    // if a unit is selected then draw a box around it
    if (selectedx != -1) {
      g.setColor(Color.black);
      g.drawRect(selectedx * 16, selectedy * 16, 15, 15);
      g.drawRect((selectedx * 16) - 2, (selectedy * 16) - 2, 19, 19);
      g.setColor(Color.white);
      g.drawRect((selectedx * 16) - 1, (selectedy * 16) - 1, 17, 17);
    }

    // Logica do TIRO
    g =
        g.create(
            getInsets().right,
            getInsets().top,
            getWidth() - getInsets().left,
            getHeight() - getInsets().bottom);

    if (ball != null) {
      ball.draw((Graphics2D) g); // Desenhamos a bola
    }

    g.dispose(); // Liberamos o contexto criado.

    // finally draw the buffer to the real graphics context in one
    // atomic action
    graphics.drawImage(buffer, 0, 0, null);
  }