gamepane.java

来自「一个CS模式的网络五子棋游戏,用JAVA写的,源码版式不好,也共享出来,希望大家」· Java 代码 · 共 134 行

JAVA
134
字号
import javax.swing.*;
import java.awt.*;

public class GamePane extends JPanel
{
	private Game game;
	public GamePane(Game g)
	{
		GameControler.getGamePaneInstance(this);
		game = g;
	}
	public void paintChessBoard(Graphics g)
	{
		int index;
		Dimension dm = new Dimension();
		int lim = game.getBoardSize();
		this.getSize(dm);
		int size = dm.width/lim;
		g.setColor(Color.blue);
		
		for(index=0; index<=lim; index++)
		{
			int xy = index*size;
			g.drawLine(xy, 0, xy, dm.width);
			g.drawLine(0, xy, dm.width, xy);
		}
	}
	
	public void paintChess(Graphics g, int row, int col)
	{
		Dimension dm = new Dimension();
		int lim = game.getBoardSize();
		this.getSize(dm);
		int size = dm.width/lim;
		int dr = size*2/3;
		int or = (size - dr)/2;
		int ox = col*size + or;
		int oy = row*size + or;
		int state = game.getBoardState(row, col);
		
		if(state == 1)
		{
			g.setColor(Color.red);
			g.drawOval(ox, oy, dr, dr);
			g.fillOval(ox, oy, dr, dr);
		}
		else if(state == -1)
		{
			g.setColor(Color.blue);
			g.drawOval(ox, oy, dr, dr);
			g.fillOval(ox, oy, dr, dr);
		}
	}
	
	public void paintAllChesses(Graphics g)
	{
		int row,col;
		int lim = game.getBoardSize();
		for(row=0; row<lim; row++)
		{
			for(col=0; col<lim; col++)
			{
				paintChess(g,row,col);
			}
		}
	}
	
	public void paint(Graphics g)
	{
		super.paint(g);
		paintChessBoard(g);
		DrawCurPos(g);
		paintAllChesses(g);
	}
	
	public int getCol(int x)
	{
		Dimension dm = new Dimension();
		int lim = game.getBoardSize();
		this.getSize(dm);
		int size = dm.width/lim;
		return x/size;
	}
	
	public int getRow(int y)
	{
		Dimension dm = new Dimension();
		int lim = game.getBoardSize();
		this.getSize(dm);
		int size = dm.width/lim;
		return y/size;
	}
	
	public void repaintChess(int row, int col)
	{
		Dimension dm = new Dimension();
		int lim = game.getBoardSize();
		this.getSize(dm);
		int size = dm.width/lim;
		int x = col * size;
		int y = row * size;
		repaint(x, y, size, size);
	}
	
	public void DrawCurPos(Graphics g)
	{
		Dimension dm = new Dimension();
		int lim = game.getBoardSize();
		this.getSize(dm);
		int size = dm.width/lim;
		int x = Game.curCol * size;
		int y = Game.curRow * size;
		
		g.setColor(Color.cyan);
//		g.drawRect(x, y, size, size);
		g.fillRect(x+1, y+1, size-1, size-1);
	}
	
	public void repaintCurPos()
	{
		int lim = game.getBoardSize();
		if(Game.oldCol>=0&&Game.oldCol<lim
				&&Game.oldRow>=0&&Game.oldRow<lim)
		{
			repaintChess(Game.oldRow,Game.oldCol);
		}
		if(Game.curCol>=0&&Game.curCol<lim
				&&Game.curRow>=0&&Game.curRow<lim)
		{
			repaintChess(Game.curRow,Game.curCol);
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?