178 lines
4.2 KiB
Java
178 lines
4.2 KiB
Java
package break_out.view;
|
|
|
|
import java.awt.Color;
|
|
import java.awt.Dimension;
|
|
import java.awt.Graphics;
|
|
import java.awt.Graphics2D;
|
|
import java.awt.RenderingHints;
|
|
|
|
import javax.swing.JPanel;
|
|
|
|
import break_out.Constants;
|
|
import net.miginfocom.swing.MigLayout;
|
|
|
|
/**
|
|
* The field represents the board of the game. All components are on the board
|
|
*
|
|
* @author dmlux, modified by iSchumacher, modified by Gruppe 175 (Moritz Henseleit, Ruben Meyer)
|
|
*/
|
|
public class Field extends JPanel {
|
|
|
|
/**
|
|
* Automatic generated serial version UID
|
|
*/
|
|
private static final long serialVersionUID = 2434478741721823327L;
|
|
|
|
/**
|
|
* The connected view object
|
|
*/
|
|
private View view;
|
|
|
|
/**
|
|
* The background color
|
|
*/
|
|
private Color background;
|
|
|
|
/**
|
|
* The constructor needs a view
|
|
*
|
|
* @param view The view of this board
|
|
*/
|
|
public Field(View view) {
|
|
super();
|
|
|
|
this.view = view;
|
|
this.background = new Color(177, 92, 107);
|
|
|
|
setFocusable(true);
|
|
|
|
// Load settings
|
|
initialize();
|
|
}
|
|
|
|
/**
|
|
* Initializes the settings for the board
|
|
*/
|
|
private void initialize() {
|
|
// creates a layout
|
|
setLayout(new MigLayout("", "0[grow, fill]0", "0[grow, fill]0"));
|
|
}
|
|
|
|
/**
|
|
* Change the background color
|
|
*
|
|
* @param color The new color
|
|
*/
|
|
public void changeBackground(Color color) {
|
|
background = color;
|
|
repaint();
|
|
}
|
|
|
|
/**
|
|
* This method is called when painting/repainting the playground
|
|
*
|
|
* @param g the graphics object
|
|
*/
|
|
@Override
|
|
protected void paintComponent(Graphics g) {
|
|
super.paintComponent(g);
|
|
|
|
double w = Constants.SCREEN_WIDTH;
|
|
double h = Constants.SCREEN_HEIGHT;
|
|
|
|
// Setting the dimensions of the playground
|
|
setPreferredSize(new Dimension((int) w, (int) h));
|
|
setMaximumSize(new Dimension((int) w, (int) h));
|
|
setMinimumSize(new Dimension((int) w, (int) h));
|
|
|
|
Graphics2D g2 = (Graphics2D) g;
|
|
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
|
RenderingHints.VALUE_ANTIALIAS_ON);
|
|
|
|
// Setting the background color
|
|
g2.setColor(background);
|
|
g2.fillRect(0, 0, getWidth(), getHeight());
|
|
|
|
// Setting the color for the following components
|
|
g2.setColor(new Color(200, 200, 200));
|
|
|
|
// Calls the method for drawing the ball
|
|
drawBall(g2);
|
|
|
|
// Calls the method for drawing the grid
|
|
drawGrid(g2);
|
|
|
|
// Calls the method for drawing the bottom paddle
|
|
drawPaddleBottom(g2);
|
|
|
|
// Calls the method for drawing the top paddle
|
|
drawPaddleTop(g2);
|
|
|
|
}
|
|
|
|
/**
|
|
* Draws the ball
|
|
*
|
|
* @param g2 The graphics object
|
|
*/
|
|
private void drawBall(Graphics2D g2) {
|
|
g2.fillOval((int) view.getGame().getLevel().getBall().getPosition().getX(),
|
|
(int) view.getGame().getLevel().getBall().getPosition().getY(),
|
|
Constants.BALL_DIAMETER,
|
|
Constants.BALL_DIAMETER);
|
|
}
|
|
|
|
/**
|
|
* Draws the grid
|
|
*
|
|
* @param g2 The graphics object
|
|
*/
|
|
private void drawGrid(Graphics2D g2) {
|
|
|
|
// size of grid blocks
|
|
int blockWidth = Constants.SCREEN_WIDTH / Constants.SQUARES_X;
|
|
int blockHeight = Constants.SCREEN_HEIGHT / Constants.SQUARES_Y;
|
|
|
|
// draw vertical lines
|
|
for (int i = 1; i < Constants.SQUARES_X; i++) {
|
|
g2.drawLine(i * blockWidth, 0, i * blockWidth, Constants.SCREEN_HEIGHT);
|
|
}
|
|
|
|
// draw horizontal lines
|
|
for (int i = 1; i < Constants.SQUARES_Y; i++) {
|
|
g2.drawLine(0, i * blockHeight, Constants.SCREEN_WIDTH, i * blockHeight);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Draws the bottom paddle
|
|
*
|
|
* @param g2 The graphics object
|
|
*/
|
|
private void drawPaddleBottom(Graphics2D g2) {
|
|
// fillRoundRect(x, y, width, height, arcWidth, arcHeight)
|
|
g2.fillRoundRect((int) view.getGame().getLevel().getPaddleBottom().getPosition().getX(),
|
|
(int) view.getGame().getLevel().getPaddleBottom().getPosition().getY(),
|
|
(int) view.getGame().getLevel().getPaddleBottom().getWidth(),
|
|
(int) view.getGame().getLevel().getPaddleBottom().getHeight(),
|
|
10,
|
|
10);
|
|
}
|
|
|
|
/**
|
|
* Draws the top paddle
|
|
*
|
|
* @param g2 The graphics object
|
|
*/
|
|
private void drawPaddleTop(Graphics2D g2) {
|
|
// fillRoundRect(x, y, width, height, arcWidth, arcHeight)
|
|
g2.fillRoundRect((int) view.getGame().getLevel().getPaddleTop().getPosition().getX(),
|
|
(int) view.getGame().getLevel().getPaddleTop().getPosition().getY(),
|
|
(int) view.getGame().getLevel().getPaddleTop().getWidth(),
|
|
(int) view.getGame().getLevel().getPaddleTop().getHeight(),
|
|
10,
|
|
10);
|
|
}
|
|
|
|
}
|