第一步:新建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 }