随记—java学习
      大学两年匆匆流过,学的东西很多但好像又什么都没学到。曾想安安静静的学点东西,但发现好难,因为不是我一个人在生活,身边的人、身边的事,无时无刻的在影响着我,左右着我。这并不是我想要的生活,曾今想在大学做一些自己喜欢的时就可以了,理想是丰满的,现实太骨干,有的事太浮夸了,跟我想的背道而驰。现在想静下心来学点东西发现确实不容易,就Java来说刚开始只是在书本上看感觉挺简单的,只是能看懂,但要是自己敲起来,一大堆bug,最近一段时间跟着斌哥上了几节课后发现一门语言的学习仅仅靠看书是远远不够的,还要自己多实践,也就是在理解的基础之上自己要能敲得出来,斌哥一节课能讲明白的东西可能我花一周时间也不一定能理解的那么透彻,就比如五子棋,通过对象的调用让我更加清楚地理解了面向对象的感念(之前一直不理解面向过程和面向对象的区别),并且理解了类与对象的关系,对象与方法的关系。还教我们平时敲代码时的细节,很重要,但以前从没想过。这里要特别感谢斌哥,谢谢斌哥的耐心指导,好长时间不写东西,有点不会写了,以下是人人版五子棋代码:


import java.util.ArrayList; 

 import java.awt.*; 

 import java.awt.event.*; 



 import javax.swing.*; 



 public class Hhesses { 

// 定义全局变量 JFrame frame = new JFrame(); 

// JFrame frame = new JFrame(); 

Graphics g; // 队组建和图像进项绘制 

boolean colorFlag = true; // 定义布尔类型区分颜色 

boolean Flag = false; 

boolean beganlistener=false; 

int[][] chessclass = new int[22][23]; // 定义二维数组储存棋子信息 

      Color color; 

      ArrayList list = new ArrayList(); 

// boolean Flag=true; 

public void fivechesses() { // 定义一个五子棋方法 

JFrame frame = new JFrame(); // 定义一个窗体 

// 加入鼠标监听 

MouseListener listener = new MouseListener() { 



public void mouseClicked(MouseEvent e) { // 



if (Flag == true) { 

 

int x = e.getX(); 

int y = e.getY(); 



// 获取交叉点坐标 

for (int i = 0; i < 22; i++) { 

for (int j = 0; j <23; j++) { 

int x0 = 20 * (j+1); 

int y0 = 20 * (i+1); 

// 判断误差范围 

if (x > x0 - 20 / 3 && x < x0 + 20 / 3 

&& y > y0 - 20 / 3 && y < y0 + 20 / 3) { 

// 判断此位置是否有棋子 

if (chessclass[i][j] == 0) { // 判断是否为空 

if (colorFlag == true) { 

g.setColor(Color.white); // 设置颜色为白色 

chessclass[i][j] = 1; 

colorFlag = false; 



} else if (colorFlag == false) { 

g.setColor(Color.black); 

chessclass[i][j] = 2; 

colorFlag = true; 

} 



//g.fillOval(x0 - 9, y0 - 9, 18, 18); 

// printArray(chessclass); 

// System.out.println(); 

//  绘制棋子 

Chess chess = new Chess( x0 - 9,  y0 - 9, color); 

chess.drawChess(g);     //画棋子 

list.add(chess);   //保存棋子信息 

System.out.print("容器的大小"+list.size()); 
  

win(i, j); 

} 

} 

} 

} 

} else if (Flag==false)  { 

JOptionPane.showMessageDialog(null,"请开始!"); 

} 

} 



public void mousePressed(MouseEvent e) { 

// TODO 自动生成的方法存根 



} 



@Override 

public void mouseReleased(MouseEvent e) { 

// TODO 自动生成的方法存根 



} 



@Override 

public void mouseEntered(MouseEvent e) { 

// TODO 自动生成的方法存根 



} 



@Override 

public void mouseExited(MouseEvent e) { 

// TODO 自动生成的方法存根 



} 

}; 

frame.addMouseListener(listener); 

frame.setTitle("五子棋小游戏"); // 设置窗体标题 

frame.setSize(600, 500); // 设置窗体大小 

frame.setLocationRelativeTo(null); // 设置窗体居中 

frame.setDefaultCloseOperation(3); // 关闭窗口 

FlowLayout layout = new FlowLayout(); // 设置流式布局 

frame.setLayout(layout); 

// 讲窗口分成两块 

 final JPanel chesspanel = new JPanel() { // 设置棋盘区 

/** 

*  

*/ 

private static final long serialVersionUID = -6882750265960641876L; 



// 绘制棋盘 

public void paint(Graphics g) { 

super.paint(g); 

// 绘制棋盘 

for (int i = 0; i < 22; i++) { 

g.drawLine(20 * (i+1), 20, 20 * (i+1), 440); 

} 

for (int i = 0; i < 23; i++) { 

g.drawLine(20, 20 * (i+1), 440, 20 * (i+1)); 

} 

//棋子的重绘   从容器中取出所有棋子对象 ,然后绘制 

for(int i =1;i<=list.size();i++)   { 

Chess chess = (Chess)list.get(i); 

chess.drawChess(g); 

} 

 

 

} 

}; 



chesspanel.setBackground(Color.red); // 设置颜色 

JPanel actionpanel = new JPanel(); // 设置功能区 

JButton button1 = new JButton("开始");        

JButton button2 = new JButton("托管"); 

JButton button3 = new JButton("单机"); 

JButton button4 = new JButton("悔棋"); 

JButton button5 = new JButton("退出"); 
  

 

chesspanel.setBackground(Color.red); // 设置颜色 

actionpanel.setBackground(Color.blue); 

actionpanel.setPreferredSize(new Dimension(100, 450)); // 设置功能区大小 

chesspanel.setPreferredSize(new Dimension(450, 450)); // 设置棋盘区大小 

frame.add(chesspanel); 

actionpanel.add(button1); 

actionpanel.add(button2); 

actionpanel.add(button3); 

actionpanel.add(button4); 

actionpanel.add(button5); 

frame.add(actionpanel); 

ActionListener actionlistener = new ActionListener() { 

public void actionPerformed(ActionEvent e) { 

String rocord  = e.getActionCommand(); 

                   switch(rocord)  { 

                   case "开始" : 

                 
 beganlistener= true; 

                   if (!Flag) { 
 

                 
 Flag = true; 

   
 } 

                   break; 

                   case "托管" : 

                       beganlistener = true; 

                       JOptionPane.showMessageDialog(null, "等一会吧"); 

                       break; 

                   case "人人" : 

                 
 beganlistener= true; 

                       break; 

                   case "悔棋" : 

                 
 beganlistener = true; 

                 
 //删除最后一个元素对象 

                 
 Chess lastchess = (Chess)list.get(list.size()-1); 

                 
 Color lastcolor = lastchess.color; 

                 
    list.remove(list.size()-1); 

                 
    //强制重绘 

                 
chesspanel.repaint(); 

                 
 break; 

                           //   

                   case "退出" : 

                 
 beganlistener = true; 

                 
 System.exit(0); 

                       break; 

                   } 

} 

}; 

 

button1.addActionListener(actionlistener); 

button2.addActionListener(actionlistener); 

button3.addActionListener(actionlistener); 

button4.addActionListener(actionlistener); 

button5.addActionListener(actionlistener); 

chesspanel.addMouseListener(listener); 

frame.setVisible(colorFlag); // 设置窗体可见 

// g=actionpanel.getGraphics(); 

g = chesspanel.getGraphics(); 

} 



// 判断输赢方法 

public void win(int i, int j) { 

int count = 1; // 计数器 统计相同棋子 个数 

int m = 1; // 改变下标 

// 横向 向左 

while (j - m >= 0 && chessclass[i][j] == chessclass[i][j - m]) { 

count++; 

m++; 

} 

m = 1; // 重置 m向右 

while (j + m < 23 && chessclass[i][j] == chessclass[i][j + m]) { 

count++; 

m++; 

} 

//纵向 判断 

while (i - m >= 0 && chessclass[i][j] == chessclass[i - m][j]) { 

count++; 

m++; 

} 

m = 1; // 重置m为一 

while (i + m < 22 && chessclass[i][j] == chessclass[i + m][j]) { 

count++; 

m++; 

} 

// 判断斜向 

while (j - m >= 0 && i - m >=0 

&& chessclass[i][j] == chessclass[i - m][j - m]) { 

count++; 

m++; 

} 

m = 1;// 重置m 

while (i + m < 21 && j + m < 22&& chessclass[i][j] == chessclass[i + m][j + m]) { 

count++; 

m++; 

} 

if (count >= 5 && chessclass[i][j] == 1) { 

JOptionPane.showMessageDialog(null, "白棋赢!"); 

} 

if (count >= 5 && chessclass[i][j] == 2) { 

JOptionPane.showMessageDialog(null, "黑棋赢!"); 

} 

System.out.println("count=" + count + "   m=" + m); 



} 



public void printArray(int[][] array) { 

for (int i = 0; i <= array.length; i++) { 

for (int j = 0; j <= array[i].length; j++) { 

System.out.println("array[i][j]" + array[i][j]); 

} 

} 

System.out.println("  "); 

} 



public static void main(String agrs[]) { 

Hhesses h = new Hhesses(); // 实例化类对象 

h.fivechesses(); 

} 

 }