Added display

main
Brett 2022-12-17 02:52:07 -05:00
parent 8975d3b535
commit ca397ad4c4
23 changed files with 152 additions and 8 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 471 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

View File

@ -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!");
}
}

View File

@ -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<Move> getMoves() {
return new ArrayList<Move>(super.getDiagonalMoves(b.size()));

View File

@ -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){

View File

@ -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<Move> getMoves();
public abstract Image getImage();
public void applySpecialMove(Move moveWithSpecial){}
protected ArrayList<Move> getCardinalMoves(int length){

View File

@ -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<Move> getMoves() {
ArrayList<Move> moves = new ArrayList<Move>();

View File

@ -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<Move> getMoves() {
ArrayList<Move> moves = new ArrayList<Move>();

View File

@ -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<Move> getMoves() {
ArrayList<Move> moves = new ArrayList<Move>();

View File

@ -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<Move> getMoves() {
ArrayList<Move> moves = new ArrayList<Move>();

View File

@ -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<Move> getMoves() {
return new ArrayList<Move>(super.getCardinalMoves(b.size()));

48
src/ui/Display.java Normal file
View File

@ -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);
}
}
}