Java象棋方块图像

Java象棋方块图像,java,chess,Java,Chess,我以前问过这个问题,但没有得到答案 我目前正在用Java设计一个棋盘,取得了一些成功。我需要从我的电脑中为电路板上的每个方块添加一个背景图像,我目前编写的背景图像在蓝色和白色之间交替 我会在代码中添加什么来改变这一点 我的代码: import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class chessboard extends JFrame imple

我以前问过这个问题,但没有得到答案

我目前正在用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);
 }
}
import java.awt.*;
导入java.awt.event.*;
导入java.util.*;
导入javax.swing.*;
公共类棋盘扩展JFrame实现MouseListener、MouseMotionListener
{
JLayeredPane layeredPane;
JPanel棋盘;
JLabel棋子;
内部调整;
内部调整;
公共棋盘(){
尺寸板尺寸=新尺寸(600600);
//为此应用程序使用分层窗格
layeredPane=新的JLayeredPane();
getContentPane().add(layeredPane);
layeredPane.setPreferredSize(boardSize);
layeredPane.addMouseListener(此);
layeredPane.addMouseMotionListener(此);
//将棋盘添加到分层窗格
棋盘=新JPanel();
layeredPane.add(棋盘,JLayeredPane.DEFAULT_层);
棋盘布局(新网格布局(8,8));
棋盘。设置首选大小(棋盘大小);
棋盘.立根(0,0,boardSize.width,boardSize.height);
对于(int i=0;i<64;i++){
JPanel square=newjpanel(newborderlayout());
棋盘。加(正方形);
int行=(i/8)%2;
如果(行==0)
正方形。退根(i%2==0?颜色。蓝色:颜色。白色);
其他的
正方形。退根(i%2==0?颜色。白色:颜色。蓝色);
}
JLabel-piece=newjlabel(newimageicon(“/Users/Downloads/pieces/Rook.jpg”);
JPanel面板=(JPanel)chessBoard.getComponent(0);
面板。添加(件);
piece=newjlabel(newimageicon(“/Users/Downloads/pieces/Knight.jpg”);
panel=(JPanel)chessBoard.getComponent(1);
面板。添加(件);
piece=newjlabel(newimageicon(“/Users/Downloads/pieces/King.jpg”);
panel=(JPanel)chessBoard.getComponent(2);
面板。添加(件);
piece=newjlabel(newimageicon(“/Users/Downloads/pieces/Queen.jpg”);
panel=(JPanel)chessBoard.getComponent(3);
面板。添加(件);
piece=newjlabel(newimageicon(“/Users/Downloads/pieces/Bishop.jpg”);
panel=(JPanel)chessBoard.getComponent(4);
面板。添加(件);
piece=newjlabel(newimageicon(“/Users/Downloads/pieces/Knight.jpg”);
panel=(JPanel)chessBoard.getComponent(5);
面板。添加(件);
piece=newjlabel(newimageicon(“/Users/Downloads/pieces/Bishop.jpg”);
panel=(JPanel)chessBoard.getComponent(6);
面板。添加(件);
piece=newjlabel(newimageicon(“/Users/Downloads/pieces/Rook.jpg”);
panel=(JPanel)chessBoard.getComponent(7);
面板。添加(件);
piece=newjlabel(newimageicon(“/Users/Downloads/pieces/Pawn.jpg”);
panel=(JPanel)chessBoard.getComponent(9);
面板。添加(件);
piece=newjlabel(newimageicon(“/Users/Downloads/pieces/Pawn.jpg”);
panel=(JPanel)chessBoard.getComponent(10);
面板。添加(件);
piece=newjlabel(newimageicon(“/Users/Downloads/pieces/Pawn.jpg”);
panel=(JPanel)chessBoard.getComponent(11);
面板。添加(件);
piece=newjlabel(newimageicon(“/Users/Downloads/pieces/Pawn.jpg”);
panel=(JPanel)chessBoard.getComponent(12);
面板。添加(件);
piece=newjlabel(newimageicon(“/Users/Downloads/pieces/Pawn.jpg”);
panel=(JPanel)chessBoard.getComponent(13);
面板。添加(件);
piece=newjlabel(newimageicon(“/Users/Downloads/pieces/Pawn.jpg”);
panel=(JPanel)chessBoard.getComponent(14);
面板。添加(件);
piece=newjlabel(newimageicon(“/Users/Downloads/pieces/Pawn.jpg”);
panel=(JPanel)chessBoard.getComponent(15);
面板。添加(件);
piece=newjlabel(newimageicon(“/Users/Downloads/pieces/Pawn.jpg”);
panel=(JPanel)chessBoard.getComponent(8);
面板。添加(件);
}
公共无效鼠标按下(MouseEvent e){
棋子=空;
组件c=chessBoard.findComponentAt(e.getX(),e.getY());
if(JPanel的c实例)
返回;
Point parentLocation=c.getParent().getLocation();
xAdjustment=parentLocation.x-e.getX();
yAdjustment=parentLocation.y-e.getY();
棋子=(JLabel)c;
chessPiece.setLocation(e.getX()+xaadjustment,e.getY()+yaadjustment);
chessPiece.setSize(chessPiece.getWidth(),chessPiece.getHeight());
layeredPane.add(棋子,JLayeredPane.DRAG_层);
}
//把棋子四处移动
公共无效鼠标标记(MouseEvent me){
if(chessPiece==null)返回;
c hesspice.setLocation(me.getX()+xaadjustment,me.getY()+yaadjustment);
}
公共无效MouseEvent(MouseEvent e){
if(chessPiece==null)返回;
棋子。设置可见(假);
组件c=chessBoard.findComponentAt(e.getX(),e.getY());
if(JLabel的c实例){
容器父级=c.getParent();
父。删除(0);
添加(棋子);
}
否则{
容器父项=(容器)c;
添加(棋子);
}
棋子。设置可见(真);
}
公共无效mouseClicked(MouseEvent e){
}
public void mouseMoved(MouseEvent e){
}
公共无效鼠标事件(鼠标事件e){
}
公共无效mouseExited(MouseEvent e){
}
公共静态void main(字符串[]args){
JFrame=新棋盘();
frame.setDefaultCloseOperation(在关闭时处理);
frame.pack();
frame.setresizeable(true);
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
}

对于每个方块,您需要将图像设置为JPanel,而不仅仅是更改颜色

@MadProgrammer双重发布是被鼓励的吗?可能有一个很好的理由你没有得到答案,双重发布是不被鼓励的,或者welcome@user2573153对不起,我的不好,谢谢。发布代码时,最好只发布相关部分。