kuchuz

PBO-C EAS : Board

Jan 11th, 2021
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.18 KB | None | 0 0
  1. package chessgui;
  2.  
  3. import chessgui.pieces.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import java.awt.geom.*;
  7. import java.awt.image.*;
  8. import java.io.*;
  9. import java.util.*;
  10. import javax.imageio.*;
  11. import javax.swing.*;
  12.  
  13.  
  14. @SuppressWarnings("serial")
  15. public class Board extends JComponent {
  16.        
  17.     public int turnCounter = 0;
  18.     private static Image NULL_IMAGE = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB);
  19.  
  20.     private final int Square_Width = 65;
  21.     public ArrayList<Piece> White_Pieces;
  22.     public ArrayList<Piece> Black_Pieces;
  23.    
  24.  
  25.     public ArrayList<DrawingShape> Static_Shapes;
  26.     public ArrayList<DrawingShape> Piece_Graphics;
  27.  
  28.     public Piece Active_Piece;
  29.  
  30.     private final int rows = 8;
  31.     private final int cols = 8;
  32.     private Integer[][] BoardGrid;
  33.     private String board_file_path = "images" + File.separator + "board.png";
  34.     private String active_square_file_path = "images" + File.separator + "active_square.png";
  35.  
  36.     public void initGrid(){
  37.         for (int i = 0; i < rows; i++)
  38.         {
  39.             for (int j = 0; j < cols; j++)
  40.             {
  41.                 BoardGrid[i][j] = 0;
  42.             }
  43.         }
  44.  
  45.  
  46.         White_Pieces.add(new King(3,0,true,"King",this));
  47.         White_Pieces.add(new Queen(4,0,true,"Queen",this));
  48.         White_Pieces.add(new Bishop(2,0,true,"Bishop",this));
  49.         White_Pieces.add(new Bishop(5,0,true,"Bishop",this));
  50.         White_Pieces.add(new Knight(1,0,true,"Knight",this));
  51.         White_Pieces.add(new Knight(6,0,true,"Knight",this));
  52.         White_Pieces.add(new Rook(0,0,true,"Rook",this));
  53.         White_Pieces.add(new Rook(7,0,true,"Rook",this));
  54.         White_Pieces.add(new Pawn(0,1,true,"Pawn",this));
  55.         White_Pieces.add(new Pawn(1,1,true,"Pawn",this));
  56.         White_Pieces.add(new Pawn(2,1,true,"Pawn",this));
  57.         White_Pieces.add(new Pawn(3,1,true,"Pawn",this));
  58.         White_Pieces.add(new Pawn(4,1,true,"Pawn",this));
  59.         White_Pieces.add(new Pawn(5,1,true,"Pawn",this));
  60.         White_Pieces.add(new Pawn(6,1,true,"Pawn",this));
  61.         White_Pieces.add(new Pawn(7,1,true,"Pawn",this));
  62.  
  63.         Black_Pieces.add(new King(3,7,false,"King",this));
  64.         Black_Pieces.add(new Queen(4,7,false,"Queen",this));
  65.         Black_Pieces.add(new Bishop(2,7,false,"Bishop",this));
  66.         Black_Pieces.add(new Bishop(5,7,false,"Bishop",this));
  67.         Black_Pieces.add(new Knight(1,7,false,"Knight",this));
  68.         Black_Pieces.add(new Knight(6,7,false,"Knight",this));
  69.         Black_Pieces.add(new Rook(0,7,false,"Rook",this));
  70.         Black_Pieces.add(new Rook(7,7,false,"Rook",this));
  71.         Black_Pieces.add(new Pawn(0,6,false,"Pawn",this));
  72.         Black_Pieces.add(new Pawn(1,6,false,"Pawn",this));
  73.         Black_Pieces.add(new Pawn(2,6,false,"Pawn",this));
  74.         Black_Pieces.add(new Pawn(3,6,false,"Pawn",this));
  75.         Black_Pieces.add(new Pawn(4,6,false,"Pawn",this));
  76.         Black_Pieces.add(new Pawn(5,6,false,"Pawn",this));
  77.         Black_Pieces.add(new Pawn(6,6,false,"Pawn",this));
  78.         Black_Pieces.add(new Pawn(7,6,false,"Pawn",this));
  79.  
  80.     }
  81.  
  82.     public Board() {
  83.  
  84.         BoardGrid = new Integer[rows][cols];
  85.         Static_Shapes = new ArrayList();
  86.         Piece_Graphics = new ArrayList();
  87.         White_Pieces = new ArrayList();
  88.         Black_Pieces = new ArrayList();
  89.  
  90.         initGrid();
  91.  
  92.        
  93.         this.setPreferredSize(new Dimension(520, 520));
  94.  
  95.         this.addMouseListener(mouseAdapter);
  96.         //this.addComponentListener(componentAdapter);
  97.         //this.addKeyListener(keyAdapter);
  98.  
  99.  
  100.        
  101.         this.setVisible(true);
  102.         this.requestFocus();
  103.         drawMenu();
  104.     }
  105.  
  106.     private void drawWinner(String winner){
  107.         //Reset semua bidak catur
  108.         White_Pieces.removeAll(White_Pieces);
  109.         Black_Pieces.removeAll(Black_Pieces);
  110.         //Reset semua gambar bidak yg terload
  111.         Piece_Graphics.clear();
  112.         Static_Shapes.clear();
  113.         Image board = loadImage("images" + File.separator + "win_"+winner+".png");
  114.         Static_Shapes.add(new DrawingImage(board, new Rectangle2D.Double(0, 0, board.getWidth(null), board.getHeight(null))));
  115.         this.repaint();
  116.         initGrid();
  117.     }
  118.     private void drawMenu(){
  119.         //Reset semua bidak catur
  120.         White_Pieces.removeAll(White_Pieces);
  121.         Black_Pieces.removeAll(Black_Pieces);
  122.         //Reset semua gambar bidak yg terload
  123.         Piece_Graphics.clear();
  124.         Static_Shapes.clear();
  125.         Image board = loadImage("images" + File.separator + "menu.png");
  126.         Static_Shapes.add(new DrawingImage(board, new Rectangle2D.Double(0, 0, board.getWidth(null), board.getHeight(null))));
  127.         this.repaint();
  128.         initGrid();
  129.     }
  130.  
  131.     private void drawBoard()
  132.     {
  133.         Piece_Graphics.clear();
  134.         Static_Shapes.clear();
  135.         String winner = "";
  136.         Image board = loadImage(board_file_path);
  137.         Static_Shapes.add(new DrawingImage(board, new Rectangle2D.Double(0, 0, board.getWidth(null), board.getHeight(null))));
  138.         if (Active_Piece != null){
  139.             Image active_square = loadImage("images" + File.separator + "active_square.png");
  140.             Static_Shapes.add(new DrawingImage(active_square, new Rectangle2D.Double(Square_Width*Active_Piece.getX(),Square_Width*Active_Piece.getY(), active_square.getWidth(null), active_square.getHeight(null))));
  141.         }
  142.         boolean is_white_king_exist = false;
  143.         for (int i=0;i<White_Pieces.size(); i++){
  144.             if(White_Pieces.get(i).getFilePath().equals("King")) is_white_king_exist = true;
  145.             int COL = White_Pieces.get(i).getX();
  146.             int ROW = White_Pieces.get(i).getY();
  147.             Image piece = loadImage("images" + File.separator + "white_pieces" + File.separator + White_Pieces.get(i).getFilePath() + ".png");
  148.             Piece_Graphics.add(new DrawingImage(piece, new Rectangle2D.Double(Square_Width*COL,Square_Width*ROW, piece.getWidth(null), piece.getHeight(null))));
  149.         }
  150.         boolean is_black_king_exist = false;
  151.         for (int i = 0; i < Black_Pieces.size(); i++){
  152.             if(Black_Pieces.get(i).getFilePath().equals("King")) is_black_king_exist = true;
  153.             int COL = Black_Pieces.get(i).getX();
  154.             int ROW = Black_Pieces.get(i).getY();
  155.             Image piece = loadImage("images" + File.separator + "black_pieces" + File.separator + Black_Pieces.get(i).getFilePath() + ".png");
  156.             Piece_Graphics.add(new DrawingImage(piece, new Rectangle2D.Double(Square_Width*COL,Square_Width*ROW, piece.getWidth(null), piece.getHeight(null))));
  157.         }
  158.         if(!is_white_king_exist) drawWinner("black");
  159.         if(!is_black_king_exist) drawWinner("white");
  160.         this.repaint();
  161.     }
  162.  
  163.    
  164.     public Piece getPiece(int x, int y) {
  165.         for (Piece p : White_Pieces){
  166.             if (p.getX() == x && p.getY() == y){
  167.                 return p;
  168.             }
  169.         }
  170.         for (Piece p : Black_Pieces){
  171.             if (p.getX() == x && p.getY() == y){
  172.                 return p;
  173.             }
  174.         }
  175.         return null;
  176.     }
  177.  
  178.  
  179.     private MouseAdapter mouseAdapter = new MouseAdapter() {
  180.  
  181.         @Override
  182.         public void mouseClicked(MouseEvent e){
  183.         }
  184.         @Override
  185.         public void mousePressed(MouseEvent e) {
  186.             int d_X = e.getX();
  187.             int d_Y = e.getY();  
  188.             int Clicked_Row = d_Y / Square_Width;
  189.             int Clicked_Column = d_X / Square_Width;
  190.             boolean is_whites_turn = true;
  191.             boolean king_killed = false;
  192.             if (turnCounter%2 == 1){
  193.                 is_whites_turn = false;
  194.             }
  195.             Piece clicked_piece = getPiece(Clicked_Column, Clicked_Row);
  196.             if (Active_Piece == null && clicked_piece != null &&
  197.                     ((is_whites_turn && clicked_piece.isWhite()) || (!is_whites_turn && clicked_piece.isBlack())))
  198.             {
  199.                 Active_Piece = clicked_piece;
  200.             }
  201.             else if (Active_Piece != null && Active_Piece.getX() == Clicked_Column && Active_Piece.getY() == Clicked_Row)
  202.             {
  203.                 Active_Piece = null;
  204.             }
  205.             else if (Active_Piece != null && Active_Piece.canMove(Clicked_Column, Clicked_Row)
  206.                     && ((is_whites_turn && Active_Piece.isWhite()) || (!is_whites_turn && Active_Piece.isBlack())))
  207.             {
  208.                 // Jika posisi target sudah ada bidak, maka diganti dengan bidak yg dijalankan
  209.                 if (clicked_piece != null)
  210.                 {
  211.                     if (clicked_piece.isWhite())
  212.                     {
  213.                         White_Pieces.remove(clicked_piece);
  214.                     }
  215.                     else
  216.                     {
  217.                         Black_Pieces.remove(clicked_piece);
  218.                     }
  219.                 }
  220.  
  221.                 Active_Piece.setX(Clicked_Column);
  222.                 Active_Piece.setY(Clicked_Row);
  223.                
  224.                 // Jika bidak yg dijalankan adalah pion, maka ubah status setHasMoved
  225.                 if (Active_Piece.getClass().equals(Pawn.class))
  226.                 {
  227.                     Pawn castedPawn = (Pawn)(Active_Piece);
  228.                     castedPawn.setHasMoved(true);
  229.                 }
  230.                
  231.                
  232.                 Active_Piece = null;
  233.                 turnCounter++;
  234.             }
  235.            
  236.             drawBoard();
  237.         }    
  238.     };
  239.  
  240.     private void adjustShapePositions(double dx, double dy) {
  241.  
  242.         Static_Shapes.get(0).adjustPosition(dx, dy);
  243.         this.repaint();
  244.  
  245.     }
  246.        
  247.        
  248.      
  249.     private Image loadImage(String imageFile) {
  250.         try {
  251.             return ImageIO.read(new File(imageFile));
  252.         }
  253.         catch (IOException e) {
  254.                 return NULL_IMAGE;
  255.         }
  256.     }
  257.  
  258.     @Override
  259.     protected void paintComponent(Graphics g) {
  260.  
  261.         super.paintComponent(g);
  262.  
  263.         Graphics2D g2 = (Graphics2D)g;
  264.         drawBackground(g2);
  265.         drawShapes(g2);
  266.     }
  267.  
  268.     private void drawBackground(Graphics2D g2) {
  269.         g2.setColor(getBackground());
  270.         g2.fillRect(0,  0, getWidth(), getHeight());
  271.     }
  272.        
  273.  
  274.     private void drawShapes(Graphics2D g2) {
  275.         for (DrawingShape shape : Static_Shapes) {
  276.             shape.draw(g2);
  277.         }    
  278.         for (DrawingShape shape : Piece_Graphics) {
  279.             shape.draw(g2);
  280.         }
  281.     }
  282.  
  283.     private ComponentAdapter componentAdapter = new ComponentAdapter() {
  284.  
  285.         @Override
  286.         public void componentHidden(ComponentEvent e) {
  287.  
  288.         }
  289.  
  290.         @Override
  291.         public void componentMoved(ComponentEvent e) {
  292.  
  293.         }
  294.  
  295.         @Override
  296.         public void componentResized(ComponentEvent e) {
  297.  
  298.         }
  299.  
  300.         @Override
  301.         public void componentShown(ComponentEvent e) {
  302.  
  303.         }    
  304.     };
  305.  
  306.     private KeyAdapter keyAdapter = new KeyAdapter() {
  307.  
  308.         @Override
  309.         public void keyPressed(KeyEvent e) {
  310.  
  311.         }
  312.  
  313.         @Override
  314.         public void keyReleased(KeyEvent e) {
  315.  
  316.         }
  317.  
  318.         @Override
  319.         public void keyTyped(KeyEvent e) {
  320.  
  321.         }    
  322.     };
  323.  
  324. }
  325.  
  326.  
  327.  
  328. interface DrawingShape {
  329.     boolean contains(Graphics2D g2, double x, double y);
  330.     void adjustPosition(double dx, double dy);
  331.     void draw(Graphics2D g2);
  332. }
  333.  
  334.  
  335. class DrawingImage implements DrawingShape {
  336.  
  337.     public Image image;
  338.     public Rectangle2D rect;
  339.  
  340.     public DrawingImage(Image image, Rectangle2D rect) {
  341.             this.image = image;
  342.             this.rect = rect;
  343.     }
  344.  
  345.     @Override
  346.     public boolean contains(Graphics2D g2, double x, double y) {
  347.             return rect.contains(x, y);
  348.     }
  349.  
  350.     @Override
  351.     public void adjustPosition(double dx, double dy) {
  352.             rect.setRect(rect.getX() + dx, rect.getY() + dy, rect.getWidth(), rect.getHeight());    
  353.     }
  354.  
  355.     @Override
  356.     public void draw(Graphics2D g2) {
  357.             Rectangle2D bounds = rect.getBounds2D();
  358.             g2.drawImage(image, (int)bounds.getMinX(), (int)bounds.getMinY(), (int)bounds.getMaxX(), (int)bounds.getMaxY(),
  359.                                             0, 0, image.getWidth(null), image.getHeight(null), null);
  360.     }    
  361. }
Add Comment
Please, Sign In to add comment