第一步:建立java項目,具體的命名,看下面的檔案結構。
第二步:代碼區
DrawChessBoard類:
1 package com.hp.chenyanlong;
2
3 import java.awt.Graphics;
4 import java.awt.Image;
5 import java.awt.Toolkit;
6
7 import javax.swing.JPanel;
8
9 /**
10 * 類說明 :DrawChessBoard 類
11 * @author 作者 : chenyanlong
12 * @version 建立時間:2017年10月27日
13 */
14 public class DrawChessBoard extends JPanel {
15
16 public Image boardImg;//抽象類 Image 是表示圖形圖像的所有類的超類。
17
18 //載入圖檔
19 public DrawChessBoard() {
20
21 boardImg = Toolkit.getDefaultToolkit().getImage("res/drawable/chessboard.png");//擷取圖檔
22 if (boardImg == null) {
23 System.out.println("png do not exit");
24 }
25 }
26
27 //重寫
28 @Override
29 protected void paintComponent(Graphics g){
30
31 super.paintComponent(g);
32
33 int imgWidth=boardImg.getWidth(this);//圖檔的寬度
34 int imgHeight=boardImg.getHeight(this);//圖檔的高度
35
36 int FWidth=getWidth();//容器的寬度
37 int FHeight=getHeight();//容器的高度
38
39 int x=(FWidth-imgWidth)/2;
40 int y=(FHeight-imgHeight)/2;
41 g.drawImage(boardImg,x,y,null);//畫出圖檔
42 }
43 }
Main類:
1 package com.hp.chenyanlong;
2 import java.awt.Container;
3 import javax.swing.JFrame;
4
5 import com.hp.chenyanlong.DrawChessBoard;
6 /**
7 * 類說明 :Main
8 * @author 作者 : chenyanlong
9 * @version 建立時間:2017年10月27日
10 */
11 public class Main extends JFrame {
12
13 private DrawChessBoard drawChessBoard;//設定私有話drawChessBoard
14
15 //構造方法
16 public Main() {
17 //執行個體化對象DrawChessBoard
18 drawChessBoard = new DrawChessBoard();
19
20 //Frame标題
21 setTitle("五子棋--@yanlong");
22
23 //加入到容器中
24 Container containerPane =getContentPane();
25 containerPane.add(drawChessBoard); //将drawChessBoard元件添加到容器
26 }
27
28 //主方法
29 public static void main(String[] args) {
30 Main m = new Main();
31 m.setVisible(true);
32 }
33
34 }
第三步:運作效果
第四步:為棋盤畫上橫豎線
重新寫了DrawChessBoard代碼:
1 package com.hp.chenyanlong;
2
3 import java.awt.Graphics;
4 import java.awt.Image;
5 import java.awt.Toolkit;
6
7 import javax.swing.JPanel;
8
9 /**
10 * 類說明 :DrawChessBoard 類
11 * @author 作者 : chenyanlong
12 * @version 建立時間:2017年10月27日
13 */
14 public class DrawChessBoard extends JPanel {
15
16 public Image boardImg;//抽象類 Image 是表示圖形圖像的所有類的超類。
17 final private int ROWS=19;//設定19行
18
19 //載入圖檔
20 public DrawChessBoard() {
21
22 boardImg = Toolkit.getDefaultToolkit().getImage("res/drawable/chessboard.png");//擷取圖檔
23 if (boardImg == null) {
24 System.out.println("png do not exit");
25 }
26 }
27
28 //重寫
29 @Override
30 protected void paintComponent(Graphics g){
31
32 super.paintComponent(g);
33
34 int imgWidth=boardImg.getWidth(this);//圖檔的寬度
35 int imgHeight=boardImg.getHeight(this);//圖檔的高度
36
37 int FWidth=getWidth();//容器的寬度
38 int FHeight=getHeight();//容器的高度
39
40 int x=(FWidth-imgWidth)/2;
41 int y=(FHeight-imgHeight)/2;
42 g.drawImage(boardImg,x,y,null);//畫出圖檔
43
44 int margin=x;
45 int span_x=imgWidth/ROWS;//單元格的寬度
46 int span_y=imgHeight/ROWS;//單元格的高度
47 //畫橫線
48 for(int i=0;i<ROWS;i++){
49 g.drawLine(x, y+i*span_y,FWidth-x,y+i*span_y);
50 }
51 //畫豎線
52 for(int i=0;i<ROWS;i++)
53 {
54 g.drawLine(x+i*span_x, y, x+i*span_x,FHeight-y);
55 }
56 }
57 }