import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class UserInterface extends JPanel implements MouseListener, MouseMotionListener {
static int x_pressed,y_pressed; // Mouse pressed
static int x_released, y_released; // Mouse released
static int Square_size = 60; // size of the playing square on the board.
static int Tile_offset = 67; // Offset coordinates of tile on board
static int Tile_Spacing = 62;
boolean pressed;
boolean released;
boolean piece_chosen;
// Declaring the Chess pieces
// *********************************************
// White
Image King_white = new ImageIcon("King_White.png").getImage();
Image Queen_white = new ImageIcon("Queen_White.png").getImage();
Image Knight_white = new ImageIcon("Knight_White.png").getImage();
Image Bishop_white = new ImageIcon("Bishop_White.png").getImage();
Image Rook_white = new ImageIcon("Rook_White.png").getImage();
Image Pawn_white = new ImageIcon("Pawn_White.png").getImage();
// Black
Image King_black = new ImageIcon("King_Black.png").getImage();
Image Queen_black = new ImageIcon("Queen_Black.png").getImage();
Image Knight_black = new ImageIcon("Knight_Black.png").getImage();
Image Bishop_black = new ImageIcon("Bishop_Black.png").getImage();
Image Rook_black = new ImageIcon("Rook_Black.png").getImage();
Image Pawn_black = new ImageIcon("Pawn_Black.png").getImage();
// *********************************************
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g); // Dunno what this is lol
this.setBackground(Color.BLACK); // Window Background
this.addMouseListener(this); // Mouselistener
this.addMouseMotionListener(this); // Motionlistener
// Following for() cycles generate a checkered board.
// *******************************************
for(int vertical = 1; vertical < 9; vertical++)
{
for(int horizontal = 1; horizontal < 9; horizontal++)
{
if(!((horizontal + vertical)% 2 == 0))
{
g.setColor(new Color(150,50,30)); //Darker squares
g.fillRect(62*horizontal, 62*vertical , 60, 60);
}
else
{
g.setColor(new Color(250,200,100)); // lighter squares
g.fillRect(62*horizontal, 62*vertical , 60, 60);
}
}
}
// ********************************************
// Updates the current position of the pieces on the board
for(int vertical = 0; vertical < 8; vertical ++ )
{
for(int horizontal = 0 ; horizontal < 8; horizontal ++ )
{
switch(Chess_engine.ChessBoard[vertical][horizontal]) // Paints a picture according to the value of the String on each square
{
case "a" : g.drawImage(King_black, Tile_offset + Tile_Spacing*horizontal, Tile_offset + Tile_Spacing*vertical, 50, 50, this); break;
case "q" : g.drawImage(Queen_black, Tile_offset + Tile_Spacing*horizontal,Tile_offset + Tile_Spacing*vertical, 50, 50, this); break;
case "b" : g.drawImage(Bishop_black, Tile_offset + Tile_Spacing*horizontal ,Tile_offset + Tile_Spacing*vertical, 50, 50, this); break;
case "k" : g.drawImage(Knight_black, Tile_offset + Tile_Spacing*horizontal, Tile_offset + Tile_Spacing*vertical, 50, 50, this); break;
case "r" : g.drawImage(Rook_black, Tile_offset + Tile_Spacing*horizontal, Tile_offset + Tile_Spacing*vertical, 50, 50, this); break;
case "p" : g.drawImage(Pawn_black, Tile_offset + Tile_Spacing*horizontal, Tile_offset + Tile_Spacing*vertical, 50, 50, this); break;
case "A" : g.drawImage(King_white, Tile_offset + Tile_Spacing*horizontal, Tile_offset + Tile_Spacing*vertical, 50, 50, this); break;
case "Q" : g.drawImage(Queen_white, Tile_offset + Tile_Spacing*horizontal,Tile_offset + Tile_Spacing*vertical, 50, 50, this); break;
case "B" : g.drawImage(Bishop_white, Tile_offset + Tile_Spacing*horizontal,Tile_offset + Tile_Spacing*vertical, 50, 50, this); break;
case "K" : g.drawImage(Knight_white, Tile_offset + Tile_Spacing*horizontal,Tile_offset + Tile_Spacing*vertical, 50, 50, this); break;
case "R" : g.drawImage(Rook_white, Tile_offset + Tile_Spacing*horizontal, Tile_offset + Tile_Spacing*vertical, 50, 50, this); break;
case "P" : g.drawImage(Pawn_white, Tile_offset + Tile_Spacing*horizontal, Tile_offset + Tile_Spacing*vertical, 50, 50, this); break;
case " " : break; // Square is empty
} // End of switch statement
} // End of For loop 1
} // End of for loop 2
// End of updating pieces
// ************************************
}
@Override
public void mouseMoved(MouseEvent e){
}
@Override
public void mousePressed(MouseEvent e){
System.out.println("Pressed");
/*
x_pressed = e.getX();
y_pressed = e.getY();
if(x_pressed > 62 && x_pressed < 558 && y_pressed > 62 && y_pressed < 558) // If the press is within the board
{
Chess_engine.Square_pressed(x_pressed, y_pressed); // Buffer the piece
}
*/
}
@Override
public void mouseReleased(MouseEvent e){
System.out.println("Released");
/*
x_released = e.getX();
y_released = e.getY();
if(y_released > 62 && y_released < 558 && y_released> 62 && y_released < 558)
{
Chess_engine.Square_pressed(y_released, y_released);
repaint();
}
*/
}
@Override
public void mouseClicked(MouseEvent e){
}
@Override
public void mouseDragged(MouseEvent e){
}
@Override
public void mouseEntered(MouseEvent e){}
@Override
public void mouseExited(MouseEvent e){}
}