Java Reference
In-Depth Information
7.
import
javax.swing.JOptionPane;
8.
9.
public class
BlockPuzzlePanel
extends
JPanel{
10.
11.
private
BoardModel boardMod;
12.
private
ConfigurationModel currentConf;
13.
private int
noOfRows, noOfCols;
14.
private int
columnWidth,rowHeight;
15.
16.
public
BlockPuzzlePanel( BoardModel bm){
17.
boardMod = bm;
18.
noOfRows = bm.getNoOfRows();
19.
noOfCols = bm.getNoOfCols();
20.
this
.setPreferredSize(
new
Dimension(300,300));
this
.setBackground(Color.white);
21.
}
22.
23.
24.
public void
paintComponent(Graphics g){
super
.paintComponent(g);
25.
int
w=getWidth();
26.
int
h=getHeight();
27.
columnWidth = w/noOfCols;
28.
29.
rowHeight = h/noOfRows;
30.
currentConf = boardMod.getCurrentConfiguration();
31.
for
(
int
r=0; r < noOfRows; r++){
32.
for
(
int
c=0; c < noOfCols; c++){
33.
if
(currentConf.getBlockNo(r,c) != 0){
34.
drawBlock(r,c,currentConf.getBlockNo(r,c),g);
35.
}
36.
else
37.
{
38.
drawMissingBlock(r,c,g);
39.
}
40.
}//
for c
41.
}//
for r
42.
}
43.
44.
public void
makeMove(MoveModel move){
if
(boardMod.moveIt(move)){
45.
this
.repaint();
46.
}
47.
else
{
48.
JOptionPane.showMessageDialog(
this
,
49.
"Illegal Move","ITS BlockPuzzle",
50.
51.
JOptionPane.WARNING_MESSAGE);
52.
}//
ifelse
Search WWH ::
Custom Search