如何用java画棋盘类的背景,就是那些方格。
1个回答
展开全部
一个10×10的黑肢埋白双色
private BufferedImage chessboard;
private BufferedImage createChessBoard(){
BufferedImage texture = new BufferedImage(80, 80, BufferedImage.TYPE_BYTE_BINARY);
Graphics2D g2 = texture.createGraphics();
g2.setPaint(Color.WHITE);
g2.fillRect(0,0,80,80);
g2.setPaint(Color.BLACK);
g2.fillRect(0,0,40,40);
g2.fillRect(40,40,80,80);
g2.dispose();
TexturePaint paint = new TexturePaint(texture, new Rectangle2D.Double(0,0,80,80));
BufferedImage chessboard = new BufferedImage(400,400, BufferedImage.TYPE_BYTE_BINARY);
Graphics2D g2d = chessboard.createGraphics();
g2d.setPaint(paint);
g2d.fillRect(0,0,400,400);
g2d.dispose();
return chessboard;
}
@Override
public void paintComponent(Graphics g){
if(chessboard == null)
chessboard = createChessBoard();
g.drawImage(chessboard, 0, 0, null);
...
}
可以根碰仔据需笑饥汪要修改。
private BufferedImage chessboard;
private BufferedImage createChessBoard(){
BufferedImage texture = new BufferedImage(80, 80, BufferedImage.TYPE_BYTE_BINARY);
Graphics2D g2 = texture.createGraphics();
g2.setPaint(Color.WHITE);
g2.fillRect(0,0,80,80);
g2.setPaint(Color.BLACK);
g2.fillRect(0,0,40,40);
g2.fillRect(40,40,80,80);
g2.dispose();
TexturePaint paint = new TexturePaint(texture, new Rectangle2D.Double(0,0,80,80));
BufferedImage chessboard = new BufferedImage(400,400, BufferedImage.TYPE_BYTE_BINARY);
Graphics2D g2d = chessboard.createGraphics();
g2d.setPaint(paint);
g2d.fillRect(0,0,400,400);
g2d.dispose();
return chessboard;
}
@Override
public void paintComponent(Graphics g){
if(chessboard == null)
chessboard = createChessBoard();
g.drawImage(chessboard, 0, 0, null);
...
}
可以根碰仔据需笑饥汪要修改。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询