-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathField.java
More file actions
135 lines (111 loc) · 3.97 KB
/
Field.java
File metadata and controls
135 lines (111 loc) · 3.97 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.*;
public class Field
{
private final JPanel gui = new JPanel( new BorderLayout( 3, 3 ) );
private JButton[][] chessBoardSquares = new JButton[8][8];
private JPanel chessBoard;
private final JLabel message = new JLabel( "REKKER is ready to play!" );
private static final String COLS = "ABCDEFGH";
Field()
{
initializeGui();
}
public final void initializeGui()
{
// set up the main GUI
gui.setBorder( new EmptyBorder( 5, 5, 5, 5 ) );
JToolBar tools = new JToolBar();
tools.setFloatable( false );
gui.add( tools, BorderLayout.PAGE_START );
tools.add( new JButton( "New Game" ) ); // TODO - add functionality!
tools.add( new JButton( "Move" ) ); // TODO - add functionality!
tools.add( new JButton( "Attack" ) ); // TODO - add functionality!
tools.addSeparator();
tools.add( new JButton( "End Turn" ) ); // TODO - add functionality!
tools.addSeparator();
tools.add( message );
gui.add( new JLabel( "?" ), BorderLayout.LINE_START );
chessBoard = new JPanel( new GridLayout( 0, 9 ) );
chessBoard.setBorder( new LineBorder( Color.BLACK ) );
gui.add( chessBoard );
// create the chess board squares
Insets buttonMargin = new Insets( 0, 0, 0, 0 );
for ( int ii = 0; ii < chessBoardSquares.length; ii++ )
{
for ( int jj = 0; jj < chessBoardSquares[ii].length; jj++ )
{
JButton b = new JButton();
b.setMargin( buttonMargin );
// our chess pieces are 64x64 px in size, so we'll
// 'fill this in' using a transparent icon..
ImageIcon icon = new ImageIcon( new BufferedImage( 64, 64, BufferedImage.TYPE_INT_ARGB ) );
b.setIcon( icon );
if ( ( jj % 2 == 1 && ii % 2 == 1 )
// ) {
|| ( jj % 2 == 0 && ii % 2 == 0 ) )
{
b.setBackground( Color.WHITE );
}
else
{
b.setBackground( Color.WHITE );
}
chessBoardSquares[jj][ii] = b;
}
}
// fill the chess board
chessBoard.add( new JLabel( "" ) );
// fill the top row
for ( int ii = 0; ii < 8; ii++ )
{
chessBoard.add( new JLabel( COLS.substring( ii, ii + 1 ), SwingConstants.CENTER ) );
}
// fill the black non-pawn piece row
for ( int ii = 0; ii < 8; ii++ )
{
for ( int jj = 0; jj < 8; jj++ )
{
switch ( jj )
{
case 0:
chessBoard.add( new JLabel( "" + ( ii + 1 ), SwingConstants.CENTER ) );
default:
chessBoard.add( chessBoardSquares[jj][ii] );
}
}
}
}
public final JComponent getChessBoard()
{
return chessBoard;
}
public final JComponent getGui()
{
return gui;
}
public static void main( String[] args )
{
Runnable r = new Runnable()
{
@Override
public void run()
{
Field cb = new Field();
JFrame f = new JFrame( "ChessChamp" );
f.add( cb.getGui() );
f.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
f.setLocationByPlatform( true );
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// ensures the minimum size is enforced.
f.setMinimumSize( f.getSize() );
f.setVisible( true );
}
};
SwingUtilities.invokeLater( r );
}
}