Files
Prog_1/Reversi/ReversiField.java
Felix Steghofer 3db4f94e6a init
2022-12-04 01:25:28 +01:00

83 lines
1.9 KiB
Java

package reversi.view;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import javax.swing.JPanel;
import reversi.model.Field;
import reversi.model.Player;
/**
* Class for a single play field panel. Paints the circles with the color of
* the Player.
*/
public class ReversiField extends JPanel {
private static final long serialVersionUID = -5762403586769495603L;
private int height;
private int width;
private int y;
private int x;
private final Field field;
private Player player;
/**
*
* @param field <code>Field</code> with coordinates of the play field.
*/
public ReversiField(final Field field) {
this.field = field;
setPlayer(player);
}
/**
* {@inheritDoc}
*/
@Override
public final void paintComponent(final Graphics g) {
super.paintComponent(g);
height = (int) (super.getHeight() * 0.8);
width = (int) (super.getWidth() * 0.8);
y = (int) (width * 0.1 + 2);
x = (int) (height * 0.1 + 2);
if (player != null && player.getType()) {
g.setColor(Color.BLUE);
g.fillOval(y, x, width, height);
} else if (player != null && !player.getType()) {
g.setColor(Color.RED);
g.fillOval(y, x, width, height);
}
}
/**
*
* @return <code>Field</code> with coordinates of the play field.
*/
public final Field getField() {
return this.field;
}
/**
* Set the listener created in the controller.
*
* @param ma Mouseadapter
*/
public final void setListener(final MouseAdapter ma) {
this.addMouseListener(ma);
}
/**
* Set a player on a field (draw the circle in right color).
*
* @param player Player that is set.
*/
public void setPlayer(Player player) {
this.player = player;
repaint();
}
}