中国象棋棋盘java_如何用java swing 实现中国象棋的棋盘。棋盘就好。。。

展开全部

package com.fzhw.app;

import java.awt.BorderLayout;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.HeadlessException;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class Xiangqi extends JFrame{

private static final long serialVersionUID = 1;

public Xiangqi() throws HeadlessException {

636f707962616964757a686964616f31333335326135super();

setTitle("my board");

this.getContentPane().setLayout(new BorderLayout());

this.getContentPane().add(new Board(), BorderLayout.CENTER);

this.setSize(960, 960);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String args[]){

new Xiangqi().setVisible(true);

}

}

class Board extends JPanel{

private static final long serialVersionUID = 1L;

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2d=(Graphics2D)g;

int width=this.getWidth();

int height=this.getHeight();

int hGap=height/9;

int wGap=width/8;

for(int i=0;i<10;i++){

g2d.drawLine(0, i*hGap, width, i*hGap);

}

for(int i=0;i<9;i++){

g2d.drawLine(i*wGap, 0, i*wGap, 4*hGap);

g2d.drawLine(i*wGap, 5*hGap, i*wGap, height);

}

g2d.drawString("楚河         汉界", width/2, height/2);

g2d.drawLine(3*wGap, 0, 5*wGap, 2*hGap);

g2d.drawLine(5*wGap, 0, 3*wGap, 2*hGap);

g2d.drawLine(3*wGap, height, 5*wGap, height-2*hGap);

g2d.drawLine(3*wGap, height-2*hGap, 5*wGap, height);

g2d.dispose();

}

}

中国象棋棋盘java_如何用java swing 实现中国象棋的棋盘。棋盘就好。。。_第1张图片

两种选择,第一,你用一个背景图片来充当欺骗,落子位置的在panel上转化,楚河汉界不用管,两边是对称的。

第二种,用Graphics2D自己draw line。

我给你写好了,但是10分太少,不想给啊

你可能感兴趣的:(中国象棋棋盘java)