Deux articles précédents: Java met en œuvre deux gozi gibs jeu (deux) dessine un tableau d'échecs; Java met en œuvre deux gozi au jeu d'échecs (deux) dessine une pièce d'échecs
Devant, nous avons dessiné des échecs et des pièces d'échecs. Ensuite, nous devons cliquer sur l'écran avec la souris pour obtenir la position et faire le pas (sans considérer le jeu d'échecs et la victoire ou la défaite en premier).
étape:
1) Capturez la position où la souris est pressée
2) Après la transformation des coordonnées (à partir de la position des pixels -> 0-19 Position d'échecs)
3) Mettez à jour le tableau bidimensionnel qui enregistre l'état de la carte
4) Renforcez la planche à dessin.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
A Chessman.java
Package xchen.test.simplegobang; classe publique Chessman {private int couleur; // 1-blanc, 0-noir privé boolean placé = false; 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; }} Drawchessboard.java
Package xchen.test.simplegobang; import java.awt.graphics; import java.awt.graphics2d; Importer java.awt.radialgradientpaint; import java.awt.image; import java.awt.toolkit; Importer java.awt.event.mousevent; Importer java.awt.event.mouseListener; import java.awt.color; import javax.swing.jpanel; La classe publique Drawchessboard étend les implémentations JPanel 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 System.out.println ("Draw !!"); 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 ++) {pour (int j = 0; j <lignes + 1; j ++) {if (Chessstatus [i] [j]! = Null && Chessstatus [i] [j] .getplace () == true) {system.out.println ("dessiner l'écheclman" + 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 [] 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 ("Draw White Chess"); peinture = new RadialgradIentPaint (pos_x-chessman_width / 2f, pos_y-chessman_width / 2f, radius_w * 2, fractions, couleurs_w); } else {System.out.println ("Draw Black Chess"); 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 ("pression"); 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 ("légal"); 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.out.println ("" " "+ Point_x +" "+ (x-chessman_width / 2 + i * span_x) +" "+ (x + Chessman_width / 2 + i * span_x)); status_x = i; }} pour (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 * 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; }}} Chessman Chessman = new Chessman (noir, vrai); ChessStatus [Status_X] [Status_y] = Chessman; repeindre(); } System.out.println (status_x + "" + statut_y + "" + Chessstatus [status_x] [statut_y] .getColor () + "" + Chessstatus [status_x] [statut_y] .getplaced ()); } @Override // public void mouleclicked (MouseEvent e) {// Todo Méthode générée automatiquement Stub} @Override public void Mouseleled (Mouseevent E) {// Todo Generated Method Stub} @Override Public Void Mouseentered (MouseEvent E) {// TODO AUTO-généré par la méthode générée sur l'auto-généré Auto-généré par Auto-généré par Auto-généré Stub} MouseExited (Mouseevent E) {// TODO Méthode générée automatiquement Stub}}Le code du module principal reste inchangé
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); }}Courir
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.