Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.GridLayout;
- import java.awt.Image;
- import java.awt.event.ActionEvent;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- import javax.swing.Icon;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class ChessBoard extends JPanel {
- //implements MouseListener, MouseMotionListener
- private int activeButton;
- private ImageIcon player;
- JButton grid[][] = new JButton[8][8];
- JButton square;
- public ChessBoard(){
- //black pieces
- ImageIcon blackCastle = new ImageIcon("Images/BlackCastle.jpg");
- Image bc = blackCastle.getImage();
- Image bcimg = bc.getScaledInstance(90, 90, java.awt.Image.SCALE_SMOOTH);
- ImageIcon bcIcon = new ImageIcon(bcimg);
- ImageIcon blackBishop = new ImageIcon("Images/BlackBishop.jpg");
- Image bb = blackBishop.getImage();
- Image bbimg = bb.getScaledInstance(90, 90, java.awt.Image.SCALE_SMOOTH);
- ImageIcon bbIcon = new ImageIcon(bbimg);
- ImageIcon blackKing = new ImageIcon("Images/BlackKing.jpg");
- Image bK = blackKing.getImage();
- Image bKimg = bK.getScaledInstance(90, 90, java.awt.Image.SCALE_SMOOTH);
- ImageIcon bKIcon = new ImageIcon(bKimg);
- ImageIcon blackPawn = new ImageIcon("Images/BlackPond.jpeg");
- Image bp = blackPawn.getImage();
- Image bpimg = bp.getScaledInstance(90, 90, java.awt.Image.SCALE_SMOOTH);
- ImageIcon bpIcon = new ImageIcon(bpimg);
- ImageIcon blackQueen = new ImageIcon("Images/BlackQueen.jpg");
- Image bq = blackQueen.getImage();
- Image bqimg = bq.getScaledInstance(90, 90, java.awt.Image.SCALE_SMOOTH);
- ImageIcon bqIcon = new ImageIcon(bqimg);
- ImageIcon BlackKnite = new ImageIcon("Images/BlackKnight.jpg");
- Image bk = BlackKnite.getImage();
- Image bkimg = bk.getScaledInstance(90, 90, java.awt.Image.SCALE_SMOOTH);
- ImageIcon bkIcon = new ImageIcon(bkimg);
- //white pieces
- ImageIcon whiteCastle = new ImageIcon("Images/WhiteCastle.jpg");
- Image wc = whiteCastle.getImage();
- Image wcimg = wc.getScaledInstance(90, 90, java.awt.Image.SCALE_SMOOTH);
- ImageIcon wcIcon = new ImageIcon(wcimg);
- ImageIcon whiteBishop = new ImageIcon("Images/WhiteBishop.jpg");
- Image wb = whiteBishop.getImage();
- Image wbimg = wb.getScaledInstance(90, 90, java.awt.Image.SCALE_SMOOTH);
- ImageIcon wbIcon = new ImageIcon(wbimg);
- ImageIcon whiteKing = new ImageIcon("Images/WhiteKing.jpg");
- Image wK = whiteKing.getImage();
- Image wKimg = wK.getScaledInstance(90, 90, java.awt.Image.SCALE_SMOOTH);
- ImageIcon wKIcon = new ImageIcon(wKimg);
- ImageIcon whitePawn = new ImageIcon("Images/WhitePawn.jpg");
- Image wp = whitePawn.getImage();
- Image wpimg = wp.getScaledInstance(90, 90, java.awt.Image.SCALE_SMOOTH);
- ImageIcon wpIcon = new ImageIcon(wpimg);
- ImageIcon whiteQueen = new ImageIcon("Images/WhiteQueen.jpg");
- Image wq = whiteQueen.getImage();
- Image wqimg = wq.getScaledInstance(90, 90, java.awt.Image.SCALE_SMOOTH);
- ImageIcon wqIcon = new ImageIcon(wqimg);
- ImageIcon whiteKnite = new ImageIcon("Images/WhiteKnight.jpg");
- Image wk = whiteKnite.getImage();
- Image wkimg = wk.getScaledInstance(90, 90, java.awt.Image.SCALE_SMOOTH);
- ImageIcon wkIcon = new ImageIcon(wkimg);
- //set up board
- JFrame board = new JFrame();
- board.setSize(640,640);
- board.setLayout(new GridLayout(8,8));
- //create 64 button field
- for(int x = 0; x<8; x++)
- {
- for(int y = 0; y<8; y++)
- {
- JButton square = new JButton();
- if (x % 2 == 0)
- if (y % 2 == 0)
- square.setBackground(Color.RED);
- else
- square.setBackground(Color.BLACK);
- if (x % 2 == 1)
- if (y % 2 == 1)
- square.setBackground(Color.RED);
- else
- square.setBackground(Color.BLACK);
- square.setOpaque(true);
- grid[x][y] = square;
- board.add(grid[x][y]);
- }
- }
- //row 1
- grid[0][0].setIcon(bcIcon);
- grid[0][1].setIcon(bkIcon);
- grid[0][2].setIcon(bbIcon);
- grid[0][3].setIcon(bKIcon);
- grid[0][4].setIcon(bqIcon);
- grid[0][5].setIcon(bbIcon);
- grid[0][6].setIcon(bkIcon);
- grid[0][7].setIcon(bcIcon);
- //row2
- grid[1][0].setIcon(bpIcon);
- grid[1][1].setIcon(bpIcon);
- grid[1][2].setIcon(bpIcon);
- grid[1][3].setIcon(bpIcon);
- grid[1][4].setIcon(bpIcon);
- grid[1][5].setIcon(bpIcon);
- grid[1][6].setIcon(bpIcon);
- grid[1][7].setIcon(bpIcon);
- //row8
- grid[7][0].setIcon(wcIcon);
- grid[7][1].setIcon(wkIcon);
- grid[7][2].setIcon(wbIcon);
- grid[7][3].setIcon(wqIcon);
- grid[7][4].setIcon(wKIcon);
- grid[7][5].setIcon(wbIcon);
- grid[7][6].setIcon(wkIcon);
- grid[7][7].setIcon(wcIcon);
- //row7
- grid[6][0].setIcon(wpIcon);
- grid[6][1].setIcon(wpIcon);
- grid[6][2].setIcon(wpIcon);
- grid[6][3].setIcon(wpIcon);
- grid[6][4].setIcon(wpIcon);
- grid[6][5].setIcon(wpIcon);
- grid[6][6].setIcon(wpIcon);
- grid[6][7].setIcon(wpIcon);
- board.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- board.setVisible(true);
- }
- //This is the part that I am having trouble with. I need to find a way to select the icon and get it to move to the other JButton as well as remove it from the original JButton.
- public void actionPerformed(ActionEvent evt) {
- Object source = evt.getSource();
- if (source instanceof JButton) {
- JButton clicked = (JButton)source;
- grid[8][8].get(clicked).setIcon(null);
- clicked.setIcon(player);
- activeButton = grid.indexOf(clicked);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement