0
我之前問過這個問題,沒有得到答案。爪哇國際象棋廣場圖片
我目前正在用Java設計一個棋盤,並取得了一些成功。我需要從我的電腦上爲棋盤上的每個方塊添加一個背景圖像,我目前正在寫這些圖像以在藍色和白色之間切換。
我會在我的代碼中添加什麼來改變它?
我的代碼:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class chessboard extends JFrame implements MouseListener, MouseMotionListener
{
JLayeredPane layeredPane;
JPanel chessBoard;
JLabel chessPiece;
int xAdjustment;
int yAdjustment;
public chessboard(){
Dimension boardSize = new Dimension(600, 600);
// Use a Layered Pane for this this application
layeredPane = new JLayeredPane();
getContentPane().add(layeredPane);
layeredPane.setPreferredSize(boardSize);
layeredPane.addMouseListener(this);
layeredPane.addMouseMotionListener(this);
//Add a chess board to the Layered Pane
chessBoard = new JPanel();
layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
chessBoard.setLayout(new GridLayout(8, 8));
chessBoard.setPreferredSize(boardSize);
chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);
for (int i = 0; i < 64; i++) {
JPanel square = new JPanel(new BorderLayout());
chessBoard.add(square);
int row = (i/8) % 2;
if (row == 0)
square.setBackground(i % 2 == 0 ? Color.blue : Color.white);
else
square.setBackground(i % 2 == 0 ? Color.white : Color.blue);
}
JLabel piece = new JLabel(new ImageIcon("/Users/Downloads/pieces/Rook.jpg"));
JPanel panel = (JPanel)chessBoard.getComponent(0);
panel.add(piece);
piece = new JLabel(new ImageIcon("/Users/Downloads/pieces/Knight.jpg"));
panel = (JPanel)chessBoard.getComponent(1);
panel.add(piece);
piece = new JLabel(new ImageIcon("/Users/Downloads/pieces/King.jpg"));
panel = (JPanel)chessBoard.getComponent(2);
panel.add(piece);
piece = new JLabel(new ImageIcon("/Users/Downloads/pieces/Queen.jpg"));
panel = (JPanel)chessBoard.getComponent(3);
panel.add(piece);
piece = new JLabel(new ImageIcon("/Users/Downloads/pieces/Bishop.jpg"));
panel = (JPanel)chessBoard.getComponent(4);
panel.add(piece);
piece = new JLabel(new ImageIcon("/Users/Downloads/pieces/Knight.jpg"));
panel = (JPanel)chessBoard.getComponent(5);
panel.add(piece);
piece = new JLabel(new ImageIcon("/Users/Downloads/pieces/Bishop.jpg"));
panel =(JPanel)chessBoard.getComponent(6);
panel.add(piece);
piece = new JLabel(new ImageIcon("/Users/Downloads/pieces/Rook.jpg"));
panel =(JPanel)chessBoard.getComponent(7);
panel.add(piece);
piece = new JLabel(new ImageIcon("/Users/Downloads/pieces/Pawn.jpg"));
panel =(JPanel)chessBoard.getComponent(9);
panel.add(piece);
piece = new JLabel(new ImageIcon("/Users/Downloads/pieces/Pawn.jpg"));
panel =(JPanel)chessBoard.getComponent(10);
panel.add(piece);
piece = new JLabel(new ImageIcon("/Users/Downloads/pieces/Pawn.jpg"));
panel =(JPanel)chessBoard.getComponent(11);
panel.add(piece);
piece = new JLabel(new ImageIcon("/Users/Downloads/pieces/Pawn.jpg"));
panel =(JPanel)chessBoard.getComponent(12);
panel.add(piece);
piece = new JLabel(new ImageIcon("/Users/Downloads/pieces/Pawn.jpg"));
panel =(JPanel)chessBoard.getComponent(13);
panel.add(piece);
piece = new JLabel(new ImageIcon("/Users/Downloads/pieces/Pawn.jpg"));
panel =(JPanel)chessBoard.getComponent(14);
panel.add(piece);
piece = new JLabel(new ImageIcon("/Users/Downloads/pieces/Pawn.jpg"));
panel =(JPanel)chessBoard.getComponent(15);
panel.add(piece);
piece = new JLabel(new ImageIcon("/Users/Downloads/pieces/Pawn.jpg"));
panel =(JPanel)chessBoard.getComponent(8);
panel.add(piece);
}
public void mousePressed(MouseEvent e){
chessPiece = null;
Component c = chessBoard.findComponentAt(e.getX(), e.getY());
if (c instanceof JPanel)
return;
Point parentLocation = c.getParent().getLocation();
xAdjustment = parentLocation.x - e.getX();
yAdjustment = parentLocation.y - e.getY();
chessPiece = (JLabel)c;
chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());
layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
}
//Move the chess piece around
public void mouseDragged(MouseEvent me) {
if (chessPiece == null) return;
c hessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
}
public void mouseReleased(MouseEvent e) {
if(chessPiece == null) return;
chessPiece.setVisible(false);
Component c = chessBoard.findComponentAt(e.getX(), e.getY());
if (c instanceof JLabel){
Container parent = c.getParent();
parent.remove(0);
parent.add(chessPiece);
}
else {
Container parent = (Container)c;
parent.add(chessPiece);
}
chessPiece.setVisible(true);
}
public void mouseClicked(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e) {
}
public static void main(String[] args) {
JFrame frame = new chessboard();
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frame.pack();
frame.setResizable(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
@MadProgrammer雙張貼被鼓勵? – user2573153
可能有一個很好的理由,你沒有得到答案,雙張貼是不鼓勵或歡迎 – MadProgrammer
@ user2573153對不起,我的壞,謝謝。 – MadProgrammer