diff --git a/Chess.class b/Chess.class index 0a41837..dfad50b 100644 Binary files a/Chess.class and b/Chess.class differ diff --git a/Chess.java b/Chess.java index 8369341..46ee31e 100644 --- a/Chess.java +++ b/Chess.java @@ -1,73 +1,70 @@ -import java.util.ArrayList; import java.awt.*; -import javax.swing.*; -import java.awt.event.*; -import javax.swing.border.*; import java.awt.Color; +import java.awt.event.*; +import java.util.ArrayList; +import javax.swing.*; +import javax.swing.border.*; +public class Chess extends JPanel implements ActionListener { -public class Chess extends JPanel implements ActionListener{ - - //pieces stuff - ArrayList white; - ArrayList black; - - //game vars - int boardWidth, boardHeight; - boolean whiteTurn; - Timer gameTimer; - Color creme = new Color(254,245,218); - Color brown = new Color(121,92,50); - - public Chess(int boardWidth, int boardHeight){ - this.boardWidth = boardWidth; - this.boardHeight = boardHeight; - setPreferredSize(new Dimension(this.boardWidth, this.boardHeight)); - setBackground(Color.WHITE); - setFocusable(true); - - white = new ArrayList<>(); - black = new ArrayList<>(); - - for (int i =0 ; i <= 7; i++){ - white.add(new Pawn(i+1,2,"White")); - } - for (int i =0 ; i <= 7; i++){ - black.add(new Pawn(i+1,7,"Black")); - } - - gameTimer = new Timer(200,this); - - gameTimer.start(); - repaint(); + // pieces stuff + ArrayList white; + ArrayList black; + + // game vars + int boardWidth, boardHeight; + boolean whiteTurn; + Timer gameTimer; + Color creme = new Color(254, 245, 218); + Color brown = new Color(121, 92, 50); + + public Chess(int boardWidth, int boardHeight) { + this.boardWidth = boardWidth; + this.boardHeight = boardHeight; + setPreferredSize(new Dimension(this.boardWidth, this.boardHeight)); + setBackground(Color.WHITE); + setFocusable(true); + + white = new ArrayList<>(); + black = new ArrayList<>(); + + for (int i = 0; i <= 7; i++) { + white.add(new Pawn(i + 1, 2, "White")); } - - public void gameLoop(){ - + for (int i = 0; i <= 7; i++) { + black.add(new Pawn(i + 1, 7, "Black")); } - - public void draw(Graphics g){ - - //draw board - for (int i = 1; i <= 8; i++){ - for (int j = 1; j<= 8; j++){ - g.setColor((i%2 == 1 && j%2 == 1 ) || (i%2==0 && j%2 == 0)? creme : brown); - g.fillRect(i * 40, j * 40, 40, 40); - } - } - - //draw pieces - for (Piece p : white) p.draw(g); - for (Piece p : black) p.draw(g); + + gameTimer = new Timer(200, this); + + gameTimer.start(); + repaint(); + } + + public void gameLoop() {} + + public void draw(Graphics g) { + + // draw board + for (int i = 1; i <= 8; i++) { + for (int j = 1; j <= 8; j++) { + g.setColor((i % 2 == 1 && j % 2 == 1) || (i % 2 == 0 && j % 2 == 0) ? creme : brown); + g.fillRect(i * 40, j * 40, 40, 40); + } } - - public void paintComponent(Graphics g){ - super.paintComponent(g); - draw(g); - } - - public void actionPerformed(ActionEvent e){ - gameLoop(); - repaint(); - } -} \ No newline at end of file + + // draw pieces + for (Piece p : white) p.draw(g); + for (Piece p : black) p.draw(g); + } + + public void paintComponent(Graphics g) { + super.paintComponent(g); + draw(g); + } + + public void actionPerformed(ActionEvent e) { + gameLoop(); + repaint(); + } +} diff --git a/Display.class b/Display.class index 7301443..2cb4db0 100644 Binary files a/Display.class and b/Display.class differ diff --git a/Display.java b/Display.java index c02317e..fc7b48e 100644 --- a/Display.java +++ b/Display.java @@ -1,23 +1,24 @@ import javax.swing.*; public class Display { - public static void main(String[] args) throws Exception{ - //creating instance of JFrame - int boardWidth = 400; - int boardHeight = 400; - - JFrame game = new JFrame(); - game.setSize(boardWidth, boardHeight); - game.setVisible(true); - game.setLocationRelativeTo(null); - game.setResizable(false); - - Chess chess = new Chess(boardWidth, boardHeight); - game.add(chess); - game.pack(); - chess.requestFocus(); - } + public static void main(String[] args) throws Exception { + // creating instance of JFrame + int boardWidth = 400; + int boardHeight = 400; + + JFrame game = new JFrame(); + game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + game.setSize(boardWidth, boardHeight); + game.setVisible(true); + game.setLocationRelativeTo(null); + game.setResizable(false); + + Chess chess = new Chess(boardWidth, boardHeight); + game.add(chess); + game.pack(); + chess.requestFocus(); + } } - -//images : https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQN6qOyhiUDLlTlwl19PaMTeiY5rSOqkUqu-g&s \ No newline at end of file +// images : +// https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQN6qOyhiUDLlTlwl19PaMTeiY5rSOqkUqu-g&s diff --git a/Pawn.class b/Pawn.class index c2d667e..4ac6511 100644 Binary files a/Pawn.class and b/Pawn.class differ diff --git a/Pawn.java b/Pawn.java index 718b7e4..31d309a 100644 --- a/Pawn.java +++ b/Pawn.java @@ -1,10 +1,9 @@ import java.awt.*; import javax.swing.ImageIcon; -public class Pawn extends Piece{ - - - public Pawn(int x, int y,String color){ - super(x,y,new ImageIcon("Sprites/" + color + "/Pawn.png").getImage()); - } -} \ No newline at end of file +public class Pawn extends Piece { + + public Pawn(int x, int y, String color) { + super(x, y, new ImageIcon("Sprites/" + color + "/pawn.png").getImage()); + } +} diff --git a/Piece.java b/Piece.java index e1fc13f..1cd1439 100644 --- a/Piece.java +++ b/Piece.java @@ -1,18 +1,18 @@ import java.awt.*; import java.util.*; -public class Piece{ - ArrayList legalMoves; - int x,y; - Image sprite; - - public Piece(int x, int y, Image sprite){ - this.x = x; - this.y = y; - this.sprite = sprite; - } - - public void draw(Graphics g){ - g.drawImage(sprite,x * 40,y * 40,null); - } -} \ No newline at end of file +public class Piece { + ArrayList legalMoves; + int x, y; + Image sprite; + + public Piece(int x, int y, Image sprite) { + this.x = x; + this.y = y; + this.sprite = sprite; + } + + public void draw(Graphics g) { + g.drawImage(sprite, x * 40, y * 40, null); + } +}