/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package tamagotchi.view;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import tamagotchi.model.GameModel;
import javax.swing.JPanel;
import tamagotchi.core.Core;
import tamagotchi.core.Etat;
import tamagotchi.model.GUIModel;
import tamagotchi.model.Tamago;
/**
*
* @author mastersnes
*/
public class GamePanel extends JPanel implements Observer {
private Image back;
private GameModel gameModel;
public GamePanel(final GameModel gameModel) {
this.gameModel = gameModel;
gameModel.addObserver(this);
try {
back = ImageIO.read(new File("ressources/system/gameBackground"));
} catch (final IOException ex) {
Logger.getLogger(MenuPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void paintComponent(Graphics g) {
g = (Graphics2D) g;
final Tamago tamago = gameModel.getTamago();
g.drawImage(Core.scale(back, GUIModel.SCREEN_SIZE.width, GUIModel.SCREEN_SIZE.height), 0, 0, this);
g.setColor(Color.black);
g.drawImage(tamago.getImage(), (GUIModel.SCREEN_SIZE.width / 2) - (tamago.getImage().getWidth(this) / 2), GUIModel.SCREEN_SIZE.height / 4, this);
if (gameModel.isDisplayStat()) {
g.drawImage(gameModel.getEtat(), (2*GUIModel.SCREEN_SIZE.width) / 5, (4 * GUIModel.SCREEN_SIZE.height) / 5, this);
} else {
g.drawImage(gameModel.getMenu(), GUIModel.SCREEN_SIZE.width / 6, (3 * GUIModel.SCREEN_SIZE.height) / 4, this);
}
g.drawImage(gameModel.getHorloge(), 0, 0, this);
if (tamago.getEtatPiece() == Etat.DORT && tamago.getEtatPiece() != Etat.MALADE) {
g.setColor(new Color(100, 100, 100, 100));
g.fillRect(0, 0,GUIModel.SCREEN_SIZE.width , GUIModel.SCREEN_SIZE.height);
}else if (tamago.getEtatPiece() == Etat.MALADE) {
g.setColor(new Color(0, 100, 0, 100));
g.fillRect(0, 0,GUIModel.SCREEN_SIZE.width , GUIModel.SCREEN_SIZE.height);
}
}
@Override
public void update(final Observable o, final Object arg) {
this.gameModel = (GameModel) o;
this.repaint();
}
}