Java form using the basic (backgammon case)

Java form using the basic (backgammon case)

She was recently seen in the discussion of the form, just nothing to try to write a little about the case to understand

Briefly, the bottom of the form is a container, for the discharge of various components, and a common container having JFrame JDialog two kinds, the JPanel intermediate container, the bottom can be placed in a container, the intermediate container manageable using the basic components. Layout Manager is in the container assembly according to certain rules and the manner in containers. There are several common layout

  1. FlowLayout () flow layout (Panel is the default layout)
  2. GridLayout (int rows, int cols) grid layout
  3. BorderLaout () border layout (JFrame is the default layout)
  4. A CardLayout () card layout
  5. The GridBagLayout () GridBagLayout

Today used mainly border layout, word does not say, look at the code it

  1. Variable definitions
	//表示当前下棋的人,-1代表黑棋,0 下棋结束胜负以分, 1代表白棋
	private static int dir = -1;
	//存放棋盘上的棋子信息,-1代表黑棋,0 代表位子空, 1代表白棋
	private static int chess[][] = null;
	//默认棋盘行数:19
	private static int row = 19;
	//默认棋盘每行格子数:19
	private static int col = 19;
	//这个本来是用来记录鼠标光标位置的,但我比较懒,不想多写别的,
	//这里就用它来记录获胜者五个棋子的起始数组所在下标和结束数组所在下标
	private Point winerStart = null;
	private Point winerEnd = null;
	//用来显示获胜方的,初始文字随便写的
	public JLabel title = new JLabel("观棋不语,落子无悔");
  1. Draw a checkerboard grid (where the data type to use some place to note)

	/**
	 * 因为这里采用的是JPeanl面板作为棋盘的容器,棋盘内容都是同过Graphcis对象绘制的,
	 * 重写了Component里的paintComponent(Graphics g)方法,该方法可以通过对象g绘制初始化界面
	 */
	@Override
	protected void paintComponent(Graphics g) {
		// TODO Auto-generated method stub
		super.paintComponent(g);
		//设置画笔颜色为灰色
		g.setColor(Color.gray);
		//求出当前每个网格的宽间距,不能用整型,不然精度会出问题
		double colSpace = (0.0+this.getWidth())/this.col;
		//求出当前每个网格的高间距,不能用整型,不然精度会出问题
		double rowSpace = (0.0+this.getHeight())/this.row;
		//for循环绘制各个位置的经线纬线
		for(int i = 0; i < this.col; i++ ) {
			g.drawLine((int)(i*colSpace), 0, (int)(i*colSpace), this.getHeight());
		}
		for(int i = 0; i < this.row ; i++) {
			g.drawLine(0, (int)(i*rowSpace), this.getWidth(),  (int)(i*rowSpace));
		}
	}

This is the renderings okay,
Here Insert Picture Description
3. Record the piece, and draw current board Lazi content
generally talk about ideas, we will first draw after a good checkerboard grid, each grid can be seen as a Lazi area, so we use an int type two-dimensional array (Chess [] []) to store information within each zone Zi (-1 means Black, 0 for empty seats, represents White,), we click the mouse cursor position when the form is calculating the current position of the region (the first few lines of columns), corresponding to the target value to a single array of values corresponding to the front side zi like, relatively simple drawing, by the repaint (); zi achieved after each screen board refresh (of course within paintComponent (Graphics g) to draw the pieces together, corresponds to traversing Chess [] [] array, -1 Videos Black, White Videos 1 when drawing board), as we alternate square chess variable determines the like (dir = -1) code is as follows:

These are some of the variables needed


	//表示当前下棋的人
	private static int dir = -1;
	//存放棋盘上的棋子信息,-1代表黑棋,0 代表位子空, 1代表白棋
	private static int chess[][] = null;
	//默认棋盘行数:19
	private static int row = 19;
	//默认棋盘每行格子数:19
	private static int col = 19;

With a constructor parameter

/**
	 * 初始化一个自定义大小的棋盘,每行col各格子,共row行
	 * @param row	一个int类型数据 ,棋盘行数
	 * @param col	一个int类型数据 ,棋盘每行格子数
	 */
	public CheckerBoard(int row, int col) {
		this.row = row;
		this.col = col;
		//初始化棋盘信息
		chess = new int[row][col];
		//定义背景颜色为黑色
		this.setBackground(Color.yellow);
		//给个默认大小
		this.setSize(800,800);
		//添加一个鼠标事件的Listener 
		this.addMouseListener(this);
	}

Lazi record information when clicked


@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		//计算鼠标当前点击的位子是第几列(0代表第一列)
		int col = e.getX()*this.col/this.getWidth();
		//计算鼠标当前点击的位子是第几行(0代表第一行)
		int row = e.getY()*this.row/this.getHeight();
		//判定当前位子是否已经落子了,同时棋局是否进行(dir=0表示棋局已经结算了)
		//有棋子在的话肯定不能在下了,胜负以分也不能再下了
		if(chess[row][col] == 0 && dir != 0) {
			//记录当前位置棋子信息
			chess[row][col] = dir; 
			//下一个落子方
			dir = -dir;
			//重新调用 paintComponent(Graphics g)方法绘制棋盘
			repaint();
		}
		
	}

The following is paintComponent (Graphics g) drawing up the pieces of code

protected void paintComponent(Graphics g) {
		// TODO Auto-generated method stub
		super.paintComponent(g);
		g.setColor(Color.gray);
		double colSpace = (0.0+this.getWidth())/this.col;
		double rowSpace = (0.0+this.getHeight())/this.row;
		for(int i = 0; i < this.col; i++ ) {
			g.drawLine((int)(i*colSpace), 0, (int)(i*colSpace), this.getHeight());
		}
		for(int i = 0; i < this.row ; i++) {
			g.drawLine(0, (int)(i*rowSpace), this.getWidth(),  (int)(i*rowSpace));
		}
		//加了这一截遍历chess[][]的方法
		for (int i = 0; i < this.row; i++) {
			for (int j = 0; j < this.col; j++) {
				//如果大于0(1嘛)
				if (chess[i][j] > 0) {
					//绘画颜色为白色
					g.setColor(Color.WHITE);
					//System.out.println(j*colSpace+","+ i*rowSpace);
					//通过数组行列值计算出绘制坐标,画一个白色的圆 (绘制方法以图像区域左上角为零点)
					g.fillOval((int)(j*colSpace), (int)(i*rowSpace), (int)colSpace, (int)rowSpace);
				}
				if(chess[i][j] < 0) {
					g.setColor(Color.black);
					g.fillOval((int)(j*colSpace), (int)(i*rowSpace), (int)colSpace, (int)rowSpace);
				}
			}
		}
	}

Code results are as follows:

Here Insert Picture Description
4. The next step is to determine the mechanism to win
and see each time there is no win situation Lazi

//L代表左R代表有,(取名难哪)判定同一行有没有获胜情况
private void judgeLR(int row, int col) {
		int coltemp1 = col , coltemp2 = col;
		int rowtemp1 = row, rowtemp2 = row;
		//从当前位置向左找与当前落子相同的连续的最后一次棋子位置
		while(coltemp1 > 0 && chess[rowtemp1][coltemp1-1] == dir)
			{
				coltemp1--;
			}
		//从当前位置向左找与当前落子相同的连续的最后一次棋子位置
		while(coltemp2 < this.col-1 && chess[rowtemp2][coltemp2+1] == dir)
			{
				coltemp2++;
			}
		//如果连续相同的数目大于5个,即胜负以分
		if(coltemp2 - coltemp1 >= 4){
			//记录连续相同的起始棋子坐标
			winerStart = new Point(coltemp1, rowtemp1);
			//记录连续相同的结束棋子坐标
			winerEnd = new Point(coltemp2, rowtemp2);
		}
	}

Ah, a position just above there are several other position not listed here, I have a file upload source

  1. After winning always want a little show, we will look at it, not preceded by the same start recording a continuous piece and record the coordinates of the end of the same continuous piece coordinates Well, draw red lines to link these two points, plus or place. . . . . paintComponent (Graphics g) method, as follows:
protected void paintComponent(Graphics g) {
		// TODO Auto-generated method stub
		super.paintComponent(g);
		g.setColor(Color.gray);
		double colSpace = (0.0+this.getWidth())/this.col;
		double rowSpace = (0.0+this.getHeight())/this.row;
		for(int i = 0; i < this.col; i++ ) {
			g.drawLine((int)(i*colSpace), 0, (int)(i*colSpace), this.getHeight());
		}
		for(int i = 0; i < this.row ; i++) {
			g.drawLine(0, (int)(i*rowSpace), this.getWidth(),  (int)(i*rowSpace));
		}
		for (int i = 0; i < this.row; i++) {
			for (int j = 0; j < this.col; j++) {
				if (chess[i][j] > 0) {
					g.setColor(Color.WHITE);
					//System.out.println(j*colSpace+","+ i*rowSpace);
					g.fillOval((int)(j*colSpace), (int)(i*rowSpace), (int)colSpace, (int)rowSpace);
				}
				if(chess[i][j] < 0) {
					g.setColor(Color.black);
					g.fillOval((int)(j*colSpace), (int)(i*rowSpace), (int)colSpace, (int)rowSpace);
				}
			}
		}
		//多了这段(如果起始和结束都有记录的坐标点,就证明分出胜负了)
		if(winerStart!=null && winerEnd != null) {
			//划红线嘛,当然要设置成红色,线条的宽度你可以自己调一下,这里不多演示(代码太长了)
			g.setColor(Color.RED);
			//说出来你别不信,这就是为了画一条直线
			g.drawLine(
					/*根据起始坐标所在列求出线段开始端点的x坐标,
					*(+ (int)colSpace/2主要是为了让线段端点在表格中心)
					*/
					(int)(winerStart.x*colSpace) + (int)colSpace/2,
					/*根据起始坐标所在行求出线段开始端点的y坐标,
					*(+ (int)rowSpace/2主要是为了让线段端点在表格中心)
					*/
					(int)(winerStart.y*rowSpace) + (int)rowSpace/2,
					/*根据结束坐标所在列求出线段结束端点的x坐标,
					*(+ (int)colSpace/2主要是为了让线段端点在表格中心)
					*/
					(int)(winerEnd.x*colSpace) + (int)colSpace/2,
					/*根据结束坐标所在行求出线段结束端点的y坐标,
					*(+ (int)rowSpace/2主要是为了让线段端点在表格中心)
					*/ 
					(int)(winerEnd.y*rowSpace) + (int)rowSpace/2
			);
			if(chess[winerStart.y][winerStart.x] == -1) {
				title.setText("黑棋获胜");
			}else {
				title.setText("白棋获胜");
			}
			this.dir = 0;
		}
	}

This code is written about it, I believe there are some small problems wit you certainly do understand the source after

This is the last operating results (of wood have to see Flanagan glare of the red line):
Here Insert Picture Description

Released nine original articles · won praise 18 · views 5966

Guess you like

Origin blog.csdn.net/LioTomcat/article/details/104576283