<//Import the proper packages
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class ConnectFourPanel extends JPanel
{
// Create instance variables
private JLabel[][] gameBoardArrayLabel;
private JButton newButton, exitButton;
private JLabel PlayerOneLabel, PlayerTwoLabel, drawLabel, gameBoardLabel;
private JPanel informationPanel, gameBoardPanel, buttonPanel, smallerPanel1, smallerPanel2;
private final int MAX_ROWS, MAX_COLUMNS;
private int playerOneCount, playerTwoCount, draw;
// Create the graphics variables
public BufferedImage gameBoardImage;
public BufferedImage playerOneImage;
public Image PlayerTwoImage;
public ImageIcon gameBoardIcon;
public ImageIcon playerOneIcon;
public ImageIcon playerTwoIcon;
// Create a constructor
public ConnectFourPanel()
{
// Initialize the maximum amount of rows and columns for the board
MAX_ROWS = 7;
MAX_COLUMNS = 6;
// Initialize the number of wins and draws to zero
playerOneCount = 0;
playerTwoCount = 0;
draw = 0;
// Create an object to play the game
// PlayConnectFour() play = new PlayConnectFour();
// Initialize the gameBoardArrayLabel size
gameBoardArrayLabel = new JLabel[MAX_ROWS][MAX_COLUMNS];
// Set the graphics for the game board and the tokens of the players
gameBoardIcon = new ImageIcon("Connect4Board.jpg");
playerOneIcon = new ImageIcon("BlueToken.jpg");
playerTwoIcon = new ImageIcon("PurpleToken.jpg");
// Get the Images from the icons
// gameBoardImage = gameBoardIcon.getImage();
// playerOneImage = playerOneIcon.getImage();
// PlayerTwoImage = playerTwoIcon.getImage();
// Try to read the game board image onto the screen
try
{
gameBoardImage = ImageIO.read(new File("Connect4Board.jpg"));
}
// Catch the IOException if the image does not display
catch (IOException exception)
{
System.out.println("The picture of the game board could not be read; this is an IOException.");
}
// Try to read the player one image onto the screen
try
{
playerOneImage = ImageIO.read(new File("BlueToken.jpg"));
}
// Catch the IOException if the image does not display
catch (IOException exception)
{
System.out.println("The picture of the blue token could not be read; this is an IOException.");
}
// Initialize the two dimensional array, center it, and create a border for the grid
for (int row = 0; row < MAX_ROWS; row++)
{
for (int column = 0; column < MAX_COLUMNS; column++)
{
gameBoardArrayLabel[row][column] = new JLabel("0");
gameBoardArrayLabel[row][column].setHorizontalAlignment(SwingConstants.CENTER);
gameBoardArrayLabel[row][column].setBorder(BorderFactory.createLineBorder(Color.BLACK));
}
}
// Create a layout for the Connect4Panel
// this.setLayout(new GridLayout(3, 1));
this.setLayout(new BorderLayout());
// Create a panel to talk to the players and set the layout
informationPanel = new JPanel();
informationPanel.setLayout(new GridLayout(1, 1));
// Add the informationPanel to the Connect4Panel
add(informationPanel);
// Create a label that tells the user how many time player 1 won and adds
// it to the informationPanel
PlayerOneLabel = new JLabel("Player 1 Total Wins: " + playerOneCount);
informationPanel.add(PlayerOneLabel);
// Create a label that tells the user how many time player 2 won and adds
// it to the informationPanel
PlayerTwoLabel = new JLabel("Player 2 Total Wins: " + playerTwoCount);
informationPanel.add(PlayerTwoLabel);
// Create a label that tells the user how many time player 2 won and adds
// it to the informationPanel
drawLabel = new JLabel("Total Draws: " + draw);
informationPanel.add(drawLabel);
// Set the size of the informationPanel and add it to the north
informationPanel.setPreferredSize(new Dimension(50, 50));
add (informationPanel, BorderLayout.NORTH);
// Create a panel for the game board and set the layout
gameBoardPanel = new JPanel();
gameBoardPanel.setLayout(new GridLayout(1, 1));
// Add the gameBoardPanel to the connect4Panel
//gameBoardPanel.add(gameBoardImage);
// Add the image of the gameboard to the gameBoardLabel
gameBoardLabel = new JLabel(new ImageIcon(gameBoardImage));
// Add the gameBoardLabel to the GameBoardPanel and set the size
gameBoardPanel.add(gameBoardLabel);
gameBoardPanel.setPreferredSize(new Dimension(200, 600));
// Add the gameBoardPanel to the ConnectFourPanel and add it to the south
add(gameBoardPanel);
add (gameBoardPanel, BorderLayout.SOUTH);
// Add the each row and column of the gameBoardArrayLabel to the gameBoardPanel
for (int row = 0; row < MAX_ROWS; row++)
{
for (int column = 0; column < MAX_COLUMNS; column++)
{
//gameBoardPanel.add(gameBoardArrayLabel[row][column]);
}
}
// Create a PointListener object to listen to when the players press the mouse button
// on the gameBoardPanel and adds it to the canvas
PointListener listener2 = new PointListener();
gameBoardPanel.addMouseListener(listener2);
gameBoardPanel.addMouseMotionListener(listener2);
// Add the Mouse listener to each of the rows and columns in the array
for (int row = 0; row < MAX_ROWS; row++)
{
for (int column = 0; column < MAX_COLUMNS; column++)
{
gameBoardArrayLabel[row][column].addMouseListener(listener2);
gameBoardArrayLabel[row][column].addMouseMotionListener(listener2);
}
}
// Create the button panel and set the layout
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1, 2));
// Add the buttonPanel to the Connect4Panel
add(buttonPanel);
// Create a button that says "New Game" and add it to the buttonPanel
newButton = new JButton("New Game");
// Create a smallerPanel1 and add the newButton to minimize the newButton
smallerPanel1 = new JPanel();
smallerPanel1.add(newButton);
// Add the smallerPanel1 to the buttonPanel
buttonPanel.add(smallerPanel1);
// Create a button that says "Exit Game" and add it to the buttonPanel
exitButton = new JButton("Exit Game");
buttonPanel.add(exitButton);
// Create a smallerPanel2 and add exitButton to minimize the exitButton
smallerPanel2 = new JPanel();
smallerPanel2.add(exitButton);
// Add the smallerPanel2 to the buttonPanel
buttonPanel.add(smallerPanel2);
// Add the buttonPanel to the center
add (buttonPanel, BorderLayout.CENTER);
// Create a ButtonListener object to listen to when the user
// clicks the button and adds it
ButtonListener listener = new ButtonListener();
newButton.addActionListener(listener);
// Adds the exitButton to the ButtonListener object
exitButton.addActionListener(listener);
} // End the ConnectFourPanel constructor
// This method needs to be defined to draw in this panel
private class Canvas extends JPanel
{
// Method that draws rectangles with their selected color, coordinate, and size.
public void paintComponent(Graphics page)
{
// Calls the paintComponent method from the parent class
super.paintComponent(page);
page.setColor(Color.black);
// Sets the background color to white
//setBackground(Color.white);
// Draw the lines of the grid
// page.drawLine(arg0, arg1, arg2, arg3);
int x =100;
int y = 150;
page.drawImage(gameBoardImage, x, y, null);
page.drawImage(playerOneImage, x, y, null);
gameBoardIcon.paintIcon(this, page, x, y);
playerOneIcon.paintIcon(this, page, x, y);
playerTwoIcon.paintIcon(this, page, x, y);
}
}
// Represents an action listener for the pet input field
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
}
} // Ends the ButtonListener class
// Represents a MouseListener and a MouseMotionListener that listens to the mouse when the user
// plays Connect4
private class PointListener implements MouseListener, MouseMotionListener
{
//
public void mouseClicked(MouseEvent event)
{
for (int row = 0; row < MAX_ROWS; row++)
{
for (int column = 0; column < MAX_COLUMNS; column++)
{
// event.getComponent().compareTo();
}
}
} // Ends the mouseClicked method
// Additional methods in the MouseListener that are left blank
public void mousePressed(MouseEvent event) {}
public void mouseReleased(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
public void mouseMoved(MouseEvent event) {}
public void mouseDragged(MouseEvent event) {}
} // End of PointListener
} // Ends the Connect4Panel class>