From 9850fe44401a1401aef5e6bbf683b507befa03dd Mon Sep 17 00:00:00 2001 From: Timothée Floure Date: Mon, 9 May 2016 16:19:56 +0200 Subject: Implementation of the XBlastComponent Class --- src/ch/epfl/xblast/client/XBlastComponent.java | 171 +++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 src/ch/epfl/xblast/client/XBlastComponent.java (limited to 'src') diff --git a/src/ch/epfl/xblast/client/XBlastComponent.java b/src/ch/epfl/xblast/client/XBlastComponent.java new file mode 100644 index 0000000..ac9f223 --- /dev/null +++ b/src/ch/epfl/xblast/client/XBlastComponent.java @@ -0,0 +1,171 @@ +package ch.epfl.xblast.client; + +import ch.epfl.xblast.PlayerID; + +import javax.swing.*; +import java.awt.*; +import java.util.List; + +/** + * @author Timothée FLOURE (257420) + */ +public final class XBlastComponent extends JComponent { + private static final int PREFERRED_WIDTH = 960; + private static final int PREFERRED_HEIGHT = 688; + private static final int BLOCK_WIDTH = 64; + private static final int BLOCK_HEIGHT = 48; + private static final int SCORE_IMAGE_WIDTH = 48; + private static final int SCORE_IMAGE_HEIGHT = 48; + private static final int TIME_LED_WIDTH = 16; + private static final int TIME_LED_HEIGHT = 16; + + private static final Font font = new Font("Arial", Font.BOLD, 25); + private static final int SCORES_TEXT_VERTICAL_POSITION = 659; + private static final int SCORES_P1_TEXT_HORIZONTAL_POSITION = 96; + private static final int SCORES_P2_TEXT_HORIZONTAL_POSITION = 240; + private static final int SCORES_P3_TEXT_HORIZONTAL_POSITION = 768; + private static final int SCORES_P4_TEXT_HORIZONTAL_POSITION = 912; + + /** + * Displayed gameState. + */ + private GameState gamestate; + + /** + * Display a list of images on the given graphic context. + * + * @param g the graphic context + * @param images the given list of images, ordered in row-major order + */ + private void drawMap(Graphics2D g, List images) { + int x = 0; + int y = 0; + + for (Image img : images) { + if (x + BLOCK_WIDTH > PREFERRED_WIDTH) { + y += BLOCK_HEIGHT; // Or img.getHeight(null) ? + x = 0; + } + + if (img != null) + g.drawImage(img, x, y, null); + + x += BLOCK_WIDTH; // Or img.getWidth(null) ? + } + } + + /** + * Compute the horizontal position of the player in px. + * + * @param player the given player + * @return the horizontal position in pixel + */ + private int playerXPosition(GameState.Player player) { return 4 * player.position().x() - 24; } + + /** + * Compute the vertical position of the player in px. + * + * @param player the given player + * @return the vertical position in pixel + */ + private int playerYPosition(GameState.Player player) { return 3 * player.position().y() - 52; } + + /** + * Draw the players on the graphic context. + * + * @param g the graphic context + * @param players the list of players to be displayed + */ + private void drawPlayers(Graphics2D g, List players) { + for (GameState.Player player : players) { + g.drawImage(player.image(), playerXPosition(player), playerYPosition(player), null); + } + } + + /** + * Draw the scores on the graphic context. + * + * @param g the graphic context + * @param scores the list of images composing the scores to be displayed + */ + private void drawScores(Graphics2D g, List scores) { + int x = 0; + int y = PREFERRED_HEIGHT - SCORE_IMAGE_HEIGHT - TIME_LED_HEIGHT; + for (Image img : scores) { + g.drawImage(img, x, y, null); + x += SCORE_IMAGE_WIDTH; + } + } + + /** + * Write the remaining lives of each player on the graphic context. + * + * @param g the graphic context + * @param players list of players + */ + private void writeScores(Graphics2D g,List players) { + g.setColor(Color.WHITE); + g.setFont(font); + + // Ugly !!!!! + g.drawString(Integer.toString(players.get(0).lives()), SCORES_P1_TEXT_HORIZONTAL_POSITION,SCORES_TEXT_VERTICAL_POSITION); + g.drawString(Integer.toString(players.get(1).lives()), SCORES_P2_TEXT_HORIZONTAL_POSITION,SCORES_TEXT_VERTICAL_POSITION); + g.drawString(Integer.toString(players.get(2).lives()), SCORES_P3_TEXT_HORIZONTAL_POSITION,SCORES_TEXT_VERTICAL_POSITION); + g.drawString(Integer.toString(players.get(3).lives()), SCORES_P4_TEXT_HORIZONTAL_POSITION,SCORES_TEXT_VERTICAL_POSITION); + + } + + /** + * Draw the time "line" on the graphic context + * + * @param g the graphic context + * @param time the list of images composing the line + */ + private void drawTime(Graphics2D g, List time) { + int x = 0; + int y = PREFERRED_HEIGHT - TIME_LED_HEIGHT; + for (Image img : time) { + g.drawImage(img, x, y, null); + x += TIME_LED_WIDTH; + } + } + + /** + * Display the given GameState from the point of view of the given player. + * + * @param gameState GameState to be displayer + * @param player player related to the view + */ + public void setGameState(GameState gameState, PlayerID player) { + this.gamestate = gameState; + repaint(); + } + + /** + * Returns the value of the preferredSize property. + * + * @return the value of the preferredSize property + */ + @Override + public Dimension getPreferredSize() { + return new Dimension(PREFERRED_WIDTH,PREFERRED_HEIGHT); + } + + /** + * Draw the component. + * + * @param g0 the Graphics context + */ + @Override + protected void paintComponent(Graphics g0) { + if (this.gamestate != null) { + Graphics2D g = (Graphics2D) g0; + drawMap(g, this.gamestate.board()); + drawMap(g, this.gamestate.explosions()); + drawPlayers(g, this.gamestate.players()); + drawScores(g, this.gamestate.scores()); + writeScores(g, this.gamestate.players()); + drawTime(g, this.gamestate.ticks()); + }; + } +} -- cgit v1.2.3