-
Notifications
You must be signed in to change notification settings - Fork 2
/
MancalaPanel.java
72 lines (62 loc) · 1.77 KB
/
MancalaPanel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import src.Model.MancalaModel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
COPYRIGHT (C). All Rights Reserved.
@author Jonathan Sagabaen
@version 1.00
Class representing a mancala display.
*/
public class MancalaPanel extends JPanel implements ChangeListener
{
private int stoneAmount;
private char playerMancala;
private MancalaModel mancalaModel;
private BoardFormatter format;
/**
* Constructor.
* @param mancalaModel model this panel is tied to
* @param playerMancala player this mancala represents
*/
public MancalaPanel(MancalaModel mancalaModel, char playerMancala)
{
this.mancalaModel = mancalaModel;
this.playerMancala = playerMancala;
stoneAmount = mancalaModel.getMancalaValue(playerMancala);
}
/**
* Set display format for this panel.
* @param format format to use
*/
public void setFormat(BoardFormatter format)
{
this.format = format;
}
/**
* Paint this panel.
*/
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(format.getColor());
g2.draw(format.getMancalaShape());
g2.setColor(format.getFillColor());
for (Shape s : format.getMancalaStoneShapes(stoneAmount))
g2.fill(s);
g2.setColor(Color.BLACK);
g2.drawString("Player " + Character.toUpperCase(playerMancala), 50, GameBoard.MANCALA_HEIGHT + 20);
}
/**
* Update panel based on model.
*/
public void stateChanged(ChangeEvent e)
{
stoneAmount = mancalaModel.getMancalaValue(playerMancala);
}
}