Dois artigos anteriores: Java implementa dois jogos de xadrez Gozi (dois) desenham um quadro de xadrez; Java implementa dois jogos de xadrez Gozi (dois) desenham uma peça de xadrez
Na frente, desenhamos quadros de xadrez e peças de xadrez. Em seguida, precisamos clicar na tela com o mouse para obter a posição e fazer a mudança (não considerando o jogo de xadrez e a vitória ou o julgamento da derrota primeiro).
etapa:
1) Capture a posição onde o mouse é pressionado
2) Após a transformação da coordenada (da posição do pixel -> 0-19 Posição do quadro de xadrez)
3) Atualize a matriz bidimensional que registra o status da placa
4) Reestre a prancheta.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Um chessman.java
pacote xchen.test.simplegobang; classe pública Chessman {private int color; // 1-white, 0-Black Private Boolean colocado = false; public chessman (int cor, boolean colocado) {this.color = color; this.placed = colocado; } public boolean getPlaced () {return colocado; } public void SetPlaced (boolean colocado) {this.Placed = Placed; } public int getColor () {return color; } public void setColor (int cor) {this.color = color; }} Drawchessboard.java
pacote xchen.test.simplegobang; importar java.awt.graphics; importar java.awt.graphics2d; importar java.awt.RadialGradientPaint; importar java.awt.image; importar java.awt.toolkit; importar java.awt.event.mouseevent; importar java.awt.event.mouselistener; importar java.awt.color; importar javax.swing.jpanel; classe pública drawchessboard estende implementações jpanel mouselistener {final static int preto = 0; estático final Int branco = 1; public int chesscolor = preto; int chessman_width = 30; Public Image Boardimg; Final Private Int linhas = 19; Chessman [] [] chessstatus = novo chessman [linhas+1] [linhas+1]; public drawchessboard () {boardimg = Toolkit.getDefaultToolkit (). getImage ("res/drawable/chessboard2.png"); if (boardimg == null) system.err.println ("png não existe"); addmouseListener (isto); } @Override Protected Void PaintComponent (Gráfico G) {// TODO Método Gerado Auto-Gerado 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/linhas; int span_y = imgheight/linhas; G.Drawimage (Boardimg, X, Y, Null); // desenhe linha horizontal para (int i = 0; i <linhas; i ++) {g.drawline (x, y+i*span_y, fwidth-x, y+i*span_y); } // desenhe linha vertical para (int i = 0; i <linhas; i ++) {g.drawline (x+i*span_x, y, x+i*span_x, fHeight-y); } // desenha peças de xadrez para (int i = 0; i <linhas+1; i ++) {for (int j = 0; j <linhas+1; j ++) {if (chessstatus [i] [j]! int pos_x = x+i*span_x; int pos_y = y+j*span_y; Float raius_b = 40; Float raius_w = 80; float [] frações = novo float [] {0f, 1f}; java.awt.color [] colors_b = new java.awt.color [] {color.black, color.white}; Color [] colors_w = nova cor [] {color.white, color.black}; Tinta radialgradientepaint; 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, FRAÇÕES, 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, FRAÇÕES, COLORS_B); } ((Graphics2d) g) .SetPaint (tinta); ((Graphics2d) G) .Filoval (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/linhas; int span_y = imgheight/linhas; 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+imghight) {System.out.println ("legal"); para (int i = 0; i <linhas+1; i ++) {if (point_x> = x-chessman_width/2+1+i*span_x) {if (point_x <= x+chessman_width/2-1+i*span_x) // se for largura/2, dois valores de correspondência "aparecerão no meio. "+Point_x+" "+(x-chessman_width/2+i*span_x)+" "+(x+chessman_width/2+i*span_x)); status_x = i; }} para (int i = 0; i <linhas+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" "+(y-chessman_width/2+1+i*span_y)+" "+(y+chessman_width/2-1+i*span_y)); status_y = i; }}} Chessman Chessman = New Chessman (preto, verdadeiro); chessstatus [status_x] [status_y] = chessman; repintar (); } 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 Method Stub}}} stub}O código principal do módulo permanece inalterado
pacote xchen.test.simplegobang; importar java.awt.container; importar javax.swing.jframe; importar xchen.test.simplegobang.drawchessboard; A classe pública principal estende JFrame {private Drawchessboard drawchessboard; public main () {drawchessboard = new DrawChessboard (); // título de quadro Settitle ("Goji independente"); ContainerPane de contêiner = getContentPane (); Containerpane.add (DrawChessboard); } public static void main (string [] args) {main m = new main (); M.SetSize (800, 800); M.Setvisible (True); }}Execute
O exposto acima é todo o conteúdo deste artigo. Espero que seja útil para o aprendizado de todos e espero que todos apoiem mais o wulin.com.