Dos artículos anteriores: Java implementa dos juegos de ajedrez de Gozi (dos) dibuja un tablero de ajedrez; Java implementa dos juegos de ajedrez de Gozi (dos) dibuja una pieza de ajedrez
En frente, hemos dibujado tableros de ajedrez y piezas de ajedrez. A continuación, debemos hacer clic en la pantalla con el mouse para obtener el puesto y hacer el movimiento (sin considerar el juego de ajedrez y la victoria o el juicio de derrota primero).
paso:
1) Capture la posición donde se presiona el mouse
2) Después de la transformación de coordenadas (desde la posición del píxel -> 0-19 Posición del tablero de ajedrez)
3) Actualice la matriz bidimensional que registra el estado del tablero
4) Vuelva a renderizar el tablero de dibujo.
---------------------------------------------------------------------------------------------------
Un Chessman.java
paquete xchen.test.simplegoBang; Public Class Chessman {private int color; // 1-White, 0-Black Private Boolean colocado = falso; Public Chessman (int color, boolean colocado) {this.color = color; this. -colgado = colocado; } public boolean getplaced () {return colocado; } public void setplaced (boolean colocado) {this.placed = colocado; } public int getColor () {return color; } public void setColor (int color) {this.color = color; }} DrawChessboard.java
paquete xchen.test.simplegoBang; import java.awt.graphics; import java.awt.graphics2d; import java.awt.RadialGradientPaint; import java.awt.image; import java.awt.toolkit; import java.awt.event.mousevent; import java.awt.event.mouselistener; import java.awt.color; import javax.swing.jpanel; Public Class DrawChessboard extiende las implementaciones de JPanel MouseListener {final static int black = 0; Final static int white = 1; public int ChessColor = Black; int Chessman_Width = 30; Imagen pública Boardimg; Final privado int filas = 19; Chessman [] [] Chessstatus = nuevo Chessman [filas+1] [filas+1]; public drawChessboard () {boardImg = Toolkit.getDefaultToolkit (). getImage ("res/drawable/Chessboard2.png"); if (boardimg == null) system.err.println ("Png no existe"); addMouselistener (esto); } @Override protegido void pintarComponent (Graphics g) {// TODO Método Generado automático 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/filas; int span_y = imgheight/filas; G.Drawimage (Boardimg, X, Y, Null); // Dibuja la línea horizontal para (int i = 0; i <filas; i ++) {g.drawline (x, y+i*span_y, fwidth-x, y+i*span_y); } // dibujar línea vertical para (int i = 0; i <shows; i ++) {g.drawline (x+i*span_x, y, x+i*span_x, fheight-y); } // Dibuja piezas de ajedrez para (int i = 0; i <filas+1; i ++) {for (int j = 0; j <shows+1; j ++) {if (ChessStatus [i] [j]! = Null && ChessStatus [i] [j] .getPlaced () == true) {System.out.Println ("Dibuje" SRACHE "SRACHE" J). int pos_x = x+i*span_x; int pos_y = y+j*span_y; flotante radio_b = 40; flotante radio_w = 80; flotante [] fracciones = new float [] {0f, 1f}; java.awt.color [] colores_b = new java.awt.color [] {color.black, color.white}; Color [] colores_w = nuevo color [] {color.white, color.black}; Pintura radialgradientPaint; if (ChessStatus [i] [j] .getColor () == 1) {System.out.println ("Dibuja el ajedrez blanco"); pintar = nuevo RadialGradientPaint (pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_w*2, fracciones, colores_w); } else {System.out.println ("dibujar ajedrez negro"); pintura = nuevo RadialGradientPaint (pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_b*2, fracciones, colores_b); } ((Graphics2d) g) .SetPaint (pintura); ((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/filas; int span_y = imgheight/filas; System.out.println ("Presione"); 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"); for(int i=0;i<ROWS+1;i++) { if(point_x>=x-chessman_width/2+1+i*span_x) { if(point_x<=x+chessman_width/2-1+i*span_x)//If it is width/2, two matching values will appear at the middle point { System.out.println("point x "+i+" "+Point_X+" "+(x-chessman_width/2+i*span_x)+" "+(x+chessman_width/2+i*span_x)); status_x = i; }} para (int i = 0; i <filas+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 (negro, verdadero); Chessstatus [status_x] [status_y] = Chessman; repintado (); } 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 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 Generado Método STUB}}El código del módulo principal permanece sin cambios
paquete xchen.test.simplegoBang; import java.awt.container; import javax.swing.jframe; importar xchen.test.SimpleGobang.DrawChessboard; Public Class Main extiende JFrame {DrawChessboard private DrawChessboard; public main () {drawChessboard = new DrawChessboard (); // Marco de título Settitle ("Stand-Alone goji"); Contenedor contenedorpane = getContentPane (); ContainerPane.Add (DrawChessboard); } public static void main (string [] args) {main m = new main (); M.Setsize (800, 800); M.SetVisible (verdadero); }}Ejecutar
Lo anterior es todo el contenido de este artículo. Espero que sea útil para el aprendizaje de todos y espero que todos apoyen más a Wulin.com.