이전 기사는 Java가 두 개의 Gozi Chess 게임을 구현하는 것에 대해 이야기합니다 (2) Chessboard를 그리며 체스 판이 그려졌습니다. 다음으로, 우리는 주요 함수 인 제어 기능을 구현해야합니다.
1) 체스 조각을 선택하십시오
2) 체스 조각을 그립니다
3) 승자를 판단하십시오
4) 교환 체스
체스 조각의 일부를 먼저 깨달으십시오
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
먼저 체스 조각 클래스를 정의하십시오. 이 클래스는 조각의 색상 (0- 검은 색 및 1- 흰색을 의미)과 움직일지 여부 (체스 조각의 턴 정보를 저장하기 위해 2 차원 배열을 사용할 계획)의 두 가지 속성이 있습니다.
Chessman.java
패키지 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; }} 그런 다음 이전 부분에서 체스 판의 코드 부분을 그렸고 체스 조각을 그리는 코드를 추가했습니다. 나는 체스 조각을 그리기위한 코드를 테스트하기 위해 체스 판에 위치한 두 개의 체스 조각 (흰색과 하나의 검은 색 1 개)을 사용했습니다.
Drawchessboard.java
패키지 xchen.test.simplegobang; java.awt.graphics 가져 오기; import java.awt.graphics2d; import java.awt.radialgradientPaint; import java.awt.image; java.awt.toolkit import; import java.awt.color; import javax.swing.jpanel; Public Class DrawChessboard 확장 jpanel {Final Static int black = 0; 최종 정적 int 화이트 = 1; 공개 int chesscolor = 검은 색; 공개 이미지 Boardimg; 최종 개인 int 행 = 19; Chessman [] [] Chessstatus = New Chessman [행] [행]; public drawchessboard () {boardimg = toolkit.getDefaultToolKit (). getImage ( "res/drawable/chessboard2.png"); if (boardimg == null) system.err.println ( "png는 존재하지 않습니다"); // 테스트 드로우 체스만 부분 간단한 체스만 체스만 = 새로운 체스 맨 (0, true); Chessstatus [7] [7] = Chessman; Chessman Chessman2 = New Chessman (1, True); Chessstatus [8] [8] = Chessman2; // Test Draw Chessman Part Simple} @override Protected Void Paintcomponent (Graphics G) {// todo 자동 생성 메소드 스텁 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; G.DrawImage (Boardimg, X, Y, NULL); int margin = x; int span_x = imgwidth/rows; int span_y = imgheight/rows; // (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; i ++) {for (int j = 0; j <rows; j ++) {if (chessstatus [i] [j]! = null && chessstatus [i] [j] .getplaced () == true) {system.out.println ( "draw chessman"+i+j); int pos_x = x+i*span_x; int pos_y = y+j*span_y; int chessman_width = 20; float radius_b = 20; float radius_w = 50; 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); }}}}}}}}} 기본 모듈 코드는 변경되지 않았습니다
Main.java
패키지 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을 더 지원하기를 바랍니다.