「java棋盘例子」java棋盘棋子走子

博主:adminadmin 2023-03-18 07:40:08 641

今天给各位分享java棋盘例子的知识,其中也会对java棋盘棋子走子进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

求代码:用JAVA画出一个棋盘(伪代码就好,要有标注)

import javax.swing.*;

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程序,可以画个棋盘。

代码如下:

// 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画出一个棋盘(伪代码就好)

代码如下:

package com.lijie.test;

import java.awt.Graphics;

import java.awt.Point;

import javax.swing.JFrame;

public class JFrameTestor extends JFrame {

public JFrameTestor() {

setSize(480, 320);

setLocation(new Point(400, 150));

setVisible(true);

setResizable(false);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String[] args) {

new JFrameTestor();

}

public void paint(Graphics g) {

for(int i = 30; i = 310; i += 20) {

g.drawLine(i, 30, i, 310);

}

for(int i = 10; i = 310; i += 20) {

g.drawLine(30, i, 310, i);

}

}

}

用java写一个国际象棋的棋盘,输出结果要是一张 国际象棋的棋盘

import java.awt.*;

import javax.swing.*;

public class Chessboard extends JApplet {

int baseXPosition, baseYPosition;

int currentXPosition, currentYPosition;

public void init() {

baseXPosition = 40; // 棋盘的开始x位置

baseYPosition = 40; // 棋盘的开始y位置

setBackground(Color.black); // 设置背景颜色黑色

}

public void paint(Graphics g) { // 绘制棋盘

currentXPosition = baseXPosition; // currentXPosition当前的x位置

currentYPosition = baseYPosition; // currentYPosition当前的y位置

for (int row = 0; row 8; row++) {

currentXPosition = baseXPosition + row * 40;

for (int column = 0; column 8; column++) {

if ((column + row) % 2 == 0)

g.setColor(Color.white); // 设置棋盘格子的颜色

else

g.setColor(Color.red); // 设置棋盘格子的颜色

currentYPosition = baseXPosition + column * 40;

g.drawRect(currentXPosition,currentYPosition,40,40);//;代码4 //在当前位置绘制棋盘的格子;每个格子的大小是40*40像

g.fillRect(currentXPosition,currentYPosition,40,40);

}

}

}

}

五子棋棋盘java实现

其实我也有用JAVA做五子棋呢~,棋盘都是用画的,我把代码发下,你自己试下,也不定合你一意.事件代码我都去啦,因为是简单的麻烦事.~!

import java.awt.*;

import javax.swing.*;

@SuppressWarnings("serial")

public class ChessBoard extends JPanel{

/*

* 制作棋盘的宽高;

*/

public static final int BOARD_WIDTH=515;

/*

* 计算棋盘表格坐标(单元格宽高相等)

*/

public static int [] location=new int[22];

static{

for(int i=0,WIDTH=30;ilocation.length;i++,WIDTH+=22){

location[i]=WIDTH;

}

}

public ChessBoard(int x,int y){

super(null);

this.setBounds(x, y, BOARD_WIDTH, BOARD_WIDTH);

this.setBackground(new Color(255, 164, 85));

}

/**

* 重写方法,绘制棋盘表格图;

*/

public void paintComponent(Graphics g){

super.paintComponent(g);

char ch='A';

g.setFont(new Font("宋体",Font.BOLD,12));

//画横线

for(int i=0,width=30+22*21;ilocation.length;i++,ch++){

g.setColor(Color.black);

g.drawLine(30,location[i],width,location[i]);

g.setColor(Color.blue);

g.drawString(""+ch,5,location[i]+3);

}

//画竖线

for(int i=0,width=30+22*21;ilocation.length;i++){

g.setColor(Color.black);

g.drawLine(location[i],30,location[i],width);

g.setColor(Color.blue);

g.drawString(""+(i+1),location[i]-3,13);

}

}

}

java棋盘例子的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java棋盘棋子走子、java棋盘例子的信息别忘了在本站进行查找喔。