/*
*
* Copyright (c) 2010, L.L., Inc.
*
* All rights reserved.
*
*/
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
public class GUI {
// double buffer voor grafische interface
private BufferedImage offscreenImage;
private Graphics offscr;
// afstanden om text op goeie plaats te krijgen
int x, y, distance, fontSize;
public GUI() {
// double buffer
offscreenImage = new BufferedImage(Level.width, Level.height,
BufferedImage.TYPE_INT_ARGB);
offscr = offscreenImage.getGraphics();
x = Level.width / 5;
y = Level.height / 3;
distance = Level.height / 6;
fontSize = Level.size * 2;
}
/**
* laad het board in het begin van het spel en tijdens het spel de hele tijd
* opnieuw en tekent het op de offscr
*/
// Heel belangrijk om .equals() te gebruiken, anders werkt methode niet
public void drawBoard(String[][] board, String direction) {
int i, j;
for (i = 0; i < Level.blocksVer; i++) {
for (j = 0; j < Level.blocksHor; j++) {
// Wall
if (board[i][j].equals("w")) {
offscr.setColor(Color.black);
}
// Background
else if (board[i][j].equals("b")) {
offscr.setColor(Color.blue);
}
// Snake
else if (board[i][j].equals("s")) {
offscr.setColor(Color.red);
}
// Head of snake
else if (board[i][j].equals("h")) {
offscr.setColor(Color.red);
}
// Diamond
else if (board[i][j].equals("d")) {
offscr.setColor(Color.cyan);
}
// Endgate
else if (board[i][j].equals("e")) {
offscr.setColor(Color.yellow);
}
// Gate
else if (board[i][j].startsWith("g")) {
offscr.setColor(Color.magenta);
} else
System.out.println("foutje" + board[i][j]);
offscr.fillRect(j * Level.size, i * Level.size, Level.size,
Level.size);
if (board[i][j].equals("h")) {
snakeHead(i, j, direction);
}
}
}
}
public void snakeHead(int i, int j, String direction) {
offscr.setColor(Color.black);
if (direction == "up") {
offscr.fillRect(j * Level.size + Level.size / 5, i * Level.size
+ Level.size / 5, Level.size / 4, Level.size / 4);
offscr.fillRect(j * Level.size + 3 * Level.size / 5, i * Level.size
+ Level.size / 5, Level.size / 4, Level.size / 4);
} else if (direction == "down") {
offscr.fillRect(j * Level.size + Level.size / 5, i * Level.size + 3
* Level.size / 5, Level.size / 4, Level.size / 4);
offscr.fillRect(j * Level.size + 3 * Level.size / 5, i * Level.size
+ 3 * Level.size / 5, Level.size / 4, Level.size / 4);
} else if (direction == "right") {
offscr.fillRect(j * Level.size + 3 * Level.size / 5, i * Level.size
+ Level.size / 5, Level.size / 4, Level.size / 4);
offscr.fillRect(j * Level.size + 3 * Level.size / 5, i * Level.size
+ 3 * Level.size / 5, Level.size / 4, Level.size / 4);
} else if (direction == "left") {
offscr.fillRect(j * Level.size + Level.size / 5, i * Level.size
+ Level.size / 5, Level.size / 4, Level.size / 4);
offscr.fillRect(j * Level.size + Level.size / 5, i * Level.size + 3
* Level.size / 5, Level.size / 4, Level.size / 4);
}
}
/**
* Menu: startmenu om spel te beginnen
*/
public void menu(int speed, String[][] board, boolean cont, String code,
String direction) {
drawBoard(board, direction);
offscr.setColor(Color.black);
Font font1 = new Font("Helvetica", Font.BOLD, fontSize);
offscr.setFont(font1);
offscr.drawString("[P]lay", x, y);
offscr.drawString("[S]peed: " + convertSpeed(speed), x, y + distance);
if (cont != true) {
offscr.drawString("[C]ontinue", x, y + 2 * distance);
} else if (cont == true) {
offscr.drawString(code, x, y + 2 * distance);
}
}
public void level(String[][] board, int level, String code, String direction) {
drawBoard(board, direction);
offscr.setColor(Color.black);
Font font1 = new Font("Helvetica", Font.BOLD, fontSize);
offscr.setFont(font1);
offscr.drawString("Level: " + level, x, y);
offscr.drawString("Code: " + code, x, y + distance);
offscr.drawString("Press Enter to play", x, y + 2 * distance);
}
public void gameOver(String[][] board, String direction) {
drawBoard(board, direction);
offscr.setColor(Color.black);
Font font1 = new Font("Helvetica", Font.BOLD, fontSize);
offscr.setFont(font1);
offscr.drawString("Game Completed!", x, y + distance / 2);
offscr.drawString("Congratulations!", x, y + 2 * distance);
}
public String convertSpeed(int speed) {
String speediness = null;
if (speed == 25) {
speediness = "Damn Fast";
} else if (speed == 50) {
speediness = "Fast";
} else if (speed == 100) {
speediness = "Medium";
} else if (speed == 150) {
speediness = "Slow";
} else if (speed == 200) {
speediness = "Very Slow";
}
return speediness;
}
// getter voor offscreenimage
public Image getOffscr() {
return offscreenImage;
}
}
|