熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java實現掃雷(2)

2013-11-23 19:12:28  來源: Java核心技術 

  */

  //清楚原來的方形

  public void clearoldretangle(GraphicsD ggint cint 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(GraphicsD ggint cint 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)(Mathrandom()*col);

  y=(int)(Mathrandom()*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(StringvalueOf(count));

  }

  }

  }

  }

  //踩中地雷後全部顯示

  public void showAllDiamonds(int roint co)

  {

  GraphicsD gg=(GraphicsD)thisgetGraphics();

  ggsetRenderingHint(RenderingHintsKEY_ANTIALIASING RenderingHintsVALUE_ANTIALIAS_ON);

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

  {

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

  {

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

  diamonds[r][c]setFillColor(Colorwhite);

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

  diamonds[r][c]setFillColor(Colorred);

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

  }

  }

  }

  //重載paint

  public void paint(Graphics g)

  {

  GraphicsD gg=(GraphicsD)g;

  ggsetRenderingHint(RenderingHintsKEY_ANTIALIASING RenderingHintsVALUE_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);

  }

  }

  }

  ggdispose();

  }

  }

  Diamondsjava文件

  import javaawtBasicStroke;

  import javaawtColor;

  import javaawtGraphics;

  import javaawtGraphicsD;

  import javaawtRenderingHints;

  /**

  * 單元方格類

  * @author dragon

  *

  */

  public class Diamonds

  {

  private Color fillcolor=ColordarkGray;

  private Color rimcolor=Colorblue;

  private boolean bomb=false;

  private String showbyte=;

  private int diamondWidth=;

  private boolean opened=false;

  private boolean signbomb=false;

  public Diamonds(Color cint width)

  {

  thisrimcolor=c;

  thisdiamondWidth=width;

  }

  public Diamonds(int width)

  {

  thisdiamondWidth=width;

  }

  /**

  * 設置是否被標記為炸彈

  * dragon

  *Sep

  * @param b

  */

  public void setSignBomb(boolean b)

  {

  thissignbomb=b;

  }

  public boolean isSignBomb()

  {

  return thissignbomb;

  }

  /**

  * 設置方格的邊長

  * dragon

  *Sep

  * @param width

  */

  public void setDiamondWidth(int width)

  {

  thisdiamondWidth=width;

  }

  /**

  * 返回當前方格的字符串

  * dragon

  *Sep

  * @return

  */

  public String getShowByte()

  {

  return thisshowbyte;

  }

  /**

  * 設置方格的字符串

  * dragon

  *Sep

  * @param b

  */

  public void setShowByte(String b)

  {

  showbyte=b;

  }

  /**

  * 復位

  */

  public void reset()

  {

  fillcolor=ColorDARK_GRAY;

  rimcolor=Colorblue;

  thissignbomb=false;

  bomb=false;

  showbyte=;

  opened=false;

  }

  /**

  * 判斷方格是否已經被打開

  */

  public boolean isOpened()

  {

  return thisopened;

  }

  /**

  * 打開該方格

  */

  public void Opened()

  {

  thisopened=true;

  }

  /**

  * 判斷是否是炸彈

  */

  public boolean isbomb()

  {

  return bomb;

  }

  /**

  * 設置為炸彈

  */

  public void setbomb()

  {

  thisbomb=true;

  }

  /**

  * 點擊時的顯示函數

  */

  public void clickshow(GraphicsD ggint rint c)

  {

  ggsetStroke(new BasicStroke(f));

  if(bomb)

  {

  if(opened)

  thisfillcolor=Colorred;

  ggsetColor(thisfillcolor);

  ggfillRect(c*diamondWidth r*diamondWidth diamondWidth diamondWidth);

  ggsetColor(thisrimcolor);

  ggdrawRect(c*diamondWidth r*diamondWidth diamondWidth diamondWidth);

  ggsetColor(Colorblack);

  ggdrawOval(c*diamondWidth+diamondWidth/ r*diamondWidth+diamondWidth/ diamondWidth/ diamondWidth/);

  }

  else

  {

  ggsetColor(thisfillcolor);

  ggfillRect(c*diamondWidth r*diamondWidth diamondWidth diamondWidth);

  ggsetColor(thisrimcolor);

  ggdrawRect(c*diamondWidth r*diamondWidth diamondWidth diamondWidth);

  ggsetColor(Colorblack);

  ggdrawString(showbyte c*diamondWidth+diamondWidth/ r*diamondWidth+diamondWidth*/);

  }

  }

  /**

  * 繪畫單元格

  */

  public void draw(GraphicsD ggint rint c)

  {

  ggsetStroke(new BasicStroke(f));

  ggsetColor(thisfillcolor);

  ggfillRect(c*diamondWidth r*diamondWidth diamondWidth diamondWidth);

  ggsetColor(thisrimcolor);

  ggdrawRect(c*diamondWidth r*diamondWidth diamondWidth diamondWidth);

  }

  /**

  * 設置邊框顏色

  */

  public void setRimColor(Color c)

  {

  rimcolor=c;

  }

  /**

  * 設置填充顏色

  */

  public void setFillColor(Color c)

  {

  thisfillcolor=c;

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26439.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.