1.界面的预处理与刷新
我们要绘制五子棋棋盘,首先必须明确一点:不管我们在棋盘上怎么操作,棋盘的网格线是不可能改变的。无论是下棋,悔棋,网格线都会在那。故而我们应该知道,要想网格线始终存在,我们需要在后台存在一个绘制代码,一直不断的画这些网格线,同样在这一个代码区将来我们下了棋子之后也要不断的在下棋的位置画上棋子。
那么这样的代码区在哪呢?我们知道我们绘制界面的大框架是JFrame,其他的组件不断的往JFrame里加,我们查看调用可以发现,这些组件包括JFrame都继承于同一个类Component,而在这个类中正好就有我们所要找的代码区,也就是函数paint(Graphics g)与repaint()。
paint()是提供给用户编程的,往往声明在接口之中,然后用户实现该接口,以拥有重绘的功能。若要定制某个图形组件的界面,可以重写paint()方法,记得一般习惯这样改写:
void paint(Graphics g){
super.paint(g);
// your code
}
而repaint是刷新,里面不光有调用paint()函数刷新背景,也会刷新该组件上的所有组件。当对于界面或组件执行了某类操作,改变了界面上的图象时,jvm收到界面被调整的信息,此时会调用visible是ture的图形组