java绘制国际象棋与中国象棋棋盘
JAVA API 中的绘制图形类的paint()方法,我们可以轻松绘制中国象棋与国际象棋的棋盘。详见代码;
一、中国象棋棋盘代码
import java.awt.Font; import java.awt.Frame; import java.awt.Graphics; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class ChineseChese extends Frame{ public ChineseChese(){ setSize(630,630); this.setVisible(true); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); } public void paint(Graphics g){ Font f=new Font("微软雅黑",Font.BOLD,30); g.setFont(f); int length=60; g.drawRect(60, 50,500, 560); //外圈 g.drawRect(70, 60, 480, 540); /*横线部分*/ for(int i=0;i<9;i++){ g.drawLine(70, length, 550, length); length+=60; /*中间汉字*/ g.drawString("楚河", 130, 350); g.drawString("汉界",400 , 350); } length=130; /*上半部分竖线*/ for(int i=0;i<7;i++){ g.drawLine( length,60, length,300); length+=60; } /*下半部分竖线*/ length=130; for(int i=0;i<7;i++){ g.drawLine( length,360, length,600); length+=60; } /*上半部分九宫格斜线*/ g.drawLine(250, 60, 370, 180); g.drawLine(370, 60, 250, 180); /*下半部分九宫格斜线*/ g.drawLine(250, 480, 370, 600); g.drawLine(250, 600, 370, 480); } public static void main(String args[]){ new ChineseChese(); } }
二、国际象棋代码
import java.applet.Applet; import java.awt.Graphics; public class ChessBoard extends Applet{ public void init(){ setSize(400,400); this.setVisible(true); } public void paint(Graphics g){ g.drawRect(20, 10, 320, 320); g.drawLine(60, 10, 60, 330); //第一条竖线; g.drawLine(100,10,100,330); //第二条竖线; g.drawLine(140, 10, 140, 330); //第三条竖线; g.drawLine(180, 10, 180, 330); g.drawLine(220,10,220,330); g.drawLine(260, 10, 260, 330); g.drawLine(300,10,300,330); // g.drawLine(340,10,340,330); /*绘制横线*/ g.drawLine(20,50,340,50); //第一条横线; g.drawLine(20,90,340,90); g.drawLine(20,130,340,130); g.drawLine(20,170,340,170); g.drawLine(20,210,340,210); g.drawLine(20,250,340,250); g.drawLine(20,290,340,290); /*填充黑框*/ g.fillRect(20, 10,40, 40); g.fillRect(100, 10,40, 40); g.fillRect(180, 10,40, 40); g.fillRect(260, 10,40, 40); g.fillRect(60, 50,40, 40); g.fillRect(140, 50,40, 40); g.fillRect(220, 50,40, 40); g.fillRect(300, 50,40, 40); g.fillRect(20, 90,40, 40); g.fillRect(100, 90,40, 40); g.fillRect(180, 90,40, 40); g.fillRect(260, 90,40, 40); g.fillRect(60, 130,40, 40); g.fillRect(140, 130,40, 40); g.fillRect(220, 130,40, 40); g.fillRect(300, 130,40, 40); g.fillRect(20, 170,40, 40); g.fillRect(100, 170,40, 40); g.fillRect(180, 170,40, 40); g.fillRect(260, 170,40, 40); g.fillRect(60, 210,40, 40); g.fillRect(140, 210,40, 40); g.fillRect(220, 210,40, 40); g.fillRect(300, 210,40, 40); g.fillRect(20, 250,40, 40); g.fillRect(100, 250,40, 40); g.fillRect(180, 250,40, 40); g.fillRect(260, 250,40, 40); g.fillRect(60, 290,40, 40); g.fillRect(140, 290,40, 40); g.fillRect(220, 290,40, 40); g.fillRect(300, 290,40, 40); }
中国象棋棋盘效果图
国际象棋效果图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)