Beispiel #1
0
 private void drawSnake(Graphics2D g) {
   g.setColor(panel.getColor());
   for (BodyPart bodyPart : snake.getBody()) {
     g.fillRect(
         bodyPart.getX() * CELL_SIZE - CELL_SIZE,
         game.getScreenSize().height - (bodyPart.getY() * CELL_SIZE),
         CELL_SIZE,
         CELL_SIZE);
   }
 }
Beispiel #2
0
 private void drawApple(Graphics2D g) {
   Color c = new Color(13, 111, 28);
   g.setColor(c);
   Image img1 = Toolkit.getDefaultToolkit().getImage("/apple.png");
   g.drawImage(img1, apple.getX(), apple.getY(), CELL_SIZE, CELL_SIZE, null);
   g.fillRect(
       apple.getX() * CELL_SIZE - CELL_SIZE,
       game.getScreenSize().height - (apple.getY() * CELL_SIZE),
       CELL_SIZE,
       CELL_SIZE);
 }
Beispiel #3
0
  @Override
  public void draw(Graphics2D g) {
    g.setColor(Color.lightGray);
    g.fillRect(0, 0, game.getScreenSize().width, game.getScreenSize().height);
    drawSnake(g);
    drawApple(g);
    g.setFont(new Font("Default", Font.BOLD, 12));

    String message = "Press <F5> to save, <F6> to load.";
    Rectangle2D messageBounds =
        g.getFontMetrics()
            .getStringBounds(
                message,
                g); // g.getFontMetrics().getStringBounds - ��������� �������� ������ ��������
                    // ������ � ��������
    int messageWidth = (int) (messageBounds.getWidth());
    int messageHeight = (int) (messageBounds.getHeight());

    g.drawString(message, 15, 10);

    if (paused) {
      g.setFont(new Font("Default", Font.BOLD, 60));
      g.setColor(Color.white);
      message = "Pause";
      messageBounds =
          g.getFontMetrics()
              .getStringBounds(
                  message,
                  g); // g.getFontMetrics().getStringBounds - ��������� �������� ������ ��������
                      // ������ � ��������
      messageWidth = (int) (messageBounds.getWidth());
      messageHeight = (int) (messageBounds.getHeight());
      g.drawString(
          message,
          game.getScreenSize().width / 2 - messageWidth / 2,
          game.getScreenSize().height / 2 - messageHeight / 2);
    }
  }