مقالان سابقان: Java تنفذ لعبة Gozi Chess Game (اثنان) ترسم لوحة شطرنج ؛ Java تنفذ لعبة Gozi Chess Game (اثنان) ترسم قطعة شطرنج
أمامنا ، لقد رسمنا ألواح الشطرنج وقطع الشطرنج. بعد ذلك ، نحتاج إلى النقر على الشاشة مع الماوس للحصول على الموقف والتحرك (لا تفكر في لعبة الشطرنج والفوز أو الحكم أولاً).
خطوة:
1) التقاط الموضع الذي يتم فيه الضغط على الماوس
2) بعد التحول التنسيق (من موضع البكسل -> 0-19 موقع الشطرنج)
3) قم بتحديث الصفيف ثنائي الأبعاد الذي يسجل حالة المجلس
4) إعادة تقديم لوحة الرسم.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
A Chessman.java
حزمة 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 ؛ }} DrawChessboard.java
حزمة Xchen.test.simplegobang ؛ استيراد java.awt.graphics ؛ استيراد java.awt.graphics2d ؛ استيراد java.awt.radialgradientpaint ؛ استيراد java.awt.image ؛ استيراد java.awt.toolkit ؛ استيراد java.awt.event.mouseevent ؛ استيراد java.awt.event.mouselistener ؛ استيراد java.awt.color ؛ استيراد 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 system.out.println ("draw !!") ؛ 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]. 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*span_x) // إذا كان ذلك هو العرض/2 ، فإن قيمتان متطابقتان ستظهر في الوسط "+point_x+" "+(x-chessman_width/2+i*span_x)+" "+(x+chessman_width/2+i*span_x)) ؛ status_x = i ؛ }} لـ (int i = 0 ؛ i <rows+1 ؛ i ++) {if (point_y> = y-chessman_width/2+1+i*span_y) {if (point_y <= y+chessman_width/2-1+i*i*span_y) {system.Out.println ("point y"+i+" "+(y-chessman_width/2+1+i*span_y)+" "+(y+chessman_width/2-1+i*span_y)) ؛ status_y = i ؛ }}} Chessman Chessman = New Chessman (Black ، True) ؛ ChessStatus [status_x] [status_y] = Chessman ؛ REPAINT () ؛ } system.out.println (status_x+""+status_y+""+chessstatus [status_x] [status_y] .getColor ()+""+ChessStatus [status_x] [status_y] .getplaced ()) ؛ } Override // public void mouseclicked (mouseevent e) {// todo method method motho} override public void mousereledease (mouseevent e) {// todo method method method aduge} mouseexited (mouseevent e) {// todo method method method}}}يبقى رمز الوحدة الرئيسية دون تغيير
حزمة 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 أكثر.