مقالان سابقان: Java تنفذ لعبة Gozi (اثنان) لرسم لوحة شطرنج ؛ Java تنفذ لعبة Gozi (ثلاثة) لرسم قطعة شطرنج ؛ Java تنفذ لعبة Gozi (أربعة) لتحقيق هذه الخطوة ؛ تقوم Java بتنفيذ لعبة Gozi (ستة) لتحويل جانب الشطرنج ، يمكنك النقر لعرضها.
الوظيفة التالية التي نريد تنفيذها هي مطالبة المعلومات على الشاشة عندما يكون هناك خمسة حبات متتالية. ما عليك سوى استخدام ShowMessagedialog في الجزء Iswin من وظيفة Mousepracked.
رمز وظيفة mousepracted هو كما يلي:
Override // public void mousepressed (mouseevent e) {int point_x = e.getx () ؛ int point_y = e.gety () ؛ int imgwidth = boardimg.getheight (هذا) ؛ int imgheight = boardimg.getWidth (هذا) ؛ int fwidth = getWidth () ؛ int fheight = getheight () ؛ int x = (fwidth-imgwidth)/2 ؛ int y = (fheight-imgheight)/2 ؛ int span_x = imgwidth/rows ؛ int span_y = imgheight/ows ؛ //system.out.println("press ") ؛ int status_x = 0 ؛ int status_y = 0 ؛ if (point_x> = x && point_x <= x+imgwidth && point_y> = y && point_y <= y+imgheight) {//system.out.println("legal ") ؛ لـ (int i = 0 ؛ i <الصفوف+1 ؛ i ++) {if (point_x> = x-chessman_width/2+1+i*span_x) {if (point_x <= x+chessman_width/2-1+i*i *x) // إذا كان ذلك العرض/2 ، فإن قيمتان مطابقتان ستظهر في الأوسط {///system. "+point_x+" "+(x-chessman_width/2+i*span_x)+" "+(x+chessman_width/2+i*span_x)) ؛ status_x = i ؛ }}} لـ (int i = 0 ؛ i <صفوف+1 ؛ i ++) {if (point_y> = y-chessman_width/2+1+i*span_y) {if (point_y <= y+chessman_width/2-1+i*span_y) "+(y-chessman_width/2+1+i*span_y)+" "+(y+chessman_width/2-1+i*span_y)) ؛ status_y = i ؛ }}} if (chessStatus [status_x] [status_y] == null || chessstatus [status_x] [status_y] .getplaced () == false) {Chessman Chessman = new Chessman (Chesscolor ، true) ؛ ChessStatus [status_x] [status_y] = Chessman ؛ System.out.println ("Color Chess:"+ChessColor) ؛ if (ChessColor == Black) {ChessColor = White ؛ } آخر {ChessColor = Black ؛ } repaint () ؛ // إذا الفوز ، أعط رسالة سريعة إذا (iswin (status_x ، status_y ، chessstatus)) {system.out.println ("win !!!") ؛ الفائز بالسلسلة // إذا كان الطفل التالي أبيضًا ، فهذه المرة يكون أسود إذا كان (ChessColor == White) Winner = "Black" ؛ الفائز آخر = "فارغ" ؛ سلسلة mstring = string.format ("تهانينا ، ٪ s win !!!" ، الفائز) ؛ joptionpane.showmessagedialog (هذا ، mstring) ؛ }}}}قم بتشغيله:
في هذه المرحلة ، أكملنا وظيفة Gozi الأساسية. سأقوم بنشر الرمز الكامل أدناه:
تشيسمان
حزمة Xchen.test.simplegobang ؛ الطبقة العامة Chessman {private int color ؛ // 1-white ، 0 boolean private placed = false ؛ int matchcount = 1 ؛ 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 ؛ }} 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 (صحيح) ؛ }} DrawChessboard.java
حزمة Xchen.test.simplegobang ؛ استيراد java.awt.color ؛ استيراد java.awt.graphics ؛ استيراد java.awt.graphics2d ؛ استيراد java.awt.image ؛ استيراد java.awt.radialgradientpaint ؛ استيراد java.awt.toolkit ؛ استيراد java.awt.event.mouseevent ؛ استيراد java.awt.event.mouselistener ؛ استيراد javax.swing.joptionpane ؛ استيراد javax.swing.jpanel ؛ يمتد DrawChessboard من الطبقة العامة jpanel الأدوات mouselistener {Final Static int Black = 0 ؛ الثابت النهائي int الأبيض = 1 ؛ شطرنج int العام = أسود ؛ int chessman_width = 30 ؛ الصورة العامة poardimg ؛ صفوف int الخاصة النهائية = 19 ؛ Chessman [] [] chessstatus = new Chessman [الصفوف+1] [صفوف+1] ؛ Public DrawChessboard () {boardimg = toolkit.getDefaultToolkit (). getImage ("res/drawable/chessboard2.png") ؛ if (boardimg == null) system.err.println ("png غير موجود") ؛ addmouselistener (هذا) ؛ } override paintcomponent paintcomponent المحمية (الرسومات G) {// todo method method method super.paintcomponent (g) ؛ int imgwidth = boardimg.getheight (هذا) ؛ int imgheight = boardimg.getWidth (هذا) ؛ int fwidth = getWidth () ؛ int fheight = getheight () ؛ int x = (fwidth-imgwidth)/2 ؛ int y = (fheight-imgheight)/2 ؛ int span_x = imgwidth/rows ؛ int span_y = imgheight/ows ؛ G.DrawImage (boardimg ، x ، y ، null) ؛ // ارسم الخط الأفقي لـ (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 <rows+1 ؛ i ++) {for (int j = 0 ؛ j <rows+1 ؛ j ++) {if (chessstatus [i] [j]! = null && chessstatus [i] [j]. int pos_x = x+i*span_x ؛ int pos_y = y+j*span_y ؛ float radius_b = 40 ؛ float radius_w = 80 ؛ 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). }}}} Override // public void mousepressed (mouseevent e) {int point_x = e.getx () ؛ int point_y = e.gety () ؛ int imgwidth = boardimg.getheight (هذا) ؛ int imgheight = boardimg.getWidth (هذا) ؛ int fwidth = getWidth () ؛ int fheight = getheight () ؛ int x = (fwidth-imgwidth)/2 ؛ int y = (fheight-imgheight)/2 ؛ int span_x = imgwidth/rows ؛ int span_y = imgheight/ows ؛ //system.out.println("press ") ؛ int status_x = 0 ؛ int status_y = 0 ؛ if (point_x> = x && point_x <= x+imgwidth && point_y> = y && point_y <= y+imgheight) {//system.out.println("legal ") ؛ لـ (int i = 0 ؛ i <الصفوف+1 ؛ i ++) {if (point_x> = x-chessman_width/2+1+i*span_x) {if (point_x <= x+chessman_width/2-1+i*i *x) // إذا كان ذلك العرض/2 ، فإن قيمتان مطابقتان ستظهر في الأوسط {///system. "+point_x+" "+(x-chessman_width/2+i*span_x)+" "+(x+chessman_width/2+i*span_x)) ؛ status_x = i ؛ }} لـ (int i = 0 ؛ i <الصفوف+1 ؛ i ++) {if (point_y> = y-chessman_width/2+1+i*span_y) {if (point_y <= y+chessman_width/2-1+i*span_y) {//system.out.println h "+i+" "+(y-chessman_width/2+1+i*span_y)+" "+(y+chessman_width/2-1+i*span_y)) ؛ status_y = i ؛ }}} if (chessStatus [status_x] [status_y] == null || chessstatus [status_x] [status_y] .getplaced () == false) {Chessman Chessman = new Chessman (Chesscolor ، true) ؛ ChessStatus [status_x] [status_y] = Chessman ؛ System.out.println ("Color Chess:"+ChessColor) ؛ if (ChessColor == Black) {ChessColor = White ؛ } آخر {ChessColor = Black ؛ } repaint () ؛ // إذا فزت ، فأعطي رسالة سريعة إذا (iswin (status_x ، status_y ، chessstatus)) {system.out.println ("win !!!!") ؛ الفائز بالسلسلة // إذا كان الطفل التالي أبيضًا ، فهذه المرة يكون أسود إذا كان (ChessColor == White) Winner = "Black" ؛ آخر الفائز = "أبيض" ؛ سلسلة mstring = string.format ("تهانينا ، ٪ s win !!!" ، الفائز) ؛ joptionpane.showmessagedialog (هذا ، mstring) ؛ }}}} Override // public void mouseclicked (mouseevent e) {// todo method method method tuto} override public mousereled mousereled (mouseevent e) {// todo method method method adulated mother extride @// toder todo method method} @override public mousereledes (mouseevent E) void mouseentered (mouseevent e) {// todo method method mouseexited (mouseevent e) {// tod j = 0 ؛ j <rows+1 ؛ j ++) {// search long-side if (chessstatus [i] [j]! = null && chessstatus [i] // ابحث عن (int n = 1 ؛ n <= 4 ؛ n ++) {if ((i+n> = 0) && (i+n) <= rows) {if (chessstatus [i+n] [j]! = null && chessstatus [i+n]. {chessstatus [i] [j] .MatchCount ++ ؛ System.out.println ("pos:"+i+""+j+"count right ++:"+(i+n)+""+j+"count:"+chessstatus [i] [j] .MatchCount) ؛ if (chessstatus [i] [j] .MatchCount == 5) {return true ؛ }} آخر {break ؛ }}} // ابحث عن (int n = 1 ؛ n <= 4 ؛ n ++) {if ((in> = 0) && (in) <= rows) {if (chessstatus [in] [j]! = null && chessstatus [in] chessstatus [i] [j] .MatchCount ++ ؛ System.out.println ("pos:"+i+""+j+""+"left count ++:"+(in)+""+j+"count:"+chessstatus [i] [j] .MatchCount) ؛ if (chessstatus [i] [j] .MatchCount == 5) {return true ؛ }} else {if (chessstatus [in] [j]! = null) {chessstatus [i] [j] .MatchCount = 1 ؛ } استراحة؛ }} ستر if (chessstatus [i] [j]! = null && chessstatus [i] [J]. // انظر لأسفل ، الزاوية اليسرى العليا هي الأصل الإحداثي ، والاتجاه الموجب للمحور y ينخفض لـ (int n = 1 ؛ n <= 4 ؛ n ++) {if ((j+n> = 0) && (j+n) <= rows) { if (chessstatus [i] [j+n]! = null && chessstatus [i] [j+n] .getPlaced () == true && chessstatus [i] [j+n] .getColor () == matchcolor) {chessstatus [i] .matchcount ++ ؛ System.out.println ("pos:"+i+""+j+"up count ++:"+(i)+""+(j+n)+"count:"+chessstatus [i] [j] .MatchCount) ؛ if (chessstatus [i] [j] .MatchCount == 5) {return true ؛ }} آخر {break ؛ }}} // ابحث عن (int n = 1 ؛ n <= 4 ؛ n ++) {if ((jn> = 0) && (jn) <= rows) { if (chessstatus [i] [jn]! = null && chessstatus [i] System.out.println ("pos:"+i+""+j+""+"left count ++:"+(i)+""+(jn)+"count:"+chessstatus [i] [j] .MatchCount) ؛ if (chessstatus [i] [j] .MatchCount == 5) {return true ؛ }} آخر {if (chessstatus [i] [jn]! = null) {chessstatus [i] [j] .matchCount = 1 ؛ } استراحة؛ } ستر if (chessstatus [i] [j]! = null && chessstatus [i] [J]. // انظر لأسفل ، الزاوية اليسرى العليا هي الأصل الإحداثي ، والاتجاه الإيجابي للمحور y ينخفض لـ (int n = 1 ؛ n <= 4 ؛ n ++) {if ((jn> = 0) && (jn) <= rows && (in)> = 0 && (in) <= rows) { if (chessstatus [in] [jn]! = null && chessstatus [in] [jn] .getPlaced () == true && chessstatus [in] System.out.println ("pos:"+i+""+j+"up count ++:"+(in)+""+(jn)+"count:"+chessstatus [i] [j] .MatchCount) ؛ if (chessstatus [i] [j] .MatchCount == 5) {return true ؛ }} آخر {break ؛ }}} // أسفل اليمين لـ (int n = 1 ؛ n <= 4 ؛ n ++) {if ((j+n> = 0) && (j+n) <= rows && ( if (chessstatus [i+n] [j+n]! = null && chessstatus [i+n] [j+n] .getplaced () == true && chessstatus [i+n] [j+n] .getColor () == matchcolor) {chessstatus [i] System.out.println ("pos:"+i+""+j+""+"count ++:"+(i+n)+""+(j+n)+"count:"+chessstatus [i] [j] .MatchCount) ؛ if (chessstatus [i] [j] .MatchCount == 5) {return true ؛ }} آخر {if (chessstatus [i+n] [j+n]! = null) {chessstatus [i] [j] .MatchCount = 1 ؛ } استراحة؛ } ستر if (chessstatus [i] [j]! = null && chessstatus [i] [J]. // انظر لأسفل ، الزاوية اليسرى العليا هي الأصل الإحداثي ، والاتجاه الإيجابي للمحور y ينخفض لـ (int n = 1 ؛ n <= 4 ؛ n ++) {if ((j+n> = 0) && (j+n) <= rows && (in)> = 0 && (in) <= rows) { if (chessstatus [in] [j+n]! = null && chessstatus [in] [j+n] .getplaced () == true && chessstatus [in] [j+n] .getColor () == matchcolor) {chessstatus [i] [j] .matchcount ++ ؛ System.out.println ("pos:"+i+""+j+"up count ++:"+(in)+""+(j+n)+"count:"+chessstatus [i] [j] .MatchCount ++) ؛ if (chessstatus [i] [j] .MatchCount == 5) {return true ؛ }} آخر {break ؛ } ستر if (chessstatus [i+n] [jn]! = null && chessstatus [i+n] System.out.println ("pos:"+i+""+j+""+"left count ++:"+(i+n)+""+(jn)+"count:"+chessstatus [i] [j] .MatchCount) ؛ if (chessstatus [i] [j] .MatchCount == 5) {return true ؛ }} آخر {if (chessstatus [i+n] [jn]! = null) {chessstatus [i] [j] .MatchCount = 1 ؛ } استراحة؛ }}} chessstatus [i] [j] .MatchCount = 1 ؛ // refresh count}}} return false ؛ }}لمزيد من الألعاب إثارة ، يرجى الرجوع إلى الموضوع الخاص "ألعاب Java Classic"
ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.