Artikel sebelumnya berbicara tentang Java yang mengimplementasikan Game Catur Two Gozi (2) menggambar papan catur, dan papan catur telah digambar. Selanjutnya, kita perlu menerapkan fungsi kontrol, fungsi utama:
1) Pilih sepotong catur
2) Gambar catur
3) menilai pemenangnya
4) Pertukaran Catur
Sadari bagian dari gambar catur terlebih dahulu
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Pertama, tentukan kelas potongan catur. Kelas ini memiliki dua atribut: warna karya (0- berarti hitam dan 1- berarti putih), dan apakah akan bergerak (saya berencana menggunakan array dua dimensi untuk menyimpan informasi giliran catur)
Chessman.java
paket xchen.test.simplegobang; kelas publik catur {private int color; // 1-white, 0-black private boolean ditempatkan = false; catur publik (warna int, boolean ditempatkan) {this.color = warna; this.placed = ditempatkan; } public boolean getplaced () {return ditempatkan; } public void setplaced (boolean ditempatkan) {this.placed = ditempatkan; } public int getColor () {return color; } public void setColor (int color) {this.color = color; }} Kemudian kami telah menggambar bagian kode dari papan catur di bagian sebelumnya dan menambahkan kode untuk menggambar potongan catur. Saya menggunakan dua potong catur (satu putih dan satu hitam, terletak di papan catur [8, 8], [7, 7]) untuk menguji kode untuk menggambar potongan catur.
Drawchessboard.java
paket xchen.test.simplegobang; impor java.awt.graphics; impor java.awt.graphics2d; impor java.awt.radialgradientpaint; impor java.awt.image; impor java.awt.toolkit; impor java.awt.color; impor javax.swing.jpanel; Public Class Drawchessboard memperluas jpanel {final static int black = 0; Final Static Int White = 1; Public Int ChessColor = Hitam; boardimg citra publik; Baris int privat terakhir = 19; CHESSMAN [] [] CHESSSTATUS = CHESSMAN baru [baris] [baris]; Public DrawChessboard () {boardimg = toolkit.getDefaultToolKit (). getImage ("res/drawable/chessboard2.png"); if (boardimg == null) system.err.println ("png tidak ada"); // test draw bagian catur sederhana catur sederhana catur = catur baru (0, true); Chessstatus [7] [7] = Chessman; Chessman Chessman2 = catur baru (1, true); Chessstatus [8] [8] = Chessman2; // test draw cHessman bagian sederhana} @Override void paintComponent void (grafik g) {// TODO Metode yang dihasilkan secara otomatis Stub super.paintComponent (g); int imgwidth = boardimg.getheight (ini); int imgheight = boardimg.getwidth (ini); int fwidth = getWidth (); int fheight = getHeight (); int x = (fwidth-imgwidth)/2; int y = (fheight-imgheight)/2; G.DrawImage (boardimg, x, y, null); int margin = x; int span_x = imgwidth/baris; int span_y = imgheight/baris; // Gambarlah garis horizontal untuk (int i = 0; i <baris; i ++) {g.drawline (x, y+i*span_y, fwidth-x, y+i*span_y); } // Gambarlah garis vertikal untuk (int i = 0; i <rows; i ++) {g.drawline (x+i*span_x, y, x+i*span_x, fheight-y); } // Gambar catur untuk (int i = 0; i <baris; i ++) {for (int j = 0; j <baris; j ++) {if (chessstatus [i] [j]! = Null && chessstatus [i] [j] .getPlaced () == true) {System.out.uTln (i] [j] .gesplaced () == true) {System.out.uTln (i] [j] .gesplaced () == true) {System.out.uTln (i] [j]. "II"+"i i (); i 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 [] color_b = new java.awt.color [] {color.black, color.white}; Color [] color_w = warna baru [] {color.white, color.black}; Cat RadialGradientPaint; if (chessstatus [i] [j] .getColor () == 1) {System.out.println ("Gambar catur putih"); cat = RadialGradientPaint baru (pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_w*2, fraksi, warna_w); } else {System.out.println ("Draw Black Chess"); cat = RadialGradientPaint baru (pos_x-chessman_width/2f, pos_y-chessman_width/2f, Radius_b*2, fraksi, warna_b); } ((Grafik2d) g) .setPaint (cat); ((Graphics2d) g) .filloval (pos_x-chessman_width/2, pos_y-chessman_width/2, chessman_width, chessman_width); }}}}}}}} Kode modul utama tetap tidak berubah
Main.java
paket xchen.test.simplegobang; impor java.awt.container; impor javax.swing.jframe; impor xchen.test.simplegobang.drawchessboard; Kelas Publik Main Memperluas JFrame {Private Drawchessboard Drawchessboard; Public Main () {DrawChessboard = New DrawChessboard (); // bingkai judul settitle ("stand-alone goji"); Container containerpane = getContentPane (); containerpane.add (drawchessboard); } public static void main (string [] args) {main m = baru main (); m.setsize (800, 800); M.SetVisible (true); }}Jalankan!
Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.