Added display
After Width: | Height: | Size: 471 KiB |
After Width: | Height: | Size: 3.4 KiB |
After Width: | Height: | Size: 3.7 KiB |
After Width: | Height: | Size: 3.4 KiB |
After Width: | Height: | Size: 3.1 KiB |
After Width: | Height: | Size: 3.6 KiB |
After Width: | Height: | Size: 3.0 KiB |
After Width: | Height: | Size: 3.6 KiB |
After Width: | Height: | Size: 4.0 KiB |
After Width: | Height: | Size: 3.6 KiB |
After Width: | Height: | Size: 3.3 KiB |
After Width: | Height: | Size: 3.7 KiB |
After Width: | Height: | Size: 3.1 KiB |
|
@ -1,5 +1,29 @@
|
||||||
|
import chess.Board;
|
||||||
|
import ui.Display;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
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!");
|
System.out.println("Hello world!");
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,13 +1,25 @@
|
||||||
package chess;
|
package chess;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import static ui.Display.loadImage;
|
||||||
|
|
||||||
public class Bishop extends ChessPiece {
|
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) {
|
public Bishop(Board b, boolean isWhite, int x, int y) {
|
||||||
super(b,isWhite,x,y);
|
super(b,isWhite,x,y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Image getImage(){
|
||||||
|
if (isWhite)
|
||||||
|
return whiteBishop;
|
||||||
|
return blackBishop;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ArrayList<Move> getMoves() {
|
public ArrayList<Move> getMoves() {
|
||||||
return new ArrayList<Move>(super.getDiagonalMoves(b.size()));
|
return new ArrayList<Move>(super.getDiagonalMoves(b.size()));
|
||||||
|
|
|
@ -23,14 +23,14 @@ public class Board {
|
||||||
board[size()-4][0] = new King(this, true, size() - 4, 0);
|
board[size()-4][0] = new King(this, true, size() - 4, 0);
|
||||||
|
|
||||||
// black
|
// black
|
||||||
board[0][0] = new Rook(this, false,0, 0);
|
board[0][size()-1] = new Rook(this, false,0, 0);
|
||||||
board[size()-1][0] = new Rook(this, false, size() - 1, 0);
|
board[size()-1][size()-1] = new Rook(this, false, size() - 1, 0);
|
||||||
board[1][0] = new Knight(this, false, 1, 0);
|
board[1][size()-1] = new Knight(this, false, 1, 0);
|
||||||
board[size()-2][0] = new Knight(this, false, size() - 2, 0);
|
board[size()-2][size()-1] = new Knight(this, false, size() - 2, 0);
|
||||||
board[2][0] = new Bishop(this, false, 2, 0);
|
board[2][size()-1] = new Bishop(this, false, 2, 0);
|
||||||
board[size()-3][0] = new Bishop(this, false, size() - 3, 0);
|
board[size()-3][size()-1] = new Bishop(this, false, size() - 3, 0);
|
||||||
board[3][0] = new Queen(this, false, 3, 0);
|
board[3][size()-1] = new Queen(this, false, 3, 0);
|
||||||
board[size()-4][0] = new King(this, false, size() - 4, 0);
|
board[size()-4][size()-1] = new King(this, false, size() - 4, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean movePiece(int x, int y, int newX, int newY){
|
public boolean movePiece(int x, int y, int newX, int newY){
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package chess;
|
package chess;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public abstract class ChessPiece {
|
public abstract class ChessPiece {
|
||||||
|
@ -33,6 +34,7 @@ public abstract class ChessPiece {
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract ArrayList<Move> getMoves();
|
public abstract ArrayList<Move> getMoves();
|
||||||
|
public abstract Image getImage();
|
||||||
public void applySpecialMove(Move moveWithSpecial){}
|
public void applySpecialMove(Move moveWithSpecial){}
|
||||||
|
|
||||||
protected ArrayList<Move> getCardinalMoves(int length){
|
protected ArrayList<Move> getCardinalMoves(int length){
|
||||||
|
|
|
@ -1,13 +1,25 @@
|
||||||
package chess;
|
package chess;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import static ui.Display.loadImage;
|
||||||
|
|
||||||
public class King extends ChessPiece {
|
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) {
|
public King(Board b, boolean isWhite, int x, int y) {
|
||||||
super(b,isWhite,x,y);
|
super(b,isWhite,x,y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Image getImage(){
|
||||||
|
if (isWhite)
|
||||||
|
return whiteKing;
|
||||||
|
return blackKing;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ArrayList<Move> getMoves() {
|
public ArrayList<Move> getMoves() {
|
||||||
ArrayList<Move> moves = new ArrayList<Move>();
|
ArrayList<Move> moves = new ArrayList<Move>();
|
||||||
|
|
|
@ -1,13 +1,25 @@
|
||||||
package chess;
|
package chess;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import static ui.Display.loadImage;
|
||||||
|
|
||||||
public class Knight extends ChessPiece {
|
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) {
|
public Knight(Board b, boolean isWhite, int x, int y) {
|
||||||
super(b,isWhite,x,y);
|
super(b,isWhite,x,y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Image getImage(){
|
||||||
|
if (isWhite)
|
||||||
|
return whiteKnight;
|
||||||
|
return blackKnight;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ArrayList<Move> getMoves() {
|
public ArrayList<Move> getMoves() {
|
||||||
ArrayList<Move> moves = new ArrayList<Move>();
|
ArrayList<Move> moves = new ArrayList<Move>();
|
||||||
|
|
|
@ -1,13 +1,25 @@
|
||||||
package chess;
|
package chess;
|
||||||
|
|
||||||
|
import ui.Display;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class Pawn extends ChessPiece {
|
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) {
|
public Pawn(Board b, boolean isWhite, int x, int y) {
|
||||||
super(b,isWhite,x,y);
|
super(b,isWhite,x,y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Image getImage(){
|
||||||
|
if (isWhite)
|
||||||
|
return white;
|
||||||
|
return black;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ArrayList<Move> getMoves() {
|
public ArrayList<Move> getMoves() {
|
||||||
ArrayList<Move> moves = new ArrayList<Move>();
|
ArrayList<Move> moves = new ArrayList<Move>();
|
||||||
|
|
|
@ -1,13 +1,25 @@
|
||||||
package chess;
|
package chess;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import static ui.Display.loadImage;
|
||||||
|
|
||||||
public class Queen extends ChessPiece {
|
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) {
|
public Queen(Board b, boolean isWhite, int x, int y) {
|
||||||
super(b,isWhite,x,y);
|
super(b,isWhite,x,y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Image getImage(){
|
||||||
|
if (isWhite)
|
||||||
|
return whiteQueen;
|
||||||
|
return blackQueen;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ArrayList<Move> getMoves() {
|
public ArrayList<Move> getMoves() {
|
||||||
ArrayList<Move> moves = new ArrayList<Move>();
|
ArrayList<Move> moves = new ArrayList<Move>();
|
||||||
|
|
|
@ -1,13 +1,23 @@
|
||||||
package chess;
|
package chess;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import static ui.Display.loadImage;
|
||||||
|
|
||||||
public class Rook extends ChessPiece {
|
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) {
|
public Rook(Board b, boolean isWhite, int x, int y) {
|
||||||
super(b,isWhite,x,y);
|
super(b,isWhite,x,y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Image getImage(){
|
||||||
|
return isWhite ? whiteRook : blackRook;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ArrayList<Move> getMoves() {
|
public ArrayList<Move> getMoves() {
|
||||||
return new ArrayList<Move>(super.getCardinalMoves(b.size()));
|
return new ArrayList<Move>(super.getCardinalMoves(b.size()));
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|