How To Make a Chess Board In Java - Java Chess Tutorial #1 - creating3dgames.com

How To Make a Chess Board In Java – Java Chess Tutorial #1

Screen Works
Views: 18682
Like: 262
in this tutorial, I will explain how to create or design a chessboard in the java programming language in Netbeans IDE.

How to Move The Pieces on the Chessboard – Java Chess Tutorial #2:

Source code (GitHub):

Another Link:

Chess pieces image:

15 Comments

  1. Very nice tutorial! I am trying to learn to code and also tried to add some additional functions: Winning condition and reset button when enter key is pressed.Reset button clears the text of winning side and moves the figures to their starting positions. Here is the whole code from the video and my additional methods:Class ChessGame :import java.awt.Color;import java.awt.Component;import java.awt.Dimension;import java.awt.Font;import java.awt.Graphics;import java.awt.Image;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.util.LinkedList;import javax.imageio.ImageIO;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;public class ChessGame { public static JLabel l; public static LinkedList<Piece> ps = new LinkedList<>();public static Piece selectedPiece = null;public static Piece getPiece(int x, int y) { // get piece position int xp = x / 64; int yp = y/ 64; for(Piece p : ps) { if(p.xp == xp && p.yp == yp) { return p; } } return null;} public static void main(String[] args) throws IOException { // TODO Auto-generated method stub //crop every piece alone and draw it according to the piece typeBufferedImage all = ImageIO.read(new File("C:\Users\User\Downloads\chess.png"));Image imgs[] = new Image[12];int ind = 0;for(int y = 0; y < 400; y+= 200) { for(int x = 0; x < 1200; x+= 200) { imgs[ind] = all.getSubimage(x,y,200,200).getScaledInstance(64,64,BufferedImage.SCALE_SMOOTH); ind++; }}//adding the piecesPiece brock = new Piece(0, 0, false, "rock", ps);Piece bknight = new Piece(1, 0, false, "knight", ps);Piece bbishop = new Piece(2, 0, false, "bishop", ps);Piece bqueen = new Piece(3, 0, false, "queen", ps);Piece bking = new Piece(4, 0, false, "king", ps);Piece bbishop2 = new Piece(5, 0, false, "bishop", ps);Piece bknight2 = new Piece(6, 0, false, "knight", ps);Piece brock2 = new Piece(7, 0, false, "rock", ps);Piece bpawn1 = new Piece(0, 1, false, "pawn", ps);Piece bpawn2 = new Piece(1, 1, false, "pawn", ps);Piece bpawn3 = new Piece(2, 1, false, "pawn", ps);Piece bpawn4 = new Piece(3, 1, false, "pawn", ps);Piece bpawn5 = new Piece(4, 1, false, "pawn", ps);Piece bpawn6 = new Piece(5, 1, false, "pawn", ps);Piece bpawn7 = new Piece(6, 1, false, "pawn", ps);Piece bpawn8 = new Piece(7, 1, false, "pawn", ps);Piece wrock = new Piece(0, 7, true, "rock", ps);Piece wknight = new Piece(1, 7, true, "knight", ps);Piece wbishop = new Piece(2, 7, true, "bishop", ps);Piece wqueen = new Piece(3, 7, true, "queen", ps);Piece wking = new Piece(4, 7, true, "king", ps);Piece wbishop2 = new Piece(5, 7, true, "bishop", ps);Piece wknight2 = new Piece(6, 7, true, "knight", ps);Piece wrock2 = new Piece(7, 7, true, "rock", ps);Piece wpawn1 = new Piece(0, 6, true, "pawn", ps);Piece wpawn2 = new Piece(1, 6, true, "pawn", ps);Piece wpawn3 = new Piece(2, 6, true, "pawn", ps);Piece wpawn4 = new Piece(3, 6, true, "pawn", ps);Piece wpawn5 = new Piece(4, 6, true, "pawn", ps);Piece wpawn6 = new Piece(5, 6, true, "pawn", ps);Piece wpawn7 = new Piece(6, 6, true, "pawn", ps);Piece wpawn8 = new Piece(7, 6, true, "pawn", ps);JFrame frame = new JFrame();frame.setBounds(10,10,512,512);frame.setUndecorated(true); // remove the bordersframe.setLocationRelativeTo(null);l = new JLabel();l.setBounds(115,215, 400, 270);frame.add(l);JPanel pn = new JPanel() { ChessGame cg = new ChessGame(); public void paint(Graphics g) { super.paintComponent(g); boolean white = true; for(int y = 0; y < 8; y++) { //vertical rows for(int x = 0; x < 8; x++) { //horizontal rows if(white) { g.setColor(Color.white.darker()); } else { g.setColor(new Color(153,102,0)); } g.fillRect(x*64,y*64,64,64); white = !white; } white = !white;//check value of white.If its true its gonna make it false and vice versa } for(Piece p : ps) { int ind = 0; //represent the index of the piece in the images array //now we need to check the name of the piece and give index number according the name if(p.name.equalsIgnoreCase("king")) { ind = 0; } if(p.name.equalsIgnoreCase("queen")) { ind = 1; } if(p.name.equalsIgnoreCase("bishop")) { ind = 2; } if(p.name.equalsIgnoreCase("knight")) { ind = 3; } if(p.name.equalsIgnoreCase("rock")) { ind = 4; } if(p.name.equalsIgnoreCase("pawn")) { ind = 5; } if(!p.isWhite) { ind+= 6; } //the function to draw the piece g.drawImage(imgs[ind],p.x,p.y,this); } } }; frame.add(pn);frame.addMouseListener(new MouseListener() { //winning condition public void getInfo() { if(!ps.contains(bking)) { l.setText("white wins"); l.setFont(new Font("verdana",Font.BOLD,40)); l.setForeground(Color.yellow.brighter()); } else if(!ps.contains(wking)) { l.setFont(new Font("verdana",Font.BOLD,40)); l.setForeground(Color.yellow.brighter()); l.setText("black wins"); } } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stubSystem.out.println((getPiece(e.getX(),e.getY()).isWhite ? "white ": "black ") + getPiece(e.getX(),e.getY()).name); //get the name of clicked pieceselectedPiece = getPiece(e.getX(),e.getY()); } @Override public void mouseReleased(MouseEvent e) { //when we release the mouse we moved the piece // TODO Auto-generated method stub selectedPiece.move(e.getX() / 64, e.getY() / 64); getInfo(); frame.repaint(); } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub }});frame.addMouseMotionListener(new MouseMotionListener() { @Override public void mouseDragged(MouseEvent e) { // TODO Auto-generated method stub if(selectedPiece != null) { selectedPiece.x = e.getX() – 32; selectedPiece.y = e.getY() – 32; frame.repaint(); } } @Override public void mouseMoved(MouseEvent e) { // TODO Auto-generated method stub } });frame.addKeyListener(new KeyListener() { @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub if(e.getKeyCode() == KeyEvent.VK_ENTER) { ps.clear(); // remove all stored elements from linkedList<Piece> l.setText(""); // clear the text of winning condition ps.add(bking); //adding the same bking with last known position bking.set(4, 0); //set bking to start position ps.add(wking); ////adding the same wking with last known position wking.set(4, 7); //set wking to start position //adding the other pieces to their start positions new Piece(0, 0, false, "rock", ps); new Piece(1, 0, false, "knight", ps); new Piece(2, 0, false, "bishop", ps); new Piece(3, 0, false, "queen", ps); new Piece(5, 0, false, "bishop", ps); new Piece(6, 0, false, "knight", ps); new Piece(7, 0, false, "rock", ps); new Piece(0, 1, false, "pawn", ps); new Piece(1, 1, false, "pawn", ps); new Piece(2, 1, false, "pawn", ps); new Piece(3, 1, false, "pawn", ps); new Piece(4, 1, false, "pawn", ps); new Piece(5, 1, false, "pawn", ps); new Piece(6, 1, false, "pawn", ps); new Piece(7, 1, false, "pawn", ps); new Piece(0, 7, true, "rock", ps); new Piece(1, 7, true, "knight", ps); new Piece(2, 7, true, "bishop", ps); new Piece(3, 7, true, "queen", ps); new Piece(5,7,true,"bishop",ps); new Piece(6,7,true,"knight",ps); new Piece(7,7,true,"rock",ps); new Piece(0,6,true,"pawn",ps); new Piece(1,6,true,"pawn",ps); new Piece(2,6,true,"pawn",ps); new Piece(3,6,true,"pawn",ps); new Piece(4,6,true,"pawn",ps); new Piece(5,6,true,"pawn",ps); new Piece(6,6,true,"pawn",ps); new Piece(7,6,true,"pawn",ps); frame.repaint(); }} @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } });frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.repaint();frame.setVisible(true); } }class Piece:import java.util.*;import javax.swing.JLabel;public class Piece {int xp; //position for piecesint yp; // position for piecesboolean isWhite; // check the piece if its white or blackLinkedList<Piece> ps;String name; //the name of each pieceint x; int y; ChessGame cg = new ChessGame();public Piece(int xp,int yp,boolean isWhite,String name,LinkedList<Piece> ps) { this.xp = xp; this.yp = yp; x = xp * 64; y = yp * 64; this.isWhite = isWhite; this.ps = ps; this.name = name; ps.add(this); }public void move(int xp,int yp) { // function to move the piece if(ChessGame.getPiece(xp*64, yp*64) != null) { if(ChessGame.getPiece(xp*64, yp*64).isWhite != isWhite) {ChessGame.getPiece(xp*64, yp*64).kill(); } else { x = this.xp * 64; y = this.yp * 64; return; } } //move the piece to the position changing xp and yp this.xp = xp; this.yp = yp; x = xp * 64; y = yp * 64;}public void kill() { //remove the piece from the list ps.remove(this); }// used to adjust starting position of wking and bkingpublic void set(int xp,int yp) { // TODO Auto-generated method stub this.xp = xp; this.yp = yp; x = xp * 64; y = yp * 64;}}

  2. I was looking for a tutorial like this! Very insightful tutorial man keep it up!

  3. I'm looking for this tutorial and thank God brought me here. We're hoping to see your next video. God bless bro.

  4. Thank you very much, a great tutorial, I looking forward for the next one.

  5. I love this video thank you!! However I did try to run it when you did and got the error

    "Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The field Component.x is not visible
    Syntax error, insert ";" to complete LocalVariableDeclarationStatement
    at ChessGame.main(ChessGame.java:24)"

    that was in reference to the code line
    g.fillRect(x*64, y*64, 64,64);

    how can I fix this?

  6. Is there a reason I'm not aware of why none of the nested for-loops are indented?

Leave a Reply

Your email address will not be published.