Zwei frühere Artikel: Java implementiert zwei Gozi -Spiele (zwei), um ein Schachbrett zu zeichnen; Java implementiert zwei Gozi -Spiele (drei), um ein Schachstück zu zeichnen; Java implementiert zwei Gozi -Spiele (vier), um den Schritt zu erkennen; Java implementiert zwei Gozi -Spiele (sechs), um die Schachseite zu verwandeln. Sie können klicken, um sie anzeigen.
Die nächste Funktion, die wir implementieren möchten, besteht darin, Informationen auf dem Bildschirm zu fordern, wenn fünf aufeinanderfolgende Perlen vorhanden sind. Verwenden Sie einfach ShowMessagedialog im Iswin -Teil der mousePressed -Funktion.
Der MousePressed -Funktionscode lautet wie folgt:
@Override // public void mousePressed (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/Zeilen; int span_y = imgHeight/Zeilen; //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 "); für (int i = 0; i <Zeilen+1; i ++) {if (point_x> = x-shessman_width/2+1+i*span_x) {if (point_x <= x+chessman_width/2-1+i*span_x) // if it width/2, zwei Matching-Werte. "+point_x+" "+(x-shessman_width/2+i*span_x)+" "+(x+chessman_width/2+i*span_x)); status_x = i; }}} für (int i = 0; i <Zeilen+1; i ++) {if (point_y> = y-shessman_width/2+1+i*span_y) {if (point_y <= y+chessman_width/2-1+i*span_y) {//System.out.println("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] .getPlact () == false) {chessman chessman = neu chessman (chesscolor, true); chessstatus [status_x] [status_y] = chessman; System.out.println ("Schachfarbe:"+chesscolor); if (chesscolor == schwarz) {chesscolor = weiß; } else {chesscolor = schwarz; } repaint (); // Wenn Sie gewinnen, geben Sie eine Eingabeaufforderung Meldung an, wenn (iswin (status_x, status_y, schessstatus)) {System.out.println ("Win !!!"); Streicher Gewinner; // Wenn das nächste Kind weiß ist, dann ist es diesmal schwarz, wenn (chesscolor == White) Gewinner = "schwarz"; sonst winner = "blank"; String mstring = String.Format ("Herzlichen Glückwunsch, %s Win !!!", Gewinner); Joptionpane.showmessagedialog (this, mstring); }}}}Führen Sie es aus:
Zu diesem Zeitpunkt haben wir die grundlegendste Gozi -Funktion ausgefüllt. Ich werde den vollständigen Code unten veröffentlichen:
Chessman.java
Paket xchen.test.simlegobang; öffentliche Klasse Chessman {private int color; // 1-White, 0-schwarz private boolean platziert = false; int matchCount = 1; public chessman (int color, boolean platziert) {this.color = color; this.placed = platziert; } public boolean getPlace () {return platziert; } public void setPlate (boolean platziert) {this.placed = platziert; } public int getColor () {return color; } public void setColor (int color) {this.color = color; }} Main.java
Paket xchen.test.simlegobang; import Java.awt.Container; importieren javax.swing.jframe; import xchen.test.simlegobang.drawchessboard; öffentliche Klasse Main erweitert JFrame {private Drawchessboard Drawchessboard; public main () {drawchStboard = new Drawchessboard (); // Rahmen Titel settitle ("Stand-Alone Goji"); Container ContainerPane = getContentPane (); Containerpane.add (Drawchessboard); } public static void main (String [] args) {main m = new Main (); M.Setsize (800, 800); m.setvisible (true); }} Drawchessboard.java
Paket xchen.test.simlegobang; Import Java.awt.Color; importieren java.awt.graphics; Import Java.awt.Graphics2d; import Java.awt.image; import Java.awt.RadialgradientPaint; import Java.awt.toolkit; import Java.awt.event.mouseeEvent; import Java.awt.event.mouselistener; import Javax.swing.joptionpane; import Javax.swing.jpanel; öffentliche Klasse Drawchessboard erweitert JPanel implementiert Mouselistener {endgültige statische int schwarz = 0; endgültige statische int weiß = 1; public int chesscolor = schwarz; int chessman_width = 30; öffentliches Image Boardimg; endgültige private intreihen = 19; Chessman [] [] schachstatus = neuer Schachmann [Zeilen+1] [Zeilen+1]; public drawchessboard () {boardimg = toolkit.getDefaulttoolkit (). getImage ("res/Drawable/chessboard2.png"); if (boardimg == null) system.err.println ("PNG nicht existieren"); addmouselistener (dies); } @Override Protected void PaintComponent (Grafik g) {// Todo automatisch generierte Methode 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/Zeilen; int span_y = imgHeight/Zeilen; G.Drawimage (Boardimg, X, Y, Null); // Horizontale Zeile für (int i = 0; i <Zeilen; i ++) {g.drawline (x, y+i*span_y, fwidth-x, y+i*span_y); } // vertikale Zeile zeichnen (int i = 0; i <Zeilen; i ++) {g.drawline (x+i*span_x, y, x+i*span_x, fheight-y); } // Schachstücke zeichnen für (int i = 0; i <Zeilen+1; i ++) {für (int j = 0; j <Zeilen+1; j ++) {if (schessstatus [i] [j]! "+j); int pos_x = x+i*span_x; int pos_y = y+j*span_y; float radius_b = 40; float radius_w = 80; float [] fraktions = new float [] {0f, 1f}; java.awt.color [] colors_b = new java.awt.color [] {color.black, color.white}; Color [] colors_w = new color [] {color.white, color.black}; RadialgradientPaint -Farbe; if (chessstatus [i] [j] .getColor () == 1) {//system.out.println("draw White Chess "); Paint = new radialgradientPaint (POS_X-CHESSMAN_WIDTH/2F, POS_Y-CHESSMAN_WIDTH/2F, radius_w*2, Fraktionen, Colors_W); } else {//system.out.println("DRAW Black Chess "); Paint = new radialgradientPaint (POS_X-CHESSMAN_WIDTH/2F, POS_Y-CHESSMAN_WIDTH/2F, radius_b*2, Fraktionen, Colors_B); } ((Graphics2d) g) .setPaint (Farbe); ((Graphics2d) g) .filloval (pos_x-chessman_width/2, pos_y-chessman_width/2, chessman_width, chessman_width); }}}} @Override // public void mousePressed (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/Zeilen; int span_y = imgHeight/Zeilen; //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 "); für (int i = 0; i <Zeilen+1; i ++) {if (point_x> = x-shessman_width/2+1+i*span_x) {if (point_x <= x+chessman_width/2-1+i*span_x) // if it width/2, zwei Matching-Werte. "+point_x+" "+(x-shessman_width/2+i*span_x)+" "+(x+chessman_width/2+i*span_x)); status_x = i; }} für (int i = 0; i <Zeilen+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.println("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] .getPlact () == false) {chessman chessman = neu chessman (chesscolor, true); chessstatus [status_x] [status_y] = chessman; System.out.println ("Schachfarbe:"+chesscolor); if (chesscolor == schwarz) {chesscolor = weiß; } else {chesscolor = schwarz; } repaint (); // Wenn Sie gewinnen, geben Sie eine Eingabeaufforderung Meldung an, wenn (iswin (status_x, status_y, schessstatus)) {System.out.println ("Win !!!!"); Streicher Gewinner; // Wenn das nächste Kind weiß ist, dann ist es diesmal schwarz, wenn (chesscolor == White) Gewinner = "schwarz"; sonst winner = "white"; String mstring = String.Format ("Herzlichen Glückwunsch, %s Win !!!", Gewinner); Joptionpane.showmessagedialog (this, mstring); } } } } @Override // public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } boolean isWin(int point_x,int point_y,Chessman[][] cm) { for(int i = 0; i <Zeilen+1; i ++) {für (int j = 0; j <Zeilen+1; j ++) {// Langzeit-Suche if (schachstatus [i] [j]! // Suchen Sie nach (int n = 1; n <= 4; n ++) {if ((i+n> = 0) && (i+n) <= Zeilen) {if (chessstatus [i+n] [j]! {chessstatus [i] [j] .MatchCount ++; System.out.println ("pos:"+i+""+j+"Rechtszahl ++:"+(i+n)+""+J+"count:"+chessstatus [i] [j] .MatchCount); if (chessstatus [i] [j] .MatchCount == 5) {return true; }} else {break; }}} // Suchen Sie nach (int n = 1; n <= 4; n ++) {if ((in> = 0) && (in) <= ruws) {if (chessstatus [in] [j]! Schachstatus [i] [j] .MatchCount ++; System.out.println ("pos:"+i+""+j+""+"linke zahlung ++:"+(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; } brechen; }}}} chessstatus [i] [j] .MatchCount = 1; // Aktualisieren}}} für (int i = 0; i <Zeilen+1; i ++) {für (int j = 0; j <Zeilen+1; j ++) {// perpendikulär if (chessstatus [i] [j]! // schauen nach unten, die obere linke Ecke ist der Koordinatenursprung, die positive Richtung der y-Achse ist für (int n = 1; n <= 4; n ++) {if ((j+n> = 0) && (j+n) <= Zeilen) { if (chessStatus [i] [j+n]! 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; }}} // suchen nach (int n = 1; n <= 4; n ++) {if ((jn> = 0) && (jn) <= Zeilen) { if (chessstatus [i] [jn]! System.out.println ("pos:"+i+""+j+""+"linke zahlung ++:"+(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; } brechen; }}} chessStatus [i] [j] .MatchCount = 1; // Aktualisieren}}} // Richtung: Oben links und unten rechts für (int i = 0; i <Zeilen+1; i ++) {für (int j = 0; j <Zeilen+1; j ++) {// oben links oben links (int j = 0; if (chessstatus [i] [j]! // Schauen Sie nach unten, die obere linke Ecke ist der Koordinatenursprung, und die positive Richtung der y-Achse ist für (int n = 1; n <= 4; n ++) {if ((jn> = 0) && (jn) <= Zeilen && (in)> = 0 && (in) <= Reihen) {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{in). if (chessstatus [in] [jn]! 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; }}} // unten rechts für (int n = 1; n <= 4; n ++) {if ((j+n> = 0) && (j+n) <= Zeilen && (i+n)> = 0 && (i+n) <= rows) { if (chessstatus [i+n] [j+n]! System.out.println ("pos:"+i+""+j+""+"linke zahlung ++:"+(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; } brechen; }}} chessStatus [i] [j] .MatchCount = 1; // Aktualisieren}}} // Richtung: unter links oben rechts obere für (int i = 0; i <Zeilen+1; i ++) {für (int j = 0; j <Zeilen+1; j ++) {// unten links unten links (int j = 0; if (chessstatus [i] [j]! // schau nach unten, die obere linke Ecke ist der Koordinatenursprung, und die positive Richtung der y-Achse ist für (int n = 1; n <= 4; n ++) {if ((j+n> = 0) && (j+n) <= Zeilen && (in)> = 0 && (in) <= Zeilen) {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{if)). if (chessStatus [in] [j+n]! 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; }}} // Ober rechtes für (int n = 1; n <= 4; n ++) {if ((jn> = 0) && (jn) <= Zeilen && (i+n)> = 0 && (i+n) <= rows) { if (chessstatus [i+n] [jn]! System.out.println ("pos:"+i+""+j+""+"linke zahlung ++:"+(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; } brechen; }}} chessstatus [i] [j] .MatchCount = 1; // aktualisieren}}}} return false; }}Weitere aufregende Spiele finden Sie auf dem speziellen Thema "Java Classic Games".
Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.