来到华信

来华信上了五节课~~我从零基础开始学~~~现在知道怎么去写一个窗体出来~~~并在窗体中添加各种元素~~

在这五节课当中~~~我开始学习如何写一个小程序~~下面是我写的五子棋的代码~~重新开始功能还没能用

package FiveChess;

import javax.swing.*;
import java.util.ArrayList;
import java.awt.*;
import java.awt.event.*;
import java.awt.Container;

public class FiveChess {
 protected static final String JOptionPanel = null;
 Graphics g;
 boolean colorFlag = true;// 区分颜色的标记
 int[][] chesses = new int[22][22];// 保存棋子信息的二位数组
 boolean beginFlag = false;// 是否点击开始的标记
 Color color;// 颜色的标志
 ArrayList list = new ArrayList();// 可变的容器,用来保存棋子对象
 int patternFlag = 0;

 public void showUI() {
  // 创建窗体对象
  final JFrame chessFrame = new JFrame();

  // 设置窗体属性
  chessFrame.setTitle("五子棋");
  chessFrame.setSize(600, 500);
  chessFrame.setLocationRelativeTo(null);
  chessFrame.setDefaultCloseOperation(3);
  FlowLayout layout = new FlowLayout();
  chessFrame.setLayout(layout);
  int restartFlag=0;
  // 将界面分成两块区域(棋盘区 、功能区)
   final JPanel qipanPanel = new JPanel() {
   /**
    * 创建棋盘
    */
   private static final long serialVersionUID = 1L;

   // paint方法,来绘制棋盘
   //if(restartFlag==0){
   public void paint(Graphics g) {
    super.paint(g);
    for (int i = 0; i < 22; i++) {
     g.drawLine(20, 20 * (i + 1), 440, 20 * (i + 1));
    }
    for (int i = 0; i < 22; i++) {
     g.drawLine(20 * (i + 1), 20, 20 * (i + 1), 440);
    }
    // 棋子的重绘
    for (int i = 0; i < list.size(); i++) {
     Chess chess = (Chess) list.get(i);
     chess.drawChess(g);
    }
   
   }
  };
  JPanel functionPanel = new JPanel();
  // 设置背景颜色
  qipanPanel.setBackground(Color.red);
  functionPanel.setBackground(Color.blue);
  // 设置面板大小setSize只适合重量级容器,像JPanel,JButton轻量级组件
  // 设置大小setPreferredSize(new Dimension(w,h)) Dimension尺寸
  qipanPanel.setPreferredSize(new Dimension(450, 450));
  functionPanel.setPreferredSize(new Dimension(120, 450));
  JButton buttonstart = new JButton("开始");
  JButton buttonrenren = new JButton("人人");
  JButton buttonrenji = new JButton("人机");
  JButton buttonrestart = new JButton("重新开始");
  JButton buttonhui = new JButton("悔棋");
  JButton buttonabout = new JButton("关于");
  JButton buttonexit = new JButton("退出");

  // 将面板添加到窗体
  chessFrame.add(qipanPanel);
  chessFrame.add(functionPanel);
  functionPanel.add(buttonstart);
  functionPanel.add(buttonrenren);
  functionPanel.add(buttonrenji);
  functionPanel.add(buttonhui);
  functionPanel.add(buttonrestart);
  functionPanel.add(buttonabout);
  functionPanel.add(buttonexit);
  // 创建动作监听对象
  ActionListener action_listener = new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String record = e.getActionCommand();
    switch (record) {
    case "开始":
     beginFlag = true;
     break;
    case "退出":
     System.exit(0);
     break;
    case "悔棋":
     if (list.size() == 0) {
      break;
     }
     ;
     if (list.size() > 0) {
      Chess lastChess = (Chess) list.get(list.size() - 1);
      Color lastColor = lastChess.color;
      //System.out.println(lastChess);
      list.remove(list.size() - 1);
      chessFrame.repaint();// 强制重绘
      if (lastColor == Color.black) {
       colorFlag = false;
      } else if (lastColor == Color.white) {
       colorFlag = true;
      }
      int x = lastChess.x;
      int y = lastChess.y;
      chesses[y/20-1][x/20-1]=0;
     }
     break;
    case "关于":
     JOptionPane.showMessageDialog(null, "五子棋是也。");
     break;
    case "人人":
     patternFlag = 1;
     break;
    case "人机":
     patternFlag = 2;
     break;
    case "重新开始":
     for(int i=0;i<22;i++){
      for(int j=0;j<22;j++){
       chesses[i][j]=0;
      }
     }
      list.removeAll(list);
      qipanPanel.removeAll();
    }
   }

  };

  // 进行监听
  buttonexit.addActionListener(action_listener);
  buttonstart.addActionListener(action_listener);
  buttonhui.addActionListener(action_listener);
  buttonrenren.addActionListener(action_listener);
  buttonabout.addActionListener(action_listener);
  buttonrestart.addActionListener(action_listener);

  // 创建鼠标监听器对象
  MouseListener listener = new MouseListener() {

   // 监控进入
   public void mouseEntered(MouseEvent e) {
   }

   // 监控退出
   public void mouseExited(MouseEvent e) {
   }

   // 监控单击
   public void mouseClicked(MouseEvent e) {
    int x = e.getX();
    int y = e.getY();
    // 获取所有交叉点坐标
    if (beginFlag) {
     if (patternFlag == 0) {
      JOptionPane.showMessageDialog(null, "请选择模式");
     }
     if (patternFlag == 1) {
      for (int i = 0; i < 22; i++) {
       for (int j = 0; j < 22; j++) {
        int x0 = 20 * (j + 1);
        int y0 = 20 * (i + 1);

        // 判断误差范围
        if (x > x0 - 40 / 5 && x < x0 + 40 / 5
          && y > y0 - 40 / 5 && y < y0 + 40 / 5) {
         // 判断此位置是否已经有棋子
         if (chesses[i][j] == 0) {
          if (colorFlag == true) {
           // 白色
           color = Color.white;
           chesses[i][j] = 1;
           colorFlag = false;
          } else if (colorFlag == false) {
           // 黑色
           color = Color.black;
           chesses[i][j] = 2;
           colorFlag = true;
          }
          // 绘制棋子
          Chess chess = new Chess(x0 , y0,
            color);
          chess.drawChess(g);
          list.add(chess);
          win(i, j);// 判断输赢 d
         }
        }
       }
      }
     }

    } else {
     JOptionPane.showMessageDialog(null, "请点击开始");
    }

   }

   // 监控按下
   public void mousePressed(MouseEvent e) {
   }

   // 监控释放
   public void mouseReleased(MouseEvent e) {
   }
  };

  // 监听鼠标在窗体的行为
  qipanPanel.addMouseListener(listener);

  // 可见
  chessFrame.setVisible(true);
  // 获取窗体的画布(必须在可见之后才能获取到画布)
  g = qipanPanel.getGraphics();

 }

 // 判断输赢的方法
 public void win(int i, int j) {
  int count = 1;
  int m = 1;
  while (j - m >= 0 && chesses[i][j] == chesses[i][j - m]) {
   count++;
   m++;
  }
  m = 1;
  while (j + m < 22 && chesses[i][j] == chesses[i][j + m]) {
   count++;
   m++;
  }
  m = 1;
  while (i - m >= 0 && chesses[i][j] == chesses[i - m][j]) {
   count++;
   m++;
  }
  m = 1;
  while (i + m < 22 && chesses[i][j] == chesses[i + m][j]) {
   count++;
   m++;
  }
  m = 1;
  while (i - m >= 0 && j - m >= 0
    && chesses[i][j] == chesses[i - m][j - m]) {
   count++;
   m++;
  }
  m = 1;
  while (i + m < 22 && j + m < 22
    && chesses[i][j] == chesses[i + m][j + m]) {
   count++;
   m++;
  }
  // 判断
  if (count >= 5 && chesses[i][j] == 1) {
   JOptionPane.showMessageDialog(null, "白棋赢");
   JOptionPane.showMessageDialog(null, "请点击重新开始");
   
  }
  if (count >= 5 && chesses[i][j] == 2) {
   JOptionPane.showMessageDialog(null, "黑棋赢");
   JOptionPane.showMessageDialog(null, "请点击重新开始");
  }
 }

 public static void main(String[] args) {
  FiveChess chess = new FiveChess();
  chess.showUI();
 }

}

package FiveChess;

import java.awt.Color;
import java.awt.Graphics;


/**
 * 棋子类   坐标  颜色  画棋子的行为
 * @author Administrator
 *
 */
public class Chess {
 //坐标 颜色 属性
 int x,y;
 Color color;
 
 //构造方法  接收传递过来的数据
 public Chess(int x,int y,Color color){
  this.x = x;
  this.y = y;
  this.color = color;
 }
 
 //绘制棋子的方法
 public void drawChess(Graphics g){
  g.setColor(color);
  g.fillOval(x-7,y-7,14,14);
 }
 
 
 
 
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值