두 개의 이전 기사 : Java는 두 개의 Gozi Chess 게임 (2)을 구현합니다. Java는 두 개의 Gozi Chess 게임 (2)을 Chess 조각을 그립니다.
앞에는 체스 보드와 체스 조각을 그렸습니다. 다음으로, 우리는 마우스로 화면을 클릭하여 위치를 얻고 움직입니다 (체스 게임과 승리를 고려하지 않거나 판결을 먼저 고려하지 않음).
단계:
1) 마우스를 누르는 위치를 캡처합니다
2) 좌표 변환 후 (픽셀 위치에서 -> 0-19 체스 보드 위치)
3) 보드 상태를 기록하는 2 차원 배열 업데이트
4) 드로잉 보드를 다시 렌더링하십시오.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
체스 맨. 자바
패키지 xchen.test.simplegobang; 공개 클래스 Chessman {Private Int Color; // 1- 백인, 0- 블랙 개인 부울 = 거짓; Public Chessman (int color, 부울 배치) {this.color = color; this.placed = 배치; } public boolean getPlaced () {반환 배치; } public void setplaced (부울 배치) {this.placed = 배치; } public int getColor () {return color; } public void setColor (int color) {this.color = color; }} Drawchessboard.java
패키지 xchen.test.simplegobang; java.awt.graphics 가져 오기; import java.awt.graphics2d; import java.awt.radialgradientPaint; import java.awt.image; java.awt.toolkit import; java.awt.event.mouseevent import; import java.awt.event.mouselistener; import java.awt.color; import javax.swing.jpanel; Public Class DrawChessboard는 JPanel 구현을 확장합니다. MousElistener {Final STATIC INT BLACK = 0; 최종 정적 int 화이트 = 1; 공개 int chesscolor = 검은 색; int chessman_width = 30; 공개 이미지 Boardimg; 최종 개인 int 행 = 19; Chessman [] [] Chessstatus = New Chessman [rows+1] [Rows+1]; public drawchessboard () {boardimg = toolkit.getDefaultToolKit (). getImage ( "res/drawable/chessboard2.png"); if (boardimg == null) system.err.println ( "png는 존재하지 않습니다"); AddMousElistener (this); } @Override Protected Void PaintComponent (Graphics G) {// TODO 자동 생성 메소드 스터브 시스템.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/rows; int span_y = imgheight/rows; G.DrawImage (Boardimg, X, Y, NULL); // (int i = 0; i <rows; i ++) {g.drawline (x, y+i*span_y, fwidth-x, y+i*span_y)에 대한 수평 선을 그립니다. } // (int i = 0; i <rows; i ++) {g.drawline (x+i*span_x, y, x+i*span_x, fheight-y)에 대한 수직 선을 그립니다. } // (int i = 0; i <rows+1; i ++) {for (int j = 0; j <rows+1; j ++) {if (chessstatus [i] [j]! = null && chessstatus [i] [j] .getplaced () == true) {system.out.println ( "+"+"+"+"+"+""; 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 [] colors_b = new java.awt.color [] {color.black, color.white}; color [] colors_w = new color [] {color.white, color.black}; 방사형 그레이드 페인트 페인트; if (chessstatus [i] [j] .getColor () == 1) {System.out.println ( "흰색 체스"); Paint = New RadialgradientPaint (pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_w*2, 분수, colors_w); } else {System.out.println ( "검은 체스 그리기"); Paint = New RadialgradientPaint (pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_b*2, 분수, colors_b); } ((Graphics2d) g) .setPaint (페인트); ((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/rows; int span_y = imgheight/rows; 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 ( "법률"); 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) // 두 개의 일치 값은 중간 지점에서 나타납니다. "+point_x+" "+(x-chessman_width/2+i*span_x)+"+(x+chessman_width/2+i*span_x); 상태 _x = i; }} for (int i = 0; i <rows+1; i ++) {if (point_y> = ychessman_width/2+1+i*span_y) {if (point_y <= y+chessman_width/2-1+i*span_y) {system.out.out.out.out.out. "+(y-chessman_width/2+1+i*span_y)+"+(y+chessman_width/2-1+i*span_y); 상태 _y = i; }}} Chessman Chessman = New Chessman (Black, True); Chessstatus [status_x] [status_y] = Chessman; 리 페인트 (); } 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-regendated method stub} @override public void mouserEleled (mouseevent e) {// todo auto-kenerated method stub} @override public void mouseentered (mouseevent e) {// to auto-goder gended voor-goded mouseexited (mouseevent e) {// todo 자동 생성 메소드 스텁}}기본 모듈 코드는 변경되지 않았습니다
패키지 xchen.test.simplegobang; import java.awt.container; import javax.swing.jframe; xchen.test.simplegobang.drawchessboard 가져 오기; Public Class Main은 Jframe {Private DrawChessboard DrawChessboard; public main () {drawchessboard = New DrawChessboard (); // 프레임 제목 settitle ( "독립형 고지"); 컨테이너 containerpane = getContentPane (); ContainerPane.add (DrawChessboard); } public static void main (String [] args) {main m = new Main (); M.SetSize (800, 800); M.setvisible (true); }}실행하십시오
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.