知识大全 Java实现扫雷(2)

Posted

篇首语:三人行,必有我师焉。择其善者而从之,其不善者而改之。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 Java实现扫雷(2)相关的知识,希望对你有一定的参考价值。

Java实现扫雷(2)  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  */

  //清楚原来的方形

  public void clearoldretangle(Graphics D gg int c int r)

  

  if(hasclickbomb)

  diamonds[r][c] clickshow(gg r c);

  else

  

  if(!diamonds[r][c] isOpened())

  diamonds[r][c] draw(gg r c);

  else

  diamonds[r][c] clickshow(gg r c);

  

  

  /*

  //清除原来的圆形

  public void clearOval(Graphics D gg int c int r)

  

  if(c<col && r<row)

  

  for(int y=r ;y<r+ &&y<row;y++)

  

  if(y< )continue;

  for(int x=c ;x<c+ &&x<col;x++)

  

  if(x>= )

  

  if(hasclickbomb)

  diamonds[y][x] clickshow(gg y x);

  else

  

  if(!diamonds[y][x] isOpened())

  diamonds[y][x] draw(gg y x);

  else

  diamonds[y][x] clickshow(gg y x);

  

  

  

  

  

  

  */

  //设置炸弹

  public void buildBombInMap()

  

  int x= y= ;

  for(int i= ;i<bombcount;)

  

  x=(int)(Math random()*col);

  y=(int)(Math random()*row);

  if(!diamonds[y][x] isbomb())

  

  diamonds[y][x] setbomb();

  i++;

  

  

  countRoundBomb();//计算炸弹

  

  //计算附近的炸弹

  public void countRoundBomb()

  

  for(int i= ;i<row;i++)

  

  for(int j= ;j<col;j++)

  

  if(diamonds[i][j] isbomb())

  continue;

  else

  

  //开始计算每个附近的炸弹

  int count= ;

  for(int k=i ;k<i+ && k<row;k++)

  

  if(k< )continue;

  for(int l=j ;l<j+ && l<col;l++)

  

  if(l>= )

  

  if(diamonds[k][l] isbomb())

  count++;

  

  

  

  if(count!= )

  diamonds[i][j] setShowByte(String valueOf(count));

  

  

  

  

  //踩中地雷后全部显示

  public void showAllDiamonds(int ro int co)

  

  Graphics D gg=(Graphics D)this getGraphics();

  gg setRenderingHint(RenderingHints KEY_ANTIALIASING RenderingHints VALUE_ANTIALIAS_ON);

  for(int r= ;r<row;r++)

  

  for(int c= ;c<col;c++)

  

  if(!diamonds[r][c] isbomb())

  diamonds[r][c] setFillColor(Color white);

  else if(diamonds[r][c] isbomb())

  diamonds[r][c] setFillColor(Color red);

  diamonds[r][c] clickshow(gg r c);

  

  

  

  //重载paint

  public void paint(Graphics g)

  

  Graphics D gg=(Graphics D)g;

  gg setRenderingHint(RenderingHints KEY_ANTIALIASING RenderingHints VALUE_ANTIALIAS_ON);

  for(int y= ;y<row;y++)

  

  for(int x= ;x<col;x++)

  

  if(hasclickbomb)

  diamonds[y][x] clickshow(gg y x);

  else

  

  if(!diamonds[y][x] isOpened())

  diamonds[y][x] draw(gg y x);

  else

  diamonds[y][x] clickshow(gg y x);

  

  

  

  gg dispose();

  

  

  Diamonds java文件

  import java awt BasicStroke;

  import java awt Color;

  import java awt Graphics;

  import java awt Graphics D;

  import java awt RenderingHints;

  /**

  * 单元方格类

  * @author dragon

  *

  */

  public class Diamonds

  

  private Color fillcolor=Color darkGray;

  private Color rimcolor=Color blue;

  private boolean bomb=false;

  private String showbyte= ;

  private int diamondWidth= ;

  private boolean opened=false;

  private boolean signbomb=false;

  public Diamonds(Color c int width)

  

  this rimcolor=c;

  this diamondWidth=width;

  

  public Diamonds(int width)

  

  this diamondWidth=width;

  

  /**

  * 设置是否被标记为炸弹

  * dragon

  *Sep

  * @param b

  */

  public void setSignBomb(boolean b)

  

  this signbomb=b;

  

  public boolean isSignBomb()

  

  return this signbomb;

  

  /**

  * 设置方格的边长

  * dragon

  *Sep

  * @param width

  */

  public void setDiamondWidth(int width)

  

  this diamondWidth=width;

  

  /**

  * 返回当前方格的字符串

  * dragon

  *Sep

  * @return

  */

  public String getShowByte()

  

  return this showbyte;

  

  /**

  * 设置方格的字符串

  * dragon

  *Sep

  * @param b

  */

  public void setShowByte(String b)

  

  showbyte=b;

  

  /**

  * 复位

  */

  public void reset()

  

  fillcolor=Color DARK_GRAY;

  rimcolor=Color blue;

  this signbomb=false;

  bomb=false;

  showbyte= ;

  opened=false;

  

  /**

  * 判断方格是否已经被打开

  */

  public boolean isOpened()

  

  return this opened;

  

  /**

  * 打开该方格

  */

  public void Opened()

  

  this opened=true;

  

  /**

  * 判断是否是炸弹

  */

  public boolean isbomb()

  

  return bomb;

  

  /**

  * 设置为炸弹

  */

  public void setbomb()

  

  this bomb=true;

  

  /**

  * 点击时的显示函数

  */

  public void clickshow(Graphics D gg int r int c)

  

  gg setStroke(new BasicStroke( f));

  if(bomb)

  

  if(opened)

  this fillcolor=Color red;

  gg setColor(this fillcolor);

  gg fillRect(c*diamondWidth r*diamondWidth diamondWidth diamondWidth);

  gg setColor(this rimcolor);

  gg drawRect(c*diamondWidth r*diamondWidth diamondWidth diamondWidth);

  gg setColor(Color black);

  gg drawOval(c*diamondWidth+diamondWidth/ r*diamondWidth+diamondWidth/ diamondWidth/ diamondWidth/ );

  

  else

  

  gg setColor(this fillcolor);

  gg fillRect(c*diamondWidth r*diamondWidth diamondWidth diamondWidth);

  gg setColor(this rimcolor);

  gg drawRect(c*diamondWidth r*diamondWidth diamondWidth diamondWidth);

  gg setColor(Color black);

  gg drawString(showbyte c*diamondWidth+diamondWidth/ r*diamondWidth+diamondWidth* / );

  

  

  /**

  * 绘画单元格

  */

  public void draw(Graphics D gg int r int c)

  

  gg setStroke(new BasicStroke( f));

  gg setColor(this fillcolor);

  gg fillRect(c*diamondWidth r*diamondWidth diamondWidth diamondWidth);

  gg setColor(this rimcolor);

  gg drawRect(c*diamondWidth r*diamondWidth diamondWidth diamondWidth);

  

  /**

  * 设置边框颜色

  */

  public void setRimColor(Color c)

  

  rimcolor=c;

  

  /**

  * 设置填充颜色

  */

  public void setFillColor(Color c)

  

  this fillcolor=c;

  

cha138/Article/program/Java/hx/201311/26439

相关参考