用java怎么编写象棋棋盘
答案:2 悬赏:20 手机版
解决时间 2021-03-23 01:55
- 提问者网友:蓝莓格格巫
- 2021-03-22 01:50
用java怎么编写象棋棋盘
最佳答案
- 五星知识达人网友:渡鹤影
- 2021-03-22 03:09
这个属于java.swing里面的知识,只要学了,在加一点点算法就可以弄出来。
全部回答
- 1楼网友:不如潦草
- 2021-03-22 04:29
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 {
super();
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();
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯