java代码布局棋盘 java画棋盘
求代码:用JAVA画出一个棋盘(伪代码就好,要有标注)
import javax.swing.*;
目前成都创新互联已为1000多家的企业提供了网站建设、域名、网络空间、网站运营、企业网站设计、宝坻网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
import javax.swing.border.*;
import java.awt.*;
import java.beans.*;
import java.util.*;
/**
* 面板上的光标改变成为手型
* @author hardneedl
*/
final class ChessPaint extends JFrame{
private static final Dimension SIZE = new Dimension(600,400);
public Dimension getMinimumSize() {return SIZE;}
public Dimension getMaximumSize() {return SIZE;}
public Dimension getPreferredSize() {return SIZE;}
public String getTitle() {return "ChessPaint";}
/**
* 棋盘
* 实现属性变化监听
*/
private class Chessboard extends Observable implements PropertyChangeListener{
private int columns, rows, cellWidth;
private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
private Chessboard(int columns, int rows, int cellWidth) {
this.columns = columns;
this.rows = rows;
this.cellWidth = cellWidth;
}
private int getColumns() {
return columns;
}
private void setColumns(int columns) {
propertyChangeSupport.firePropertyChange("columns", this.columns, columns);
this.columns = columns;
}
private int getRows() {
return rows;
}
private void setRows(int rows) {
propertyChangeSupport.firePropertyChange("columns", this.rows, rows);
this.rows = rows;
}
private int getCellWidth() {
return cellWidth;
}
private void setCellWidth(int cellWidth) {
this.cellWidth = cellWidth;
}
public void propertyChange(PropertyChangeEvent evt) {
switch (evt.getPropertyName()) {
case "columns":
case "rows":
case "cellWidth":
if (!evt.getOldValue().equals(evt.getNewValue())) {
setChanged();
notifyObservers(this);
}
break;
}
}
}
/**
* 画笔
*/
private interface Brush {
void paint(Graphics g);
Component getCompoment();
}
abstract private class ChessboardBrush implements Brush, Observer {
private Chessboard chessboard;
private ChessboardBrush(Chessboard chessboard) {
this.chessboard = chessboard;
}
public void paint(Graphics g) {
if (chessboard == null) return;
Graphics2D g2 = (Graphics2D) g.create();
//背景白色
g2.setColor(Color.WHITE);
g2.fillRect(0,0, getCompoment().getWidth(), getCompoment().getHeight());
//整体偏移坐标系
g2.translate(100,100);
g2.setColor(Color.BLACK);
//绘制行线
for (int r = 0; r = chessboard.getRows(); r ++)
g2.drawLine(0, r * chessboard.getCellWidth(), chessboard.getColumns() * chessboard.getCellWidth(), r * chessboard.getCellWidth());
//绘制竖线
for (int c = 0; c = chessboard.getColumns(); c++)
g2.drawLine(c * chessboard.getCellWidth(), 0, chessboard.getCellWidth() * c , chessboard.getRows() * chessboard.getCellWidth());
g2.dispose();
}
public void update(Observable o, Object arg) {
if (arg instanceof Chessboard)
chessboard = (Chessboard)arg;
}
}
/*画布*/
private class Canvas extends JComponent{
private Brush getBrush() {
return brush;
}
private void setBrush(Brush brush) {
this.brush = brush;
}
private Brush brush;
private Canvas() {
super();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if(isVisible() brush != null)
brush.paint(g);
}
public Border getBorder() {
return BorderFactory.createLineBorder(Color.BLUE,2);
}
}
private Canvas canvas;
private ChessboardBrush brush;
private Chessboard chessboard;
private ChessPaint() {
super();
init();
addListeners();
doLay();
}
private void init(){
chessboard = new Chessboard(19,19,30);
canvas = new Canvas();
brush = new ChessboardBrush(chessboard) {
public Component getCompoment() {
return canvas;
}
};
canvas.setBrush(brush);
chessboard.addObserver(brush);
}
private void addListeners(){
}
private void doLay(){
Container container = getContentPane();
container.add(canvas, BorderLayout.CENTER);
pack();
setVisible(true);
}
public static void main(String... args) {
System.setProperty("swing.defaultlaf","com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.invokeLater(ChessPaint::new);
}
}
怎么可以让java在五子棋盘上面随机产生棋子的位置? 下面是棋盘的代码
看下棋盘的row和column各多少行,然后建立一个2d数组
boolean[][] pos = new boolean[row][column];
初始化2d数组为boolean并且全部赋值为false
你要做的就是随机产生row和column,用Random里的nextInt(row 或者column)随机产生点,并将
pos[row][column] = true;
求写Java程序,可以画个棋盘。
代码如下:
// App.java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JFrame;
public class App extends JFrame {
public App() {
this.setTitle("Chess");
this.setSize(618, 647);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void paint(Graphics g) {
super.paint(g);
Rectangle rect = getRootPane().getBounds();
int blockWidth = rect.width / 8;
int blockHeight = rect.height / 8;
for (int i = 0; i 8; i++) {
for (int j = 0; j 8; j++) {
if (i % 2 == 0) {
g.setColor(j % 2 == 0 ? Color.RED : Color.BLACK);
} else {
g.setColor(j % 2 == 0 ? Color.BLACK : Color.RED);
}
g.fillRect(rect.x + j * blockWidth, rect.y + i * blockHeight, blockWidth, blockHeight);
}
}
}
public static void main(String[] args) {
new App().setVisible(true);
}
}
运行结果:
分享标题:java代码布局棋盘 java画棋盘
转载注明:http://cdiso.cn/article/dodescd.html