001    /**
002     * SquarePanel is a GUI component representing a single square on a chessboard.
003     * 
004     * @specfield color : the color of the square; (in chess, either black or white)
005     * @specfield isSelected : whether this square has been selected by the user
006     * @specfield piece : the chess piece currently occupying this square
007     */
008    
009    package ui;
010    
011    import java.awt.Color;
012    import java.awt.Dimension;
013    import java.awt.Graphics;
014    import java.awt.image.BufferedImage;
015    import java.io.File;
016    import java.io.IOException;
017    import java.net.URL;
018    
019    import javax.imageio.ImageIO;
020    import javax.swing.JPanel;
021    
022    import rules.Empty;
023    import core.Piece;
024    
025    public class SquarePanel extends JPanel {
026            private static final long serialVersionUID = 54321;
027            
028            private boolean canMove;
029            private boolean canBeTaken;
030            private boolean isSelected;
031            private Piece piece;
032            
033            private static String artDirectory = "resources";
034            
035            /**
036             * Abstraction Function AF(r) = sp
037             * sp.color : determined by the constructor
038             * sp.isSelected = r.isSelected
039             * sp.piece = r.Piece
040             */
041            
042            /**
043             * @param c is the background color of this square
044             */
045            public SquarePanel(Color c) {
046                    this.setBackground(c);
047                    isSelected = false;
048                    canMove = false;
049                    canBeTaken = false;
050                    piece = new Empty();
051            }
052            
053            /**
054             * The SquarePanel's default size is 80x80 pixels--big enough to fit the
055             * provided piece images.
056             */
057            public Dimension getPreferredSize() {
058                    return new Dimension(80, 80);
059            }
060            
061            /**
062             * Sets the piece currently on this square
063             */
064            public void setPiece(Piece p) {
065                    this.piece = p;
066            }
067            
068            /**
069             * Sets this square's selection status
070             */
071            public void setSelected(boolean b) {
072                    this.isSelected = b;
073            }
074            
075            /**
076             * Sets this square's mobility status
077             */
078            public void setCanMove(boolean b) {
079                    this.canMove = b;
080            }
081            
082            /**
083             * Sets this square's capturability status
084             */
085            public void setCanBeTaken(boolean b) {
086                    this.canBeTaken = b;
087            }
088            
089            /**
090             * Gets this square's mobility status
091             */
092            public boolean getCanMove() {
093                    return canMove;
094            }
095            
096            /**
097             * Gets this square's capturability status
098             */
099            public boolean getCanBeTaken() {
100                    return canBeTaken;
101            }
102            
103            /**
104             * Draws the square
105             */
106            protected void paintComponent(Graphics g) {
107                    super.paintComponent(g);
108                    
109                    // Draw the square's background
110                    // (For the standard art set, this is a completely transparent image)
111                    String imgFile = "";
112                    BufferedImage img;
113                    try {
114                        String imgDir = SquarePanel.getArtDirectory();
115                        if (this.getBackground().equals(Color.WHITE)) {
116                            imgFile = "whitesquare.gif";
117                        }
118                        else {
119                            imgFile = "blacksquare.gif";
120                        }
121    
122                            URL url = getClass().getResource("/"+imgDir + "/" + imgFile);
123                            if (url != null){ //run from JAR
124                                img = ImageIO.read(url);
125                            } else {
126                                img = ImageIO.read(new File((imgDir + "/" + imgFile).replace("/", File.separator)));
127                            }
128                    }
129                    catch (IOException e) {
130                        System.err.println("Error printing " + imgFile);
131                        throw new RuntimeException(e);
132                    }
133                    
134                    // Center the image in the component and draw it
135                    int x = (this.getWidth() / 2) - (img.getWidth() / 2);
136                    int y = (this.getHeight() / 2) - (img.getHeight() / 2);
137                    g.drawImage(img, x, y, null);
138                    
139                    
140                    // If this square is selected, draw a border around it
141                    if (isSelected || canMove || canBeTaken) {
142                        if (isSelected) {
143                            g.setColor(Color.BLUE);
144                        } else if (canMove){
145                            g.setColor(Color.GREEN);
146                        } else { //canBeCaptured
147                            g.setColor(Color.RED);
148                        }
149                            g.fillRect(0, 0, this.getWidth(), 5);
150                            g.fillRect(0, 0, 5, this.getHeight());
151                            g.fillRect(this.getWidth() - 5, 0, 5, this.getHeight());
152                            g.fillRect(0, this.getHeight() - 5, this.getWidth(), 5);
153                            
154                    }
155                    
156                    // If there's a piece, draw it.
157                    if (! (piece instanceof Empty)) {
158                            imgFile = "";
159                            try {
160                                String imgDir = SquarePanel.getArtDirectory();
161                                imgFile = piece.toString() +
162                                        piece.getColor().toString() + ".gif";
163                                    URL url = getClass().getResource("/"+imgDir + "/" + imgFile);
164                                    if (url != null){ //run from JAR
165                                        img = ImageIO.read(url);
166                                    } else {
167                                        img = ImageIO.read(new File((imgDir + "/" + imgFile).replace("/", File.separator)));
168                                    }
169                            }
170                            catch (IOException e) {
171                                System.err.println("Error printing " + imgFile);
172                                throw new RuntimeException(e);
173                            }
174                            
175                            // Center the image in the component and draw it
176                            x = (this.getWidth() / 2) - (img.getWidth() / 2);
177                            y = (this.getHeight() / 2) - (img.getHeight() / 2);
178                            g.drawImage(img, x, y, null);
179                    }
180            }
181            
182            public static String getArtDirectory() {
183                    return artDirectory;
184            }
185            
186            public static void setArtDirectory(String dir) {
187                    artDirectory = dir;
188            }
189    }