تصف هذه المقالة رمز لعبة Gozi Chess الذي تم تنفيذه في Java. تتم مشاركتها معك للرجوع إليها. الرمز المحدد كما يلي
1. الأهداف العملية
1. إتقان تصميم واجهة Javagui
2. إتقان مراقبة أحداث الماوس (Mouselistener ، MousemotionListener)
2. المحتوى العملي
تصميم برنامج Gochi بسيط يمكنه تحقيق عملية لعبة Gochi Chess. كما هو مبين في الشكل أدناه
1. لوحة الشطرنج Gozi
حزمة cn.edu.ouc.fivechess ؛ استيراد java.awt.color ؛ استيراد java.awt.cursor ؛ استيراد java.awt.dimension ؛ استيراد java.awt.graphics ؛ استيراد java.awt.graphics2d ؛ استيراد java.awt.image ؛ استيراد java.awt.radialgradientpaint ؛ استيراد java.awt.renderinghints ؛ استيراد java.awt.toolkit ؛ استيراد java.awt.event.mouseevent ؛ استيراد java.awt.event.mouselistener ؛ استيراد java.awt.event.mousemotionListener ؛ استيراد java.awt.geom.ellipse2d ؛ استيراد javax.swing.*؛ ! النقطة [(الصفوف+1)*(cols+1)] ؛ // الأولي كل عنصر صفيف هو null boolean isblack = true ؛ // يبدأ الافتراضي مع لعبة الشطرنج الأولى boolean gameover = false ؛ ظلال الصورة اللون colortemp Public Chessboard () {// setbackground (color.blue) ؛ // اضبط لون الخلفية على Orange img = toolkit.getDefaultToolkit (). getImage ("board.jpg") ؛ Shadows = toolkit.getDefaultToolkit (). getImage ("Shadows.jpg") ؛ addmouselistener (هذا) ؛ AddMouseMotionListener (new mousemotionListener () {public void mousedragged (mouseevent e) {} public void mousemoved (mouseevent e) {int x1 = ( y1 = ( SetCursor (New Cursor (Cursor.default_cursor) ؛ }. int imgheight = img.getheight (هذا) ؛ // احصل على عرض وارتفاع الصورة int fwidth = getWidth () ؛ int fheight = getheight () ؛ // احصل على عرض وارتفاع النافذة int x = (fwidth-imgwidth)/2 ؛ int y = (fheight-imgheight)/2 ؛ G.DrawImage (IMG ، X ، Y ، Null) ؛ لـ (int i = 0 ؛ i <= rows ؛ i ++) {// ارسم الخط الأفقي g.drawline (الهامش ، الهامش+i*grid_span ، margin+cols*grid_span ، margin+i*grid_span) ؛ } لـ (int i = 0 ؛ i <= cols ؛ i ++) {// ارسم الخط العمودي g.drawline (margin+i*grid_span ، margin ، margin+i*grid_span) ؛ } // ارسم قطع الشطرنج لـ (int i = 0 ؛ i <chesscount ؛ i ++) {// Grid Intersection x ، y costaLinate int xpos = chesslist [i] .getx ()*grid_span+margin ؛ int ypos = chesslist [i] .gety ()*grid_span+margin ؛ G.SetColor (ChessList [i] .getColor ()) ؛ // set color // g.filloval (xpos-point.diameter/2 ، ypos-point.diameter/2 ، //point.diameter ، point.diameter) ؛ //g.drawimage(shadows ، xpos-point.diameter/2 ، ypos-point.diameter/2 ، point.diameter ، point.diameter ، null) ؛ colortemp = chesslist [i] .getColor () ؛ if (colortemp == color.black) {radialgradientpaint paint = new RadialGradientPaint (xpos-point.diameter/2+25 ، ypos-point.diameter/2+10 ، 20 ، new float [] {0f ، 1f} ، color [] {color.white ، color.black}) ؛ ((graphics2d) g) .SetPaint (Paint) ؛ ((graphics2d) g) .SetRenderingHint (renderinghints.key_antialiasing ، renderinghints.value_antialias_on) ؛ ((graphics2d) g) .SetRenderingHint (renderingHints.Key_alpha_interpolation ، renderinghints.value_alpha_interpolation_default) ؛ } آخر إذا (colortemp == color.white) {radialgradientpaint paint = new RadialGradientPaint (xpos-point.diameter/2+25 ، ypos-point.diameter/2+10 ، 70 ، new float [] {0f ، 1f} ، لون جديد [] {color.white ، color.black}) ؛ ((graphics2d) g) .SetPaint (Paint) ؛ ((graphics2d) g) .SetRenderingHint (renderinghints.key_antialiasing ، renderinghints.value_antialias_on) ؛ ((graphics2d) g) .SetRenderingHint (renderingHints.Key_alpha_interpolation ، renderinghints.value_alpha_interpolation_default) ؛ } elipse2d e = new elipse2d.float (xpos-point.diameter/2 ، ypos-point.diameter/2 ، 34 ، 35) ؛ ((graphics2d) g) .fill (e) ؛ . G.DrawRect (xpos-point.diameter/2 ، ypos-point.diameter/2 ، 34 ، 35) ؛ }}} public void mousepracked (mouseevent e) {// يتم استدعاء الماوس عند الضغط على المكون // لم يعد بإمكان اللعبة اللعب إذا كانت (gameover) تعود ؛ سلسلة colorname = isBlack؟ "أسود": "الشطرنج الأبيض" ؛ // قم بتحويل موضع الإحداثيات من الماوس انقر فوق فهرس الشبكة Xindex = ( yindex = ( // إذا سقطت خارج لوحة الشطرنج ، فلن تتمكن من وضعها إذا (Xindex <0 || xindex> صفوف || yindex <0 || yindex> cols) return ؛ // إذا كان هناك قطعة شطرنج في موضع X و Y ، فلا يمكنك وضعها إذا كانت (FindChess (Xindex ، Yindex)) ؛ // إذا كان بإمكانك القيام بذلك عندما يتم ذلك ، فلا يمكنك وضعه عند الانتهاء. Point (Xindex ، Yindex ، isBlack؟ color.black: color.hite) ؛ chesslist [ChessCount ++] = ch ؛ REPAINT () ؛ // إخطار النظام بإعادة السحب // إذا فزت ، أعط رسالة سريعة ولا يمكنها الاستمرار في لعب الشطرنج إذا (ISWIN ()) joptionpane.showmessagedialog (هذا ، msg) ؛ GameOver = صحيح ؛ } isBlack =! isBlack ؛ }. mousereleSeped (mouseevent e) {// استدعاء عندما يتم إصدار زر الماوس على المكون} // المسمى في صفيف الشطرنج لمعرفة ما إذا كانت هناك قطع شطرنج مع index x و y الموجودة في Boolean BindChess (int x ، int y) {for (point c: chesslist) {if (c! } إرجاع خطأ ؛ } boolean private iswin () {int continuecount = 1 ؛ // عدد قطع الشطرنج المستمرة // تبدو أفقيًا غربًا لـ (int x = xindex-1 ؛ x> = 0 ؛ x-) {color c = isBlack؟ color.black: color.white ؛ if (getChess (x ، yindex ، c)! = null) {continuecount ++ ؛ } استراحة أخرى ؛ } // انظر أفقيًا شرقًا لـ (int x = xindex+1 ؛ x <= cols ؛ x ++) {color c = isBlack؟ color.black: color.white ؛ if (getChess (x ، yindex ، c)! = null) {continuecount ++ ؛ } استراحة أخرى ؛ } if (continuecount> = 5) {return true ؛ } آخر continuecount = 1 ؛ // تابع بحثًا آخر رأسيًا // ابحث عن أعلى (int y = yindex-1 ؛ y> = 0 ؛ y-) {color c = isBlack؟ color.black: color.hite ؛ if (getChess (xindex ، y ، c)! = null) {continuecount ++ ؛ } استراحة أخرى ؛ } // انظر إلى الأسفل لـ (int y = yindex+1 ؛ y <= الصفوف ؛ y ++) {color c = isBlack؟ color.black: color.white ؛ if (getChess (xindex ، y ، c)! = null) continuecount ++ ؛ استراحة أخرى } if (continuecount> = 5) return true ؛ آخر مستمر = 1 ؛ // متابعة البحث عن موقف آخر: بشكل غير مباشر // شمال شرق البحث عن (int x = xindex+1 ، y = yindex-1 ؛ y> = 0 && x <= cols ؛ x ++ ، y-) {color c = isBlack؟ color.black: color.white ؛ if (getChess (x ، y ، c)! = null) {continuecount ++ ؛ } استراحة أخرى ؛ }. if (getChess (x ، y ، c)! = null) {continuecount ++ ؛ } استراحة أخرى ؛ } if (continuecount> = 5) return true ؛ آخر مستمر = 1 ؛ // متابعة البحث في موقف آخر: مائل // شمال غرب البحث عن (int x = xindex-1 ، y = yindex-1 ؛ x> = 0 && إذا (getChess (x ، y ، c)! = null) continuecount ++ ؛ استراحة أخرى }. إذا (getChess (x ، y ، c)! = null) continuecount ++ ؛ استراحة أخرى } if (continuecount> = 5) return true ؛ آخر مستمر = 1 ؛ العودة كاذبة } نقطة خاصة getChess (int xindex ، int yindex ، لون اللون) {for (point p: chesslist) {if (p! = null && p.getx () == xindex && p.gety () == yindex && p.getcolor () == color) return p ؛ } إرجاع فارغ ؛ } public void retartGame () {// clear chess actions for (int i = 0 ؛ i <chesslist.length ؛ i ++) {chesslist [i] = null ؛ } // استعادة القيمة المتغيرة المتعلقة باللعبة ISBlack = true ؛ GameOver = false ؛ // هي اللعبة التي تنتهي ChessCount = 0 ؛ // العدد الحالي من ألواح الشطرنج والقطع إعادة الطلاء () ؛ } // الرد على الشطرنج public void goBack () {if (chesscount == 0) return ؛ chesslist [chesscount-1] = null ؛ Chesscount-- ؛ if (chesscount> 0) {xindex = ChessList [ChessCount-1] .getx () ؛ yindex = chesslist [chesscount-1] .gety () ؛ } isBlack =! isBlack ؛ REPAINT () ؛ }. }} 2. قطع الشطرنج
حزمة cn.edu.ouc.fivechess ؛ استيراد java.awt.color ؛ /*** فئة الشطرنج*/Public Class Point {private int x ؛ // x Index in the Chess Board private int y ؛ // y Index in the Chess Board Private Color ؛ // Color Public Static Final Diameter = 30 ؛ // Diameter Public Point (int x ، int y ، color) {this.x = x ؛ this.y = y ؛ this.color = اللون ؛ } public int getx () {// احصل على فهرس x في لوحة الشطرنج x ؛ } public int gety () {return y ؛ } color getColor () {// احصل على لون لون الشطرنج ؛ }} 3. فئة الإطار الرئيسي Gozi
حزمة cn.edu.ouc.fivechess ؛ استيراد java.awt.event.*؛ استيراد java.awt.*؛ استيراد javax.swing.*؛ /* Goji Main Framework ، البرنامج*/ الفئة العامة StartChessJframe يمتد JFrame {private Chessboard Chessboard ؛ شريط أدوات JPANEL الخاص ؛ Private Jbutton Startbutton ، Backbutton ، Exitbutton ؛ الخاص Jmenubar Menubar ؛ الخاص Jmenu Sysmenu ؛ الخاص jmenuitem startmenuitem ، exitmenuitem ، backmenuitem ؛ // إعادة تشغيل ، خروج ، عنصر قائمة الأسف العام startChessJframe () {Settitle ("goji المستقلة") ؛ // قم بتعيين العنوان chessboard = new Chessboard () ؛ Container contentPane = getContentPane () ؛ contentPane.add (الشطرنج) ؛ chessboard.setopaque (صواب) ؛ // إنشاء وإضافة قائمة menubar = new jmenubar () ؛ // تهيئة شريط القائمة sysmenu = new jmenu ("النظام") ؛ // تهيئة القائمة // تهيئة عنصر القائمة startMenuitem = New JMenuitem ("RESTART") ؛ exitmenuitem = new jmenuitem ("exit") ؛ backmenuitem = new jmenuitem ("التوبة") ؛ // إضافة ثلاثة عناصر القائمة إلى قائمة sysmenu.add (starmenuitem) ؛ sysmenu.add (exitmenuitem) ؛ sysmenu.add (backmenuitem) ؛ // تهيئة الفئة الداخلية myitemlistener lis = new myitemlistener () ؛ . backmenuitem.addActionListener (LIS) ؛ exitmenuitem.addActionListener (LIS) ؛ menubar.add (sysmenu) ؛ // إضافة قائمة النظام إلى شريط القائمة setjmenubar (menubar) ؛ // تعيين menubar إلى menubar شريط أدوات = new jpanel () ؛ // instantiate لوحة الأدوات // ثلاثة أزرار تهيئة startbutton = new jbutton ("ابدأ") ؛ exitbutton = new jbutton ("exit") ؛ backbutton = new jbutton ("التوبة") ؛ // تخطيط أزرار لوحة الأدوات مع flowlayout toolbar.setLayout (New FlowLayout (FlowLayout.left)) ؛ // أضف ثلاثة أزرار إلى شريط أدوات لوحة الأدوات (startbutton) ؛ Toolbar.Add (Exitbutton) ؛ شريط الأدوات (backbutton) ؛ // قم بتسجيل الأزرار الثلاثة للاستماع إلى الحدث startButton.addActionListener (LIS) ؛ exitbutton.addActionListener (LIS) ؛ backbutton.addActionListener (LIS) ؛ backbutton.addActionListener (LIS) ؛ // ضع لوحة الأدوات في الواجهة "الجنوب" ، أي إضافة (شريط الأدوات ، borderlayout.south) ؛ إضافة (chessboard) ؛ // إضافة كائن لوحة إلى النموذج // قم بتعيين واجهة إغلاق الحدث setDefaultCloseOperation (jframe.exit_on_close) ؛ // setSize (800،800) ؛ pack () ؛ // almative size} private myiteMlistener تنفذ ActionListener {public void actionperformed (actionevent e) {object obj = system.out.println ("start") ؛ chessboard.restartgame () ؛ } آخر إذا (obj == exitmenuitem || obj == exitbutton) system.exit (0) ؛ آخر if (obj == backmenuitem || obj == backbutton) {system.out.println ("التوبة الشطرنج ...") ؛ chessboard.goback () ؛ }}} static void main (string [] args) {StartChessJframe f = new StartChessJframe () ؛ // إنشاء الإطار الرئيسي f.setVisible (صحيح) ؛ // إظهار الإطار الرئيسي}} 3. ملخص
1. تصميم وتنفيذ القوائم؟
2. كيف ترسم قطع الشطرنج بعد النقر على الماوس على لوحة الشطرنج؟ كيف ترسم صندوقًا أحمر لقطعة الشطرنج التي قمت بإعدادها للتو؟
3. ما هي بنية البيانات لعشرات الشطرنج؟
ما سبق هو كل شيء عن هذا المقال ، آمل أن يكون من المفيد للجميع تعلم برمجة Java.