java代码画棋盘 javafx画棋盘

求写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; i8; i) {
for (int j = 0; j8; 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.xj * blockWidth, rect.yi * blockHeight, blockWidth, blockHeight);
}
}
}
public static void main(String[] args) {
new App().setVisible(true);
}
}
运行结果:
java canvas画围棋棋盘/*首先,
应用程序必须为 Canvas 类创建子类,以获得有用的功能(如创建自定义组件) 。必须重写 paint 方法 , 以便在 canvas 上执行自定义图形 。
以下是代码
*/
import java.awt.Canvas;
import java.awt.Font;
import javax.swing.*;
public class P {
public static void main(String[] args){
JFrame jf=new JFrame();
jf.setVisible(true);
jf.setLocation(200,200);
jf.setSize(300, 300);
jf.add(new a());
}
}
class a extends Canvas{
public void paint(java.awt.Graphics g){
int j=0,i=0;
while(i6){//for循环也可以
g.drawLine(0, 20*i, 100, 20*i);//横线,y坐标不变
g.drawLine(20*i, 0, 20*i, 100);
i;
}
g.drawString("楚河汉界", 20, 130);
i=0;
while(i6){
g.drawLine(0,160 20*i, 100, 160 20*i);
g.drawLine(20*i, 160, 20*i, 260);
i;}
}
}
虽然画出来了,但我想简化两个循环,如下:
class a extends Canvas{
public void paint(java.awt.Graphics g){
int j=0,i=0;
do{//用for循环也可以
if(j==0){
g.drawLine(0, 20*i, 100, 20*i);//横线
g.drawLine(20*i, 0, 20*i, 100);
}
else{
g.drawLine(0,160 20*i, 100, 160 20*i);
g.drawLine(20*i, 160, 20*i, 260);
}
if(i==5j==0){j=1;i=-1;g.drawString("楚河汉界", 20, 130);}//因为判断第一次之后,i会被加一次,而这次是没有画线的,会导致少了一条线,所以i=-1
i;
}while(i6);
}
}
求代码:用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写一个国际象棋的棋盘,输出结果要是一张 国际象棋的棋盘import java.awt.*;
import javax.swing.*;
public class Chessboard extends JApplet {
int baseXPosition, baseYPosition;
int currentXPosition, currentYPosition;
public void init() {
baseXPosition = 40; // 棋盘java代码画棋盘的开始x位置
baseYPosition = 40; // 棋盘的开始y位置
【java代码画棋盘 javafx画棋盘】setBackground(Color.black); // 设置背景颜色黑色
}
public void paint(Graphics g) { // 绘制棋盘
currentXPosition = baseXPosition; // currentXPosition当前的x位置
currentYPosition = baseYPosition; // currentYPosition当前的y位置
for (int row = 0; row8; row) {
currentXPosition = baseXPositionrow * 40;
for (int column = 0; column8; column) {
if ((columnrow) % 2 == 0)
g.setColor(Color.white); // 设置棋盘格子的颜色
else
g.setColor(Color.red); // 设置棋盘格子的颜色
currentYPosition = baseXPositioncolumn * 40;
g.drawRect(currentXPosition,currentYPosition,40,40);//;代码4 //在当前位置绘制棋盘的格子java代码画棋盘;每个格子的大小是40*40像
g.fillRect(currentXPosition,currentYPosition,40,40);
}
}
}
}
java代码画棋盘的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于javafx画棋盘、java代码画棋盘的信息别忘了在本站进行查找喔 。

    推荐阅读