يتحدث المقال السابق عن تنفيذ Java لتنفيذ لعبة Gozi Chess (2) التي ترسم الشطرنج ، وتم رسم لوحة الشطرنج. بعد ذلك ، نحتاج إلى تنفيذ وظيفة التحكم ، الوظائف الرئيسية:
1) اختر قطعة شطرنج
2) ارسم قطع الشطرنج
3) الحكم على الفائز
4) تبادل الشطرنج
أدرك جزءًا من قطعة الشطرنج أولاً
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
أولا ، حدد فئة قطعة الشطرنج. تحتوي هذه الفئة على سمتين: لون القطعة (0- يعني الأسود و 1- أبيض) ، وما إذا كنت أقوم بخطوة (أخطط لاستخدام صفيف ثنائي الأبعاد لتخزين معلومات دور الشطرنج)
تشيسمان
حزمة Xchen.test.simplegobang ؛ الطبقة العامة Chessman {private int color ؛ // 1-white ، 0 boolean private placed = false ؛ Public Chessman (int color ، boolean placed) {this.color = color ؛ this.placed = وضعت ؛ } boolean العامة getplaced () {return placed ؛ } public void setplaced (boolean placed) {this.placed = placed ؛ } public int getColor () {return color ؛ } public void setColor (int color) {this.color = color ؛ }} ثم رسمنا جزء الكود من لوح الشطرنج في الجزء السابق وأضفنا الرمز لرسم قطع الشطرنج. لقد استخدمت قطعتين من الشطرنج (أبيض وواحد أسود ، يقع في الشطرنج [8 ، 8] ، [7 ، 7]) لاختبار الرمز لرسم قطع الشطرنج.
DrawChessboard.java
حزمة Xchen.test.simplegobang ؛ استيراد java.awt.graphics ؛ استيراد java.awt.graphics2d ؛ استيراد java.awt.radialgradientpaint ؛ استيراد java.awt.image ؛ استيراد java.awt.toolkit ؛ استيراد java.awt.color ؛ استيراد javax.swing.jpanel ؛ يمتد DrawChessboard للطبقة العامة jpanel {Final Static Int Black = 0 ؛ الثابت النهائي int الأبيض = 1 ؛ شطرنج int العام = أسود ؛ الصورة العامة poardimg ؛ صفوف int الخاصة النهائية = 19 ؛ Chessman [] [] chessstatus = New Chessman [صفوف] [صفوف] ؛ Public DrawChessboard () {boardimg = toolkit.getDefaultToolkit (). getImage ("res/drawable/chessboard2.png") ؛ if (boardimg == null) system.err.println ("png غير موجود") ؛ // اختبار رسم Chessman جزء Simple Chessman Chessman = New Chessman (0 ، true) ؛ Chessstatus [7] [7] = Chessman ؛ Chessman Chessman2 = New Chessman (1 ، true) ؛ Chessstatus [8] [8] = Chessman2 ؛ . int imgwidth = boardimg.getheight (هذا) ؛ int imgheight = boardimg.getWidth (هذا) ؛ int fwidth = getWidth () ؛ int fheight = getheight () ؛ int x = (fwidth-imgwidth)/2 ؛ int y = (fheight-imgheight)/2 ؛ G.DrawImage (boardimg ، x ، y ، null) ؛ الهامش int = x ؛ int span_x = imgwidth/rows ؛ int span_y = imgheight/ows ؛ // ارسم الخط الأفقي لـ (int i = 0 ؛ i <rows ؛ i ++) {g.drawline (x ، y+i*span_y ، fwidth-x ، y+i*span_y) ؛ } // ارسم الخط العمودي لـ (int i = 0 ؛ i <rows ؛ i ++) {g.drawline (x+i*span_x ، y ، x+i*span_x ، fheight-y) ؛ } // ارسم قطع الشطرنج لـ (int i = 0 ؛ i <الصفوف ؛ i ++) {for (int j = 0 ؛ j <rows ؛ j ++) {if (chessstatus [i] [j]! = null && chessstatus [i]. int pos_x = x+i*span_x ؛ int pos_y = y+j*span_y ؛ int chessman_width = 20 ؛ float radius_b = 20 ؛ float radius_w = 50 ؛ float [] fractions = new float [] {0f ، 1f} ؛ java.awt.color [] colors_b = new java.awt.color [] {color.black ، color.white} ؛ color [] color_w = new color [] {color.hite ، color.black} ؛ طلاء RadialGradientPaint ؛ if (chessstatus [i] [j] .getColor () == 1) {system.out.println ("Draw White Chess") ؛ Paint = New RadialGradientPaint (pos_x-thessman_width/2f ، pos_y-chessman_width/2f ، radius_w*2 ، fractions ، colors_w) ؛ } آخر {system.out.println ("Draw Black Chess") ؛ Paint = New RadialGradientPaint (pos_x-thessman_width/2f ، pos_y-chessman_width/2f ، radius_b*2 ، fractions ، colors_b) ؛ } ((graphics2d) g) .setpaint (paint) ؛ ((Graphics2d) g). }}}}}}}} يبقى رمز الوحدة الرئيسية دون تغيير
main.java
حزمة Xchen.test.simplegobang ؛ استيراد java.awt.container ؛ استيراد javax.swing.jframe ؛ استيراد xchen.test.simplegobang.drawChessboard ؛ يمتد الفئة العامة الرئيسية JFRAME {private DrawChessboard DrawChessboard ؛ Public Main () {DrawChessboard = new DrawChessboard () ؛ // عنوان الإطار Settitle ("Goji المستقل") ؛ حاوية حاوية = getContentPane () ؛ ContainerPane.Add (DrawChessboard) ؛ } public static void main (string [] args) {main m = new main () ؛ M.SetSize (800 ، 800) ؛ M.SetVisible (صحيح) ؛ }}قم بتشغيله!
ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.