Skip to content

Commit

Permalink
Added LevelPanel, a panel for choosing the level in the view
Browse files Browse the repository at this point in the history
  • Loading branch information
soficabe committed Feb 15, 2024
1 parent a7c945b commit 353edee
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 6 deletions.
17 changes: 17 additions & 0 deletions src/main/java/pvzclone/controller/api/Controller.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package pvzclone.controller.api;

import java.util.Optional;
import java.util.Set;

import pvzclone.input.api.ViewEventListener;
Expand Down Expand Up @@ -52,4 +53,20 @@ public interface Controller extends ViewEventListener {
* @return points obtained at the moment.
*/
int getSunScore();


/**
* Sets the number of the Level to the number of the argument passed.
*
* @param numberOfTheLevel
*/
void chooseLevel(int numberOfTheLevel);

/**
* Retrieves the number of the chosen level if the level was chosen,
* if not returns an empty Optional.
*
* @return number of the level the we chose;
*/
Optional<Integer> getChosenLevel();
}
13 changes: 13 additions & 0 deletions src/main/java/pvzclone/controller/impl/ControllerImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pvzclone.controller.impl;

import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

import pvzclone.controller.api.Controller;
Expand All @@ -24,11 +25,13 @@ public final class ControllerImpl implements Controller {
private World world;
private View view;
private Game game;
private Optional<Integer> chosenLevel;

@Override
public void initGame() {
this.world = new WorldImpl();
this.view = new SwingViewImpl(this);
this.chosenLevel = Optional.empty();
}

@Override
Expand Down Expand Up @@ -99,6 +102,16 @@ public int getSunScore() {
return game == null ? 0 : game.getGameState().getSunScore();
}

@Override
public void chooseLevel(final int numberOfTheLevel) {
this.chosenLevel = Optional.of(numberOfTheLevel);
}

@Override
public Optional<Integer> getChosenLevel() {
return this.chosenLevel;
}

/*
* @Override
* public void notifyWorldEvent(WorldEvent ev) {
Expand Down
72 changes: 72 additions & 0 deletions src/main/java/pvzclone/view/impl/LevelPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package pvzclone.view.impl;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.ImageIcon;
import javax.swing.JButton;


public class LevelPanel extends GenericPanel {
private static final int LAYOUT_HGAP = 20;
private static final int LAYOUT_VGAP = 50;
private static final String BUTTON_TEXTURE = "/images/tombstoneTexture.jpg";
private static final Dimension MENU_BUTTON_DIMENSION = new Dimension(
SwingViewImpl.APPLICATION_WIDTH / 6, SwingViewImpl.APPLICATION_HEIGHT / 8);
private final SwingViewImpl parent;

/**
* Level Panel Constructor.
*
* @param parent the application's view.
* @param backgroundSource the background image source.
* @see {@link GenericPanel}
*/
public LevelPanel(SwingViewImpl parent, String backgroundSource) {
super(parent, backgroundSource);
this.parent = parent;
this.setLayout(
new FlowLayout(FlowLayout.CENTER, LAYOUT_HGAP, SwingViewImpl.APPLICATION_HEIGHT / 2 - LAYOUT_VGAP));
final ImageIcon texture = new ImageIcon(getClass().getResource(BUTTON_TEXTURE));

for(int i=0; i<5; i++) {
final int numberOfTheLevel = i;
final JButton button = new JButton(String.valueOf(numberOfTheLevel + 1), texture);
this.setButton(button);
button.addActionListener(e -> {
this.parent.getController().chooseLevel(numberOfTheLevel);
this.parent.setScene(SwingViewImpl.MENU_PANEL_CONSTRAINT);
});
this.add(button);
}
}

/**
* Sets a Button's settings.
*
* @param button the chosen button.
*/
private void setButton(final JButton button) {
button.setHorizontalTextPosition(JButton.CENTER);
button.setVerticalTextPosition(JButton.CENTER);
button.setPreferredSize(MENU_BUTTON_DIMENSION);
button.setFont(new Font(null, Font.BOLD, 16));
button.setForeground(Color.BLACK);
}

/**
* Used for {@link pvzclone.view.impl.GenericPanel#update(Graphics)}.
*/
@Override
public void paintComponent(final Graphics g) {
super.paintComponent(g);

final Graphics2D g2D = (Graphics2D) g;
g2D.drawImage(this.getBackgroundImage(), 0, 0, null);
}

}
17 changes: 11 additions & 6 deletions src/main/java/pvzclone/view/impl/MenuPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
Expand All @@ -25,6 +24,7 @@ public class MenuPanel extends GenericPanel {
private static final String BUTTON_TEXTURE = "/images/tombstoneTexture.jpg";
private static final Dimension MENU_BUTTON_DIMENSION = new Dimension(
SwingViewImpl.APPLICATION_WIDTH / 6, SwingViewImpl.APPLICATION_HEIGHT / 8);
private final SwingViewImpl parent;

/**
* Menu Panel Constructor.
Expand All @@ -35,6 +35,7 @@ public class MenuPanel extends GenericPanel {
*/
public MenuPanel(final SwingViewImpl parent, final String backgroundSource) {
super(parent, backgroundSource);
this.parent = parent;
this.setLayout(
new FlowLayout(FlowLayout.CENTER, LAYOUT_HGAP, SwingViewImpl.APPLICATION_HEIGHT / 2 - LAYOUT_VGAP));
final ImageIcon texture = new ImageIcon(getClass().getResource(BUTTON_TEXTURE));
Expand All @@ -47,12 +48,16 @@ public MenuPanel(final SwingViewImpl parent, final String backgroundSource) {
this.setButton(startButton);
this.setButton(exitButton);

levelButton.addActionListener(e -> {
// parent.setScene(SwingViewImpl.LEVEL_PANEL_CONSTRAINT);
});
startButton.addActionListener(e -> {
parent.setScene(SwingViewImpl.GAME_PANEL_CONSTRAINT);
parent.getController().callMainloop();
if(this.parent.getController().getChosenLevel().isPresent()) {
this.parent.setScene(SwingViewImpl.GAME_PANEL_CONSTRAINT);
this.parent.getController().callMainloop();
}
});
levelButton.addActionListener(e -> {
if(this.parent.getController().getChosenLevel().isEmpty()) {
this.parent.setScene(SwingViewImpl.LEVEL_PANEL_CONSTRAINT);
}
});
exitButton.addActionListener(e -> {
final int n = JOptionPane.showConfirmDialog(parent.getFrame(), "Do you really want to quit?",
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/pvzclone/view/impl/SwingViewImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,18 @@ public final class SwingViewImpl implements View {
/** Menu Panel's Textual Constraint. */
public static final String MENU_PANEL_CONSTRAINT = "MENU";

/** Level Panel's Textual Constraint. */
public static final String LEVEL_PANEL_CONSTRAINT = "LEVEL";

/** Game Panel's Textual Constraint. */
public static final String GAME_PANEL_CONSTRAINT = "GAME";

/** Menu Panel's Background Image Source. */
private static final String MENU_BACKGROUND = "/images/menuBackground.jpeg";

/** Level Panel's Background Image Source. */
private static final String LEVEL_BACKGROUND = "/images/menuBackground.jpeg";

/** Game Panel's Background Image Source. */
private static final String GAME_BACKGROUND = "/images/gameBackground.png";

Expand All @@ -55,6 +61,7 @@ public final class SwingViewImpl implements View {

private final MenuPanel menuPanel;
private final GamePanel gamePanel;
private final LevelPanel levelPanel;

private Pair<Double, Double> scale;

Expand Down Expand Up @@ -83,11 +90,13 @@ public void windowClosing(WindowEvent e) {
this.frame.setResizable(IS_APPLICATION_RESIZABLE);

this.menuPanel = new MenuPanel(this, MENU_BACKGROUND);
this.levelPanel = new LevelPanel(this, LEVEL_BACKGROUND);
this.gamePanel = new GamePanel(this, GAME_BACKGROUND);

this.panel = new JPanel(sceneManager);
this.panel.setSize(APPLICATION_WIDTH, APPLICATION_HEIGHT);
this.panel.add(menuPanel, MENU_PANEL_CONSTRAINT);
this.panel.add(levelPanel, LEVEL_PANEL_CONSTRAINT);
this.panel.add(gamePanel, GAME_PANEL_CONSTRAINT);
this.panel.addComponentListener(new ComponentListener() {

Expand Down

0 comments on commit 353edee

Please sign in to comment.