JAVA面向对象.docx
《JAVA面向对象.docx》由会员分享,可在线阅读,更多相关《JAVA面向对象.docx(43页珍藏版)》请在冰点文库上搜索。
JAVA面向对象
JavaOOPDay03
Top
1.贪吃虫游戏——定义Cell类,封装网格
2.贪吃虫游戏——随机生成Food与绘制Food
3.贪吃虫游戏——构建可爬行Worm
4.贪吃虫游戏——实现自动爬行Worm
1贪吃虫游戏——定义Cell类,封装网格
1.1问题
定义类型用于创建贪吃虫的单元格对象以及食物对象,贪吃虫和食物的逻辑关系如图12:
图12
1.2方案
声明类Cell表示单元类型,用于创建对象表示贪吃虫的节点或者食物节点.Cell包含x,y属性表示在舞台上的坐标位置.并且提供构造器方便创建初始化实例,提供属性访问方法,和便于调试的toString方法.
1.3实现
创建类Cell表示单元格类型:
1.publicclassCell{
2.}
在类Cell的类体中添加属性x,y和构造器
1. privateintx;
2. privateinty;
3.
4. publicCell(){
5. }
6.
7. publicCell(intx,inty){
8. this.x=x;
9. this.y=y;
10. }
11. /**复制构造器*/
12. publicCell(Cellcell){
13. this(cell.x,cell.y);
14. }
15.
16. publicintgetX(){
17. returnx;
18. }
在类Cell的类体中添加属性的访问方法
1. publicintgetX(){
2. returnx;
3. }
4.
5. publicvoidsetX(intx){
6. this.x=x;
7. }
8.
9. publicintgetY(){
10. returny;
11. }
12.
13. publicvoidsetY(inty){
14. this.y=y;
15. }
在类Cell的类体中添加toString方法便于调试输出
1. publicStringtoString(){
2. returnx+","+y;
3. }
创建TestCell,添加main方法,在面方法中添加测试代码,创建并输出对象:
1.publicclassTestCell{
2. publicstaticvoidmain(String[]args){
3. Cellhead=newCell(12,8);
4. System.out.println(head);
5. Cellfood=newCell(30,29);
6. System.out.println(food);
7. }
8.}
执行测试结果:
1.12,8
2.30,29
完整的案例参考代码:
1.publicclassCell{
2. privateintx;
3. privateinty;
4.
5. publicCell(){
6. }
7.
8. publicCell(intx,inty){
9. this.x=x;
10. this.y=y;
11. }
12. /**复制构造器*/
13. publicCell(Cellcell){
14. this(cell.x,cell.y);
15. }
16.
17. publicintgetX(){
18. returnx;
19. }
20.
21. publicvoidsetX(intx){
22. this.x=x;
23. }
24.
25. publicintgetY(){
26. returny;
27. }
28.
29. publicvoidsetY(inty){
30. this.y=y;
31. }
32.
33. publicStringtoString(){
34. returnx+","+y;
35. }
36.}
37.
38.publicclassTestCell{
39. publicstaticvoidmain(String[]args){
40. Cellhead=newCell(12,8);
41. System.out.println(head);
42. Cellfood=newCell(30,29);
43. System.out.println(food);
44. }
45.}
1.4扩展
2贪吃虫游戏——随机生成Food与绘制Food
2.1问题
解决随机生成食物问题.在贪吃虫业务功能中每当食物被虫吃掉,就需要重新生成一颗食物.
创建贪吃虫的舞台,并在舞台上绘制随机生成的食物.
2.2方案
利用随机数生成食物的坐标位置,创建食物对象.
创建贪吃虫舞台,用于显示贪吃虫和食物,舞台继承于JPanel,并且覆盖paint方法用于实现绘制食物图形功能.
2.3实现
创建类WormStage作为蛇的舞台
1.publicclassWormStageextendsJPanel{
2.
3.}
添加常量和构造器,用于创建舞台对象,初始化舞台大小.
1. /**节点的大小,以绘制像数为单位*/
2. publicstaticfinalintCELL_SIZE=10;
3. /**舞台单元格的列数*/
4. publicstaticfinalintCELL_COLS=35;
5. /**舞台单元格的行数*/
6. publicstaticfinalintCELL_ROWS=35;
7.
8. publicWormStage(){
9. //设置舞台的大小
10. setSize(CELL_COLS*CELL_SIZE,CELL_ROWS*CELL_SIZE);
11. }
添加成员变量food代表食物的引用,添加方法randomFood()用于随机生成食物.
1. privateCellfood;
2.
3. privateCellrandomFood(){
4. intx;
5. inty;
6.
7. x=RandomUtils.nextInt(CELL_COLS);
8. y=RandomUtils.nextInt(CELL_ROWS);
9. returnnewCell(x,y);
10. }
重写绘制方法paint(),添加食物的绘制语句:
1. publicvoidpaint(Graphicsg){
2. g.setColor(newColor(35,31,32));
3. g.fillRect(0,0,getWidth(),getHeight());
4.
5. if(food!
=null){
6. g.setColor(newColor(255,192,0));
7. g.fillRect(food.getX()*WormStage.CELL_SIZE,
8. food.getY() *WormStage.CELL_SIZE,
9. WormStage.CELL_SIZE,
10. WormStage.CELL_SIZE);
11. g.setColor(newColor(35,31,32));
12. g.drawRect(food.getX()*WormStage.CELL_SIZE,
13. food.getY() *WormStage.CELL_SIZE,
14. WormStage.CELL_SIZE,
15. WormStage.CELL_SIZE);
16. }
17. }
添加action方法用于监听控制台,每次控制器输入一个字符就重新生成一次食物.
1. publicvoidaction(){
2. Scannerscanner=newScanner(System.in);
3. food=randomFood();
4. while(true){
5. scanner.next();//等待控制台的输入
6. food=randomFood();
7. repaint();//重绘界面,显示食物
8. }
9. }
添加main方法,创建舞台实例,启动action方法.
1. publicstaticvoidmain(String[]args){
2. JFramemain=newJFrame();
3. main.setSize(460,470);
4. main.setTitle("Worm");
5. Dimensiond=Toolkit.getDefaultToolkit().getScreenSize();
6. main.setLocation((d.width-main.getWidth())/2,
7. (d.height-main.getHeight())/2);
8. main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
9. main.setResizable(false);
10.
11. main.setLayout(null);
12. WormStagewormStage=newWormStage();
13. main.setVisible(true);
14.
15. intw=main.getContentPane().getWidth();
16. inth=main.getContentPane().getHeight();
17. wormStage.setLocation((w-wormStage.getWidth())/2,
18. (h-wormStage.getHeight())/2);
19. main.getContentPane().add(wormStage);
20.
21. wormStage.action();
22. }
执行结果如图13
图13
完整的代码参考(不包含Cell类,Cell类参考前面章节):
1.publicclassWormStageextendsJPanel{
2. /**节点的大小,以绘制像数为单位*/
3. publicstaticfinalintCELL_SIZE=10;
4. /**舞台单元格的列数*/
5. publicstaticfinalintCELL_COLS=35;
6. /**舞台单元格的行数*/
7. publicstaticfinalintCELL_ROWS=35;
8.
9. privateCellfood;
10.
11. publicWormStage(){
12. //设置舞台的大小
13. setSize(CELL_COLS*CELL_SIZE,CELL_ROWS*CELL_SIZE);
14. }
15.
16. publicvoidaction(){
17. Scannerscanner=newScanner(System.in);
18. food=randomFood();
19. while(true){
20. scanner.next();
21. food=randomFood();
22. repaint();
23. }
24. }
25.
26. privateCellrandomFood(){
27. intx;
28. inty;
29.
30. x=RandomUtils.nextInt(CELL_COLS);
31. y=RandomUtils.nextInt(CELL_ROWS);
32. returnnewCell(x,y);
33. }
34.
35. publicvoidpaint(Graphicsg){
36. g.setColor(newColor(35,31,32));
37. g.fillRect(0,0,getWidth(),getHeight());
38.
39. if(food!
=null){
40. g.setColor(newColor(255,192,0));
41. g.fillRect(food.getX()*WormStage.CELL_SIZE,
42. food.getY() *WormStage.CELL_SIZE,
43. WormStage.CELL_SIZE,
44. WormStage.CELL_SIZE);
45. g.setColor(newColor(35,31,32));
46. g.drawRect(food.getX()*WormStage.CELL_SIZE,
47. food.getY() *WormStage.CELL_SIZE,
48. WormStage.CELL_SIZE,
49. WormStage.CELL_SIZE);
50. }
51.
52. }
53.
54. publicstaticvoidmain(String[]args){
55. JFramemain=newJFrame();
56. main.setSize(460,470);
57. main.setTitle("Worm");
58. Dimensiond=Toolkit.getDefaultToolkit().getScreenSize();
59. main.setLocation((d.width-main.getWidth())/2,
60. (d.height-main.getHeight())/2);
61. main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
62. main.setResizable(false);
63.
64. main.setLayout(null);
65. WormStagewormStage=newWormStage();
66. main.setVisible(true);
67.
68. intw=main.getContentPane().getWidth();
69. inth=main.getContentPane().getHeight();
70. wormStage.setLocation((w-wormStage.getWidth())/2,
71. (h-wormStage.getHeight())/2);
72. main.getContentPane().add(wormStage);
73.
74. wormStage.action();
75. }
76.}
2.4扩展
在Cell类中添加paint方法,封装节点的绘制过程,简化WormStage类paint方法代码.
在Cell类中添加paint方法:
1. publicvoidpaint(Graphicsg,Colorb,Colorf){
2. g.setColor(b);
3. g.fillRect(x*WormStage.CELL_SIZE,y*WormStage.CELL_SIZE,
4. WormStage.CELL_SIZE,WormStage.CELL_SIZE);
5. g.setColor(f);
6. g.drawRect(x*WormStage.CELL_SIZE,y*WormStage.CELL_SIZE,
7. WormStage.CELL_SIZE,WormStage.CELL_SIZE);
8. }
重构的WormStage类的paint方法:
1. publicvoidpaint(Graphicsg){
2. g.setColor(newColor(35,31,32));
3. g.fillRect(0,0,getWidth(),getHeight());
4.
5. if(food!
=null){
6. food.paint(g,newColor(255,192,0),
7. newColor(35,31,32));
8. }
9. }
3贪吃虫游戏——构建可爬行Worm
3.1问题
构建贪吃虫类型实例,并且能够用控制台控制虫的爬行.
图14
3.2方案
定义类Worm表示贪吃类型,贪吃虫包含多个Cell节点.
1.publicclassWorm{
2. /**节点数组*/
3. privateCell[]cells;
4.}
在Worm构造器中创建默认的贪吃虫
1. publicWorm(){
2. //WormStage.WORM_LEN默认贪吃虫的长度
3. cells=newCell[WormStage.WORM_LEN];
4. for(inti=0;i5. cells[i]=newCell(i,0);
6. }
7. }
贪吃虫的爬行原理:
删除最后一个节点,插入新的头结点,如图15
图15
爬行算法:
1. /**
2. *爬行(creep)方法,爬行方法:
移除末尾节点,增加头节点
3. *@paramdirection爬行方向
4. */
5. publicvoidcreep(intdirection){
6. if(direction+currentDirection!
=0){//方向不能相反
7. //更换方向
8. currentDirection=direction;
9. CellnewHead=newHead(direction);
10. if(newHead!
=null){
11. //移除末尾节点
12. for(inti=cells.length-1;i>=1;i--){
13. cells[i