如何用javaswing实现中国象棋的棋盘棋盘就好

packagecom.fzhw.app;

importjava.awt.BorderLayout;

importjava.awt.Graphics;

importjava.awt.Graphics2D;

importjava.awt.HeadlessException;

importjavax.swing.JFrame;

importjavax.swing.JPanel;

publicclassXiangqiextendsJFrame{

privatestaticfinallongserialVersionUID=1;

publicXiangqi()throwsHeadlessException{

super();

setTitle(myboard);

this.getContentPane().setLayout(newBorderLayout());

this.getContentPane().add(newBoard(),BorderLayout.CENTER);

this.setSize(960,960);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

publicstaticvoidmain(Stringargs[]){

newXiangqi().setVisible(true);

}

}

classBoardextendsJPanel{

privatestaticfinallongserialVersionUID=1L;

protectedvoidpaintComponent(Graphicsg){

super.paintComponent(g);

Graphics2Dg2d=(Graphics2D)g;

intwidth=this.getWidth();

intheight=this.getHeight();

inthGap=height/9;

intwGap=width/8;

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

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

}

for(inti=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();

}

}

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

第二种用Graphics2D自己drawline。

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

免责声明:本站发布的游戏攻略(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场。
如果本文侵犯了您的权益,请联系站长邮箱进行举报反馈,一经查实,我们将在第一时间处理,感谢您对本站的关注!