Starlight-30036225 / ChessTCP

FILLMELATER
0 stars 0 forks source link

Create swing class #4

Closed Starlight-30036225 closed 7 months ago

Starlight-30036225 commented 9 months ago

I have had to seperate the swift across two classes, One that handles the display of the board, and one to handle inputs.

The display class reads the current state of the board (or would if i had this implemented) and displays it.

` private static JFrame getFrame() {

    //sets up JFrame
    JFrame frame = new JFrame();
    frame.setBounds(200, 100, BOARD_WIDTH + MARGIN, BOARD_HEIGHT + MARGIN);
    frame.setUndecorated(false);
    frame.setName("CHESS");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setBackground(Color.LIGHT_GRAY);
    return frame;
}`

This is the basic setup for the Jframe, it uses 3 constants for the size of the window

` private static void ExtractImages() { BufferedImage BaseImage;

    try {
        BaseImage = ImageIO.read(new File("src/Resources/chess.png"));
    } catch (IOException e) {
        System.out.println("Couldn't find image");
        throw new RuntimeException(e);
    }

    imgList = new Image[12];        //Creates an array of 12 images

    int ind = 0;        //saves the index

    for (int y = 0; y < 400; y += 200) {
        for (int x = 0; x < 1200; x += 200) {
            imgList[ind] = BaseImage.getSubimage(x, y, 200, 200).getScaledInstance(64, 64, BufferedImage.SCALE_SMOOTH);
            ind++;
        }
    }
}`

This snippet extracts sprites from the pieces from a sprite sheet, and saves them to a global array

Starlight-30036225 commented 9 months ago

For displaying the board and then the sprites on the frame: ` public void paint(Graphics g) { g.setFont(new Font("Serif", BOLD, 14));

            boolean white = true;       //FlipFlops
            for (int Y = 0; Y < 8; Y++) {       //8 rows and 8 cols

                // add text to label
                g.setColor(Color.BLACK);
                g.drawString(String.valueOf(Y + 1), MARGIN/2, (int) (Y * 64 + (MARGIN * 2.3)));
                g.drawString(String.valueOf((char) ((Y ) + 97)),  (int) (Y * 64 + (MARGIN * 2)),MARGIN/2);

                for (int X = 0; X < 8; X++) {
                    if (white) {
                        g.setColor(Color.YELLOW);

                    } else {
                        g.setColor(Color.PINK);
                    }

                    white = !white;  //flips the flop

                    g.fillRect(X * 64 + MARGIN, Y * 64 + MARGIN, 64, 64);
                }
                white = !white;
            }
            DrawPieces(g);
        }`

This function draws an 8 by 8 board, and label them with ranks and files. Then calls the draw pieces function to handle the pieces

` private void DrawPieces(Graphics g) { for (SimplePiece[] Row : Board) { for (SimplePiece p: Row) { if (p == null) {continue;} int imageVal = p.getImageval(); if (p == SelectedPiece) { g.drawImage(imgList[imageVal], MouseX, MouseY, this); } else{ g.drawImage(imgList[imageVal], p.getX() SQUARE_SIZE + MARGIN, p.getY() SQUARE_SIZE+MARGIN, this); }

                }
            }
        }

    };

`

This loops through all spaces on the board to check for a valid piece, then uses its piece class to select the appropriate image for that piece.

public int getImageval() { return type.BaseImageVal + (white? 0 : 6); }

This function is from inside the simple Piece class, and uses the type enum to find the base image value (which is always the same piece in white) then if the colour is white it is fine, if its is black it adds 6 (moving its index to the corresponding black piece in the sprite array)