Deux articles précédents: Java implémente deux gozi jeu (deux) pour dessiner un tableau d'échecs; Java implémente deux gozi jeu (trois) pour dessiner une pièce d'échecs; Java implémente deux gozi jeu (quatre) pour réaliser le mouvement; Java implémente deux gozi jeu (six) pour transformer le côté d'échecs, vous pouvez cliquer pour voir.
La fonction suivante que nous voulons implémenter consiste à provoquer des informations sur l'écran lorsqu'il y a cinq billes consécutives. Utilisez simplement ShowMessageDialog dans la partie ISWIN de la fonction Mousreprim.
Le code de fonction soupçonné est le suivant:
@Override // public void Mousdressressed (MouseEvent e) {int point_x = e.getx (); int point_y = e.gety (); int imgwidth = boardimg.getheight (this); int imgheight = boardimg.getwidth (this); int fwidth = getWidth (); int fheight = getheight (); int x = (fwidth-imgwidth) / 2; int y = (fheight-iMgheight) / 2; int span_x = imgwidth / lignes; int span_y = imgheight / lignes; //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 "); pour (int i = 0; i <lignes + 1; i ++) {if (Point_x> = x-chessman_width / 2 + 1 + i * span_x) {if (Point_x <= x + Chessman_Width / 2-1 + i * span_x) // si c'est width / 2, deux valeurs de correspondance apparaîtront au milieu {//System.out.Printlnwn. "+ Point_x +" "+ (x-chessman_width / 2 + i * span_x) +" "+ (x + Chessman_width / 2 + i * span_x)); status_x = i; }}} pour (int i = 0; i <lignes + 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("point y "+ 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 ("Couleur d'échecs:" + ChessColor); if (ChessColor == Black) {ChessColor = blanc; } else {ChessColor = Black; } repeain (); // Si win, donnez un message rapide if (iswin (status_x, status_y, Chessstatus)) {System.out.println ("win !!!"); Winner String; // Si le prochain enfant est blanc, alors cette fois, il est noir si (ChessColor == blanc) gagnant = "noir"; else winner = "blank"; String mstring = string.format ("Félicitations,% s win !!!", gagnant); JOptionPane.showMessageAdIalog (this, mstring); }}}}Exécutez-le:
À ce stade, nous avons rempli la fonction Gozi la plus basique. Je publierai le code complet ci-dessous:
Chessman.java
Package xchen.test.simplegobang; classe publique Chessman {private int couleur; // 1-blanc, 0-noir privé boolean placé = false; int matchCount = 1; public Chessman (int couleur, booléen placé) {this.color = couleur; this.placed = placé; } public boolean getplace () {return placé; } public void setplaced (booléen placé) {this.placed = placé; } public int getColor () {return couleur; } public void setColor (int colore) {this.color = couleur; }} Main.java
Package xchen.test.simplegobang; Importer Java.awt.Container; import javax.swing.jframe; import xchen.test.simplegobang.Drawchessboard; La classe publique Main étend JFrame {Private Drawchess Drawchessboard; public main () {drawchessboard = new Drawchessboard (); // Cadre Title Setttitle ("autonome goji"); Conteneur ContainerPane = getContentPane (); ContainerPane.Add (Drawchessboard); } public static void main (String [] args) {main m = new Main (); M.SetSize (800, 800); M.SetVisible (vrai); }} Drawchessboard.java
Package xchen.test.simplegobang; import java.awt.color; import java.awt.graphics; import java.awt.graphics2d; import java.awt.image; Importer java.awt.radialgradientpaint; import java.awt.toolkit; Importer java.awt.event.mousevent; Importer java.awt.event.mouseListener; import javax.swing.joptionpane; import javax.swing.jpanel; La classe publique Drawchessboard étend Jpanel implémente MouseListener {final static int noir = 0; Final Static int blanc = 1; public int chesscolor = noir; int Chessman_Width = 30; Image publique BoardImg; lignes int privées finales = 19; Chessman [] [] Chessstatus = new Chessman [lignes + 1] [lignes + 1]; public drawchessboard () {boardimg = toolkit.getDefaultToolkit (). getImage ("Res / Drawable / Chessboard2.png"); if (boardimg == null) System.err.println ("png n'existe pas"); AddMouseListener (this); } @Override Protected void PaintComponent (Graphics G) {// TODO Méthode générée automatique Stub super.paintComponent (G); int imgwidth = boardimg.getheight (this); int imgheight = boardimg.getwidth (this); int fwidth = getWidth (); int fheight = getheight (); int x = (fwidth-imgwidth) / 2; int y = (fheight-iMgheight) / 2; int span_x = imgwidth / lignes; int span_y = imgheight / lignes; G.DrawImage (BoardImg, X, Y, NULL); // dessine la ligne horizontale pour (int i = 0; i <lignes; i ++) {g.drawline (x, y + i * span_y, fwidth-x, y + i * span_y); } // dessiner une ligne verticale pour (int i = 0; i <lignes; i ++) {g.drawline (x + i * span_x, y, x + i * span_x, fheight-y); } // dessiner des pièces d'échecs pour (int i = 0; i <lignes + 1; i ++) {for (int j = 0; j <lignes + 1; j ++) {if (Chessstatus [i] [j]! = Null && Chessstatus [i] [j] .getplaced () == true) {//system.out.println("Draw chessman "+" + 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 [] couleurs_b = new java.awt.color [] {color.black, colore.white}; Couleur [] couleurs_w = new Color [] {Color.white, Color.Black}; Peinture radialgradientpaint; if (ChessStatus [i] [j] .getColor () == 1) {//system.out.println("raw échose blanc "); peinture = new RadialgradIentPaint (pos_x-chessman_width / 2f, pos_y-chessman_width / 2f, radius_w * 2, fractions, couleurs_w); } else {//system.out.println("raw échose noir "); peinture = new RadialgradIentPaint (pos_x-bessman_width / 2f, pos_y-bessman_width / 2f, radius_b * 2, fractions, couleurs_b); } ((Graphics2d) g) .setpaint (peinture); ((Graphics2d) g) .Filloval (pos_x-chessman_width / 2, pos_y-chessman_width / 2, Chessman_width, Chessman_width); }}}} @Override // public void Mousreprim (MouseEvent e) {int point_x = e.getx (); int point_y = e.gety (); int imgwidth = boardimg.getheight (this); int imgheight = boardimg.getwidth (this); int fwidth = getWidth (); int fheight = getheight (); int x = (fwidth-imgwidth) / 2; int y = (fheight-iMgheight) / 2; int span_x = imgwidth / lignes; int span_y = imgheight / lignes; //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 "); pour (int i = 0; i <lignes + 1; i ++) {if (Point_x> = x-chessman_width / 2 + 1 + i * span_x) {if (Point_x <= x + Chessman_Width / 2-1 + i * span_x) // si c'est width / 2, deux valeurs de correspondance apparaîtront au milieu {//System.out.Printlnwn. "+ Point_x +" "+ (x-chessman_width / 2 + i * span_x) +" "+ (x + Chessman_width / 2 + i * span_x)); status_x = i; }} pour (int i = 0; i <lignes + 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("point y "+ i +" "+ point_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 ("Couleur d'échecs:" + ChessColor); if (ChessColor == Black) {ChessColor = blanc; } else {ChessColor = Black; } repeain (); // Si vous gagnez, donnez un message rapide si (iswin (status_x, status_y, Chessstatus)) {System.out.println ("Win !!!!"); Winner String; // Si le prochain enfant est blanc, alors cette fois, il est noir si (ChessColor == blanc) gagnant = "noir"; else winner = "blanc"; String mstring = string.format ("Félicitations,% s win !!!", gagnant); JOptionPane.showMessageAdIalog (this, mstring); }}}} @Override // public void MouseClicked (MouseEvent E) {// TODO Méthode générée automatiquement Stub} @Override Public Void MouseLelent (Mouseevent E) {// TODO GÉNÉRÉ AUTO-généré Stub} @Override Public Void Mouseleleled Stud @override E) void muouseentered (Mouseevent e) {// Todo méthode générée automatiquement Stub} @Override public void MouseExit (Mouseevent e) {// TODO Méthode générée automatiquement J = 0; J <Rows + 1; J ++) {// Search à long-côté if (Chessstatus [i] [J]! = NULL && Chessstatus [i] [J] .getplaced () == true) {int matchColor = Chessstatu [i] [j] .getColor (); // recherche (int n = 1; n <= 4; n ++) {if ((i + n> = 0) && (i + n) <= lignes) {if (Chessstatus [i + n] [j]! = Null && Chessstatus [i + n] [j] .getplaced () == true && chessstatu [i + n] [j] .getcol {Chessstatus [i] [J] .MatchCount ++; System.out.println ("pos:" + i + "" + j + "Right Count ++:" + (i + n) + "" + j + "Count:" + Chessstatu [i] [j] .matchCount); if (Chessstatus [i] [j] .matchCount == 5) {return true; }} else {Break; }}} // Recherchez (int n = 1; n <= 4; n ++) {if ((in> = 0) && (in) <= lignes) {if (Chessstatus [in] [j]! = Null && chessstatu [in] [j] .getplace () == true && chessstatus [in] [j] .getColor () ==) Chessstatus [i] [J] .MatchCount ++; System.out.println ("pos:" + i + "" + j + "" + "Left Count ++:" + (in) + "" + j + "count:" + Chessstatu [i] [j] .matchCount); if (Chessstatus [i] [j] .matchCount == 5) {return true; }} else {if (Chessstatus [in] [j]! = null) {Chessstatus [i] [j] .matchCount = 1; } casser; }}} che if (Chessstatus [i] [j]! = null && Chessstatus [i] [j] .getplaced () == true) {int matchColor = Chessstatus [i] [j] .getColor (); // Regarde vers le bas, le coin supérieur gauche est l'origine de coordonnées, la direction positive de l'axe y est en panne pour (int n = 1; n <= 4; n ++) {if ((j + n> = 0) && (j + n) <= lignes) { if (Chessstatus [i] [j + n]! = null && Chessstatus [i] [j + n] .getplaced () == true && chessstatu [i] [j + n] .getColor () == matchColor) {Chessstatus [i] [j] .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; }} else {Break; }}} // Recherchez (int n = 1; n <= 4; n ++) {if ((jn> = 0) && (jn) <= lignes) { if (Chessstatus [i] [jn]! = null && Chessstatus [i] [jn] .getplaced () == true && chessstatu [i] [jn] .getColor () == matchColor) {Chessstatus [i] [j] .matchCount ++; System.out.println ("pos:" + i + "" + j + "" + "Left Count ++:" + (i) + "" + (jn) + "Count:" + Chessstatus [i] [j] .matchCount); if (Chessstatus [i] [j] .matchCount == 5) {return true; }} else {if (Chessstatus [i] [jn]! = null) {Chessstatus [i] [j] .matchCount = 1; } casser; }}} Chessstatus [i] [J] .matchCount = 1; // Refresh Count}}}} // Direction: Upper gauche et inférieur à droite pour (int i = 0; i <lignes + 1; i ++) {pour (int j = 0; j <lignes + 1; j ++) {// gauche gauche gauche if (Chessstatus [i] [j]! = null && Chessstatus [i] [j] .getplaced () == true) {int matchColor = Chessstatus [i] [j] .getColor (); // Regarde vers le bas, le coin supérieur gauche est l'origine de coordonnées, et la direction positive de l'axe y est en baisse pour (int n = 1; n <= 4; n ++) {if ((jn> = 0) && (jn) <= lignes && (in)> = 0 && (in) <= rows) { if (Chessstatus [in] [jn]! = null && Chessstatus [in] [jn] .getplaced () == true && chessstatu [in] [jn] .getColor () == matchColor) {Chessstatus [i] [j] .matchCount ++; System.out.println ("pos:" + i + "" + j + "up count ++:" + (in) + "" + (jn) + "count:" + Chessstatus [i] [j] .matchCount); if (Chessstatus [i] [j] .matchCount == 5) {return true; }} else {Break; }}} // En bas à droite pour (int n = 1; n <= 4; n ++) {if ((j + n> = 0) && (j + n) <= lignes && (i + n)> = 0 && (i + n) <= lignes) { if (Chessstatus [i + n] [j + n]! = null && Chessstatus [i + n] [j + n] .getplaced () == true && chessstatu [i + n] [j + n] .getColor () == matchColor) {chessstatus [i] [j] .matchCount ++; System.out.println ("pos:" + i + "" + j + "" + "Left Count ++:" + (i + n) + "" + (j + n) + "count:" + Chessstatus [i] [j] .matchCount); if (Chessstatus [i] [j] .matchCount == 5) {return true; }} else {if (Chessstatus [i + n] [j + n]! = null) {Chessstatus [i] [j] .matchCount = 1; } casser; }}} Chessstatus [i] [J] .matchCount = 1; // Refresh Count}}}} // Direction: inférieur gauche supérieur droit supérieur pour (int i = 0; i <lignes + 1; i ++) {pour (int j = 0; j <lignes + 1; j ++) {// inférieur gauche gauche if (Chessstatus [i] [j]! = null && Chessstatus [i] [j] .getplaced () == true) {int matchColor = Chessstatus [i] [j] .getColor (); // Regarde vers le bas, le coin supérieur gauche est l'origine de coordonnées, et la direction positive de l'axe y est en panne pour (int n = 1; n <= 4; n ++) {if ((j + n> = 0) && (j + n) <= lignes && (in)> = 0 && (in) <= lignes) {{{{{{{{{{{{{{{{{{{{{{{{{) {{les lignes ». 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; }} else {Break; }}} // supérieur à droite pour (int n = 1; n <= 4; n ++) {if ((jn> = 0) && (jn) <= lignes && (i + n)> = 0 && (i + n) <= lignes) { if (Chessstatus [i + n] [jn]! = null && Chessstatus [i + n] [jn] .getplaced () == true && Chessstatus [i + n] [jn] .getColor () == matchColor) {Chessstatus [i] [j] .matchCount ++; System.out.println ("pos:" + i + "" + j + "" + "Left Count ++:" + (i + n) + "" + (jn) + "count:" + Chessstatus [i] [j] .matchCount); if (Chessstatus [i] [j] .matchCount == 5) {return true; }} else {if (Chessstatus [i + n] [jn]! = null) {Chessstatus [i] [j] .matchCount = 1; } casser; }}} Chessstatus [i] [j] .matchCount = 1; // Refresh Count}}} return false; }}Pour des jeux plus excitants, veuillez vous référer au sujet spécial "Java Classic Games"
Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.