package jbrickbreaker.view;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.font.TextLayout;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import jbrickbreaker.JBrickBreaker;
import jbrickbreaker.UserPreferences;
import jbrickbreaker.model.GameManager;
import jbrickbreaker.model.Pad;
import jbrickbreaker.model.bonuses.Bonus;
/**
* This class represents the main part for the user. This is where the game
* happens.
*
* Date: 12 Dec 2010 Time: 16:02:24
*
* @author Thomas Michel
*/
public class GameView extends JPanel implements IBonus, ILanguage {
private GameManager gameManager;
private boolean paused = false;
private Timer timer;
private boolean gameOver = false;
private int gameOverY = 0;
private boolean playerWon = false;
private Color color;
private JBrickBreaker brickbreacker;
private boolean displayStartHelp = true;
/**
*
* @author Thomas Michel
*/
public GameView(GameManager game, JBrickBreaker brickbreacker) {
this.brickbreacker = brickbreacker;
paused = false;
gameOver = false;
playerWon = false;
displayStartHelp = true;
color = UserPreferences.getObjColor(UserPreferences.BACKGROUND);
setBorder(BorderFactory.createMatteBorder(0, 1, 0, 1, this.color));
gameManager = game;
setMaximumSize(new Dimension(JBrickBreaker.GAME_WIDTH,
JBrickBreaker.GAME_HEIGHT));
setPreferredSize(new Dimension(JBrickBreaker.GAME_WIDTH,
JBrickBreaker.GAME_HEIGHT));
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if (isPaused()) {
repaint(); //to draw the PAUSED text
try {
synchronized (GameView.this) {
GameView.this.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
gameManager.next();
repaint();
}
}, 0, 10);
}
/**
* This method prevents the game from continuing. It basically interrupts
* the update timer.
*/
public void stop() {
timer.cancel();
}
public void playerWins() {
playerWon = true;
endOfGame();
}
/**
* this method is called internally when the game is finished, regardless of
* whether the player won or lost.
*/
private void endOfGame() {
stop();
displayStartHelp = false;
gameOverY = getHeight() - 50;
Timer gameoverTimer = new Timer();
gameoverTimer.schedule(new TimerTask() {
@Override
public void run() {
if (gameOverY >= 0 && gameOverY > getHeight() / 2) {
gameOverY--;
repaint();
} else
cancel();
}
}, 0, 20);
}
/**
* This method is called when the player lost. It calls
* {@link GameView#endOfGame()} and sets the appropriate flags.
*/
public void gameOver() {
gameOver = true;
endOfGame();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(UserPreferences.getObjColor(UserPreferences.BACKGROUND));
g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
g.setColor(Color.BLACK);
// g.drawRect(0, 0, getWidth()-1, getHeight()-1);
gameManager.drawElements((Graphics2D) g.create());
Font f = g.getFont().deriveFont(36f);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
Color oldC = g2d.getColor();
Color c = new Color(255-color.getRed(), 255-color.getGreen(), 255-color.getBlue());
if (gameOver || playerWon) {
g2d.setColor(c);
String t = gameOver ? brickbreacker.getString("GameView.0") : brickbreacker.getString("GameView.1"); //$NON-NLS-1$ //$NON-NLS-2$
TextLayout tl = new TextLayout(t, f, g2d.getFontRenderContext());
tl.draw(g2d, (float) (getWidth() - tl.getBounds().getWidth()) / 2,
gameOverY);
g2d.setColor(oldC);
}
if(displayStartHelp){
g2d.setColor(c);
String text = MessageFormat.format(brickbreacker.getString("GameView.2"), //$NON-NLS-1$
KeyEvent.getKeyText(brickbreacker.keyboardController.keyEject));
TextLayout tl = new TextLayout(text, f, g2d.getFontRenderContext());
tl.draw(g2d, (float) (getWidth() - tl.getBounds().getWidth()) / 2,
(float)(Pad.getInstance().getY() - 50));
g2d.setColor(oldC);
}
if(isPaused()){
g2d.setColor(c);
String text = brickbreacker.getString("GameView.3"); //$NON-NLS-1$
TextLayout tl = new TextLayout(text, f, g2d.getFontRenderContext());
tl.draw(g2d, (float) (getWidth() - tl.getBounds().getWidth()) / 2,
getHeight()/2);
g2d.setColor(oldC);
}
}
public void setDisplayStartHelp(boolean b){
displayStartHelp = b;
}
public boolean isDisplayingStartHelp(){
return displayStartHelp;
}
public void setPaused(boolean p) {
if (!p) {
synchronized (this) {
notifyAll();
}
}
this.paused = p;
}
public boolean isPaused() {
return paused;
}
public void setColor(Color color) {
this.color = color;
setBorder(BorderFactory.createMatteBorder(0, 1, 0, 1, this.color));
}
@Override
public void updateBonus(Bonus b) {
// nothing to do with this implementation
}
@Override
public void updateLanguage(Locale lang) {
// noting to do (yet...) since it's all managed by drawString() in
// paintComponent
}
}