이 기사는 참조를 위해 Java로 Gozi Chess 게임 보드를 그리는 방법을 공유합니다. 특정 내용은 다음과 같습니다
바둑판 모듈 :
Gozi Chess 보드 그리기 : 19 수평선, 19 세로 선
1 단계 : 체스 판을 표시하십시오
Root Directory/Res/Drawable/Chessboard/PNG에 위치한 Chessboard.png라는 체스 보드가 있으며 이제이 이미지를 표시하고 싶습니다.
Drawchessboard.java
패키지 xchen.test.simplegobang; java.awt.graphics 가져 오기; import java.awt.image; java.awt.toolkit import; import javax.swing.jpanel; Public Class DrawChessboard 확장 jpanel {public image boardimg; public drawchessboard () {boardimg = toolkit.getDefaultToolKit (). getImage ( "res/drawable/chessboard.png"); if (boardimg == null) system.err.println ( "png는 존재하지 않습니다"); } @Override Protected Void PaintComponent (Graphics G) {// TODO 자동 생성 메소드 스터브 SUPER.PAINTCOMPONTE (g); int imgwidth = boardimg.getWidth (this); int imgheight = boardimg.getheight (this); int fwidth = getWidth (); int fheight = getheight (); int x = (fwidth-imgwidth)/2; int y = (fheight-Imgheight)/2; G.DrawImage (Boardimg, X, Y, NULL); }} 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.setvisible (true); }}실행하십시오
2 단계 : 체스 보드의 수평 및 수직 선을 그리십시오.
Drawchessboard.java
패키지 xchen.test.simplegobang; java.awt.graphics 가져 오기; import java.awt.image; java.awt.toolkit import; import javax.swing.jpanel; Public Class DrawChessboard 확장 jpanel {public image boardimg; 최종 개인 int 행 = 19; public drawchessboard () {boardimg = toolkit.getDefaultToolKit (). getImage ( "res/drawable/chessboard2.png"); if (boardimg == null) system.err.println ( "png는 존재하지 않습니다"); } @Override Protected Void PaintComponent (Graphics G) {// TODO 자동 생성 메소드 스터브 SUPER.PAINTCOMPONTE (g); int imgwidth = boardimg.getWidth (this); int imgheight = boardimg.getheight (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)에 대한 수직 선을 그립니다. }}}Main.java는 변경되지 않습니다
발생하는 문제 :
1) Eclipse는 폴더 아래의 그림을 인식하지 못합니다
문제 : 폴더에 사진이 있지만 Eclipse Project Bar에는 표시되지 않습니다.
솔루션 : Eclipse에서 루트 디렉토리, F5 새로 고침을 선택하면 표시됩니다.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.