diff --git a/resources/Marble and Marble 512x552.png b/resources/Marble and Marble 512x552.png new file mode 100644 index 0000000..0580727 Binary files /dev/null and b/resources/Marble and Marble 512x552.png differ diff --git a/resources/chess_piece_2_black_bishop.png b/resources/chess_piece_2_black_bishop.png new file mode 100644 index 0000000..083186f Binary files /dev/null and b/resources/chess_piece_2_black_bishop.png differ diff --git a/resources/chess_piece_2_black_king.png b/resources/chess_piece_2_black_king.png new file mode 100644 index 0000000..284764e Binary files /dev/null and b/resources/chess_piece_2_black_king.png differ diff --git a/resources/chess_piece_2_black_knight.png b/resources/chess_piece_2_black_knight.png new file mode 100644 index 0000000..a864627 Binary files /dev/null and b/resources/chess_piece_2_black_knight.png differ diff --git a/resources/chess_piece_2_black_pawn.png b/resources/chess_piece_2_black_pawn.png new file mode 100644 index 0000000..8105062 Binary files /dev/null and b/resources/chess_piece_2_black_pawn.png differ diff --git a/resources/chess_piece_2_black_queen.png b/resources/chess_piece_2_black_queen.png new file mode 100644 index 0000000..a9c7760 Binary files /dev/null and b/resources/chess_piece_2_black_queen.png differ diff --git a/resources/chess_piece_2_black_rook.png b/resources/chess_piece_2_black_rook.png new file mode 100644 index 0000000..2a86b24 Binary files /dev/null and b/resources/chess_piece_2_black_rook.png differ diff --git a/resources/chess_piece_2_white_bishop.png b/resources/chess_piece_2_white_bishop.png new file mode 100644 index 0000000..9955990 Binary files /dev/null and b/resources/chess_piece_2_white_bishop.png differ diff --git a/resources/chess_piece_2_white_king.png b/resources/chess_piece_2_white_king.png new file mode 100644 index 0000000..e492d15 Binary files /dev/null and b/resources/chess_piece_2_white_king.png differ diff --git a/resources/chess_piece_2_white_knight.png b/resources/chess_piece_2_white_knight.png new file mode 100644 index 0000000..f49db2b Binary files /dev/null and b/resources/chess_piece_2_white_knight.png differ diff --git a/resources/chess_piece_2_white_pawn.png b/resources/chess_piece_2_white_pawn.png new file mode 100644 index 0000000..98b56c5 Binary files /dev/null and b/resources/chess_piece_2_white_pawn.png differ diff --git a/resources/chess_piece_2_white_queen.png b/resources/chess_piece_2_white_queen.png new file mode 100644 index 0000000..6a9106b Binary files /dev/null and b/resources/chess_piece_2_white_queen.png differ diff --git a/resources/chess_piece_2_white_rook.png b/resources/chess_piece_2_white_rook.png new file mode 100644 index 0000000..97eb2c2 Binary files /dev/null and b/resources/chess_piece_2_white_rook.png differ diff --git a/src/Main.java b/src/Main.java index 3e59c38..4228422 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,29 @@ +import chess.Board; +import ui.Display; + public class Main { public static void main(String[] args) { + Board mainBoard = new Board(); + Display display = new Display(mainBoard); + long frames = 0; + long lastTime = System.currentTimeMillis(); + long frameTime = System.currentTimeMillis(); + while(display.update()){ + display.repaint(); + // limit usage of system resources by slowing the speed down to 60 fps. + while ((System.currentTimeMillis() - frameTime) < 64f){ + Thread.yield(); + } + frameTime = System.currentTimeMillis(); + + // print out the FPS of the display. + frames++; + if (System.currentTimeMillis() - lastTime > 1000){ + System.out.println("FPS: " + frames); + frames = 0; + lastTime = System.currentTimeMillis(); + } + } System.out.println("Hello world!"); } } \ No newline at end of file diff --git a/src/chess/Bishop.java b/src/chess/Bishop.java index 838eea1..a17e888 100644 --- a/src/chess/Bishop.java +++ b/src/chess/Bishop.java @@ -1,13 +1,25 @@ package chess; +import java.awt.*; import java.util.ArrayList; +import static ui.Display.loadImage; + public class Bishop extends ChessPiece { + private Image whiteBishop = loadImage("./resources/chess_piece_2_black_bishop.png"); + private Image blackBishop = loadImage("./resources/chess_piece_2_white_bishop.png"); + public Bishop(Board b, boolean isWhite, int x, int y) { super(b,isWhite,x,y); } + public Image getImage(){ + if (isWhite) + return whiteBishop; + return blackBishop; + } + @Override public ArrayList getMoves() { return new ArrayList(super.getDiagonalMoves(b.size())); diff --git a/src/chess/Board.java b/src/chess/Board.java index f30821a..09fa632 100644 --- a/src/chess/Board.java +++ b/src/chess/Board.java @@ -23,14 +23,14 @@ public class Board { board[size()-4][0] = new King(this, true, size() - 4, 0); // black - board[0][0] = new Rook(this, false,0, 0); - board[size()-1][0] = new Rook(this, false, size() - 1, 0); - board[1][0] = new Knight(this, false, 1, 0); - board[size()-2][0] = new Knight(this, false, size() - 2, 0); - board[2][0] = new Bishop(this, false, 2, 0); - board[size()-3][0] = new Bishop(this, false, size() - 3, 0); - board[3][0] = new Queen(this, false, 3, 0); - board[size()-4][0] = new King(this, false, size() - 4, 0); + board[0][size()-1] = new Rook(this, false,0, 0); + board[size()-1][size()-1] = new Rook(this, false, size() - 1, 0); + board[1][size()-1] = new Knight(this, false, 1, 0); + board[size()-2][size()-1] = new Knight(this, false, size() - 2, 0); + board[2][size()-1] = new Bishop(this, false, 2, 0); + board[size()-3][size()-1] = new Bishop(this, false, size() - 3, 0); + board[3][size()-1] = new Queen(this, false, 3, 0); + board[size()-4][size()-1] = new King(this, false, size() - 4, 0); } public boolean movePiece(int x, int y, int newX, int newY){ diff --git a/src/chess/ChessPiece.java b/src/chess/ChessPiece.java index c2728fc..79b394f 100644 --- a/src/chess/ChessPiece.java +++ b/src/chess/ChessPiece.java @@ -1,5 +1,6 @@ package chess; +import java.awt.*; import java.util.ArrayList; public abstract class ChessPiece { @@ -33,6 +34,7 @@ public abstract class ChessPiece { } public abstract ArrayList getMoves(); + public abstract Image getImage(); public void applySpecialMove(Move moveWithSpecial){} protected ArrayList getCardinalMoves(int length){ diff --git a/src/chess/King.java b/src/chess/King.java index 1e5fc23..eaf8798 100644 --- a/src/chess/King.java +++ b/src/chess/King.java @@ -1,13 +1,25 @@ package chess; +import java.awt.*; import java.util.ArrayList; +import static ui.Display.loadImage; + public class King extends ChessPiece { + private Image whiteKing = loadImage("./resources/chess_piece_2_black_king.png"); + private Image blackKing = loadImage("./resources/chess_piece_2_white_king.png"); + public King(Board b, boolean isWhite, int x, int y) { super(b,isWhite,x,y); } + public Image getImage(){ + if (isWhite) + return whiteKing; + return blackKing; + } + @Override public ArrayList getMoves() { ArrayList moves = new ArrayList(); diff --git a/src/chess/Knight.java b/src/chess/Knight.java index 4e4e673..faa0ebd 100644 --- a/src/chess/Knight.java +++ b/src/chess/Knight.java @@ -1,13 +1,25 @@ package chess; +import java.awt.*; import java.util.ArrayList; +import static ui.Display.loadImage; + public class Knight extends ChessPiece { + private Image whiteKnight = loadImage("./resources/chess_piece_2_black_knight.png"); + private Image blackKnight = loadImage("./resources/chess_piece_2_white_knight.png"); + public Knight(Board b, boolean isWhite, int x, int y) { super(b,isWhite,x,y); } + public Image getImage(){ + if (isWhite) + return whiteKnight; + return blackKnight; + } + @Override public ArrayList getMoves() { ArrayList moves = new ArrayList(); diff --git a/src/chess/Pawn.java b/src/chess/Pawn.java index 42a0f76..083ea64 100644 --- a/src/chess/Pawn.java +++ b/src/chess/Pawn.java @@ -1,13 +1,25 @@ package chess; +import ui.Display; + +import java.awt.*; import java.util.ArrayList; public class Pawn extends ChessPiece { + private Image white = Display.loadImage("./resources/chess_piece_2_black_pawn.png"); + private Image black = Display.loadImage("./resources/chess_piece_2_white_pawn.png"); + public Pawn(Board b, boolean isWhite, int x, int y) { super(b,isWhite,x,y); } + public Image getImage(){ + if (isWhite) + return white; + return black; + } + @Override public ArrayList getMoves() { ArrayList moves = new ArrayList(); diff --git a/src/chess/Queen.java b/src/chess/Queen.java index 9165322..2b7755b 100644 --- a/src/chess/Queen.java +++ b/src/chess/Queen.java @@ -1,13 +1,25 @@ package chess; +import java.awt.*; import java.util.ArrayList; +import static ui.Display.loadImage; + public class Queen extends ChessPiece { + private Image whiteQueen = loadImage("./resources/chess_piece_2_black_queen.png"); + private Image blackQueen = loadImage("./resources/chess_piece_2_white_queen.png"); + public Queen(Board b, boolean isWhite, int x, int y) { super(b,isWhite,x,y); } + public Image getImage(){ + if (isWhite) + return whiteQueen; + return blackQueen; + } + @Override public ArrayList getMoves() { ArrayList moves = new ArrayList(); diff --git a/src/chess/Rook.java b/src/chess/Rook.java index 784c2ac..4664fda 100644 --- a/src/chess/Rook.java +++ b/src/chess/Rook.java @@ -1,13 +1,23 @@ package chess; +import java.awt.*; import java.util.ArrayList; +import static ui.Display.loadImage; + public class Rook extends ChessPiece { + private Image whiteRook = loadImage("./resources/chess_piece_2_black_rook.png"); + private Image blackRook = loadImage("./resources/chess_piece_2_white_rook.png"); + public Rook(Board b, boolean isWhite, int x, int y) { super(b,isWhite,x,y); } + public Image getImage(){ + return isWhite ? whiteRook : blackRook; + } + @Override public ArrayList getMoves() { return new ArrayList(super.getCardinalMoves(b.size())); diff --git a/src/ui/Display.java b/src/ui/Display.java new file mode 100644 index 0000000..21849d7 --- /dev/null +++ b/src/ui/Display.java @@ -0,0 +1,48 @@ +package ui; + +import chess.Board; + +import javax.imageio.ImageIO; +import javax.swing.*; +import javax.swing.text.AttributeSet; +import java.awt.*; +import java.io.File; +import java.io.IOException; +import java.text.AttributedString; + +public class Display extends JFrame { + + private Board b; + + public Display(Board b){ + this.b = b; + this.setDefaultCloseOperation(EXIT_ON_CLOSE); + this.setSize(800, 800); + this.setVisible(true); + this.setEnabled(true); + this.setTitle("Ratatta"); + } + + @Override + public void paint(Graphics g) { + super.paint(g); + for (int i = 0; i < b.size(); i++){ + for (int j = 0; j < b.size(); j++){ + if (b.get(i,j) != null) + g.drawImage(b.get(i,j).getImage(), (i+2) * 64, (j+2) * 64, null); + } + } + } + + public boolean update(){ + return true; + } + + public static Image loadImage(String path){ + try { + return new ImageIcon(ImageIO.read(new File(path))).getImage(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +}