⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 draw.java

📁 Java版仿Windows自带小游戏——扫雷
💻 JAVA
字号:


import java.awt.* ;
import javax.swing.* ;

/**
 * <p>Title: 扫雷</p>
 *
 * <p>Description: 仿Windows自带的小游戏-扫雷-Java 版</p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: 中国矿业大学</p>
 *
 * <p>Email:chxzhou012@126.com</p>
 *
 * <p>QQ:395246039</p>
 *
 * @author zhou(周川祥)
 * @version 1.0
 */


class Draw
{

//游戏数据
  int xGrid ; //地图x轴格数
  int yGrid ; //地图y轴格数
  int mineCount ; //地雷数
  Container c ; //容器
  Image offImage ;
  Graphics offGraphics ;

//图片数组
  Image[] imgSmile ;
  Image[] imgMap ;
  Image[] imgCount ;

//所有图片宽与长
  int smileWidth , simleHeight , mapWidth ,
  mapHeight , countWidth , countHeight ;

//各区域坐标
  int stateX , stateY ; //游戏状态区
  int countX , countY ; //时间计数
  int mineX , mineY ; //地雷计数
  int mapX , mapY ; //地雷区
  int smileX , smileY ; //笑脸

  public Draw ( int gridx , //地图x轴格数
                int gridy , //地图y轴格数
                int nMine , //地雷数
                Container c )
  { //容器
    this.xGrid = gridx ;
    this.yGrid = gridy ;
    this.mineCount = nMine ;
    this.c = c ;
    offImage = c.createImage ( c.getWidth () , c.getHeight () ) ;
    offGraphics = offImage.getGraphics () ;

    //读入所有图片
    imgSmile = new Image[5] ; //游戏状态图(笑脸)
    for ( int i = 0 ; i <= 4 ; i++ )
    {
      imgSmile[i] = ( new ImageIcon ( "picture/smile/s" + i + ".gif" ) ).
          getImage () ;
    }

    imgCount = new Image[10] ; //计数(LED字型)
    for ( int i = 0 ; i <= 9 ; i++ )
    {
      imgCount[i] = ( new ImageIcon ( "picture/count/" + i + ".gif" ) ).
          getImage () ;
    }

    imgMap = new Image[15] ; //地图上(数字、地雷)
    for ( int i = 0 ; i <= 14 ; i++ )
    {
      imgMap[i] = ( new ImageIcon ( "picture/map/m" + i + ".gif" ) ).getImage () ;
    }

    //纪录图片宽与长
    smileWidth = imgSmile[0].getWidth ( null ) ; //游戏状态图
    simleHeight = imgSmile[0].getHeight ( null ) ;
    countWidth = imgCount[0].getWidth ( null ) ; //计数图
    countHeight = imgCount[0].getHeight ( null ) ;
    mapWidth = imgMap[0].getWidth ( null ) ; //地图状态图
    mapHeight = imgMap[0].getHeight ( null ) ;

    //所有区域坐标
    mapX = mapWidth ; //地雷区
    mapY = mapHeight * 5 ;
    drawMapFrame ( mapX - 3 , mapY - 3 ,
                   xGrid * mapWidth + 6 , yGrid * mapHeight + 6 ,
                   3 , false ) ;

    stateX = mapWidth ; //游戏状态区
    stateY = mapHeight ;
    drawMapFrame ( stateX - 3 , stateY - 3 ,
                   xGrid * mapWidth + 6 , mapHeight * 3 + 6 ,
                   3 , false ) ;

    mineX = stateX + ( mapHeight * 3 - countHeight ) / 2 ; //地雷数
    mineY = stateY + ( mapHeight * 3 - countHeight ) / 2 ;
    drawMapFrame ( mineX - 2 , mineY - 2 ,
                   countWidth * 3 + 4 , countHeight + 4 ,
                   2 , false ) ;

    countX = ( stateX - 3 ) +
        ( xGrid * mapWidth + 6 ) - mapWidth - countWidth * 3 ; //秒数
    countY = mineY ;
    drawMapFrame ( countX - 2 , countY - 2 ,
                   countWidth * 3 + 4 , countHeight + 4 , 2 , false ) ;

    smileX = ( stateX - 3 ) + ( xGrid * mapWidth + 6 - smileWidth ) / 2 ; //笑脸
    smileY = stateY + ( mapHeight * 3 - simleHeight ) / 2 ;

    //画出立体框
    drawMapFrame ( 0 , 0 , ( xGrid + 2 ) * mapWidth , ( yGrid + 6 ) * mapHeight ,
                   4 , true ) ;

    //mapInit();//地图初始化
    showCount ( mineX , mineY , mineCount ) ; //显示地雷数
    showCount ( countX , countY , 0 ) ; //显示秒数
    showSmile ( 0 ) ; //显示笑脸

  }

//显示地图图标
  public void showMap ( int x , int y , int mode )
  {
    if ( x >= 0 && x < xGrid && //防止超出数组
         y >= 0 && y < yGrid )
    {
      offGraphics.drawImage ( imgMap[mode] ,
                              mapX + x * mapWidth ,
                              mapY + y * mapHeight ,
                              null ) ;
    }
  }

//显示笑脸
  public void showSmile ( int i )
  {
    if ( i >= 0 && i <= 4 )
    {
      offGraphics.drawImage ( imgSmile[i] , smileX , smileY , null ) ;
    }
    update () ;
  }

//显示计数数字
  public void showCount ( int x , int y , int count )
  {
    if ( count >= 0 )
    {
      offGraphics.drawImage ( imgCount[ ( count % 1000 ) / 100] , x , y , null ) ;
      offGraphics.drawImage ( imgCount[ ( count % 100 ) / 10] , x + countWidth ,
                              y , null ) ;
      offGraphics.drawImage ( imgCount[ ( count % 10 )] , x + countWidth * 2 ,
                              y , null ) ;
      update () ; //更新画面
    }
  }

  //画出砖块组动作区地图外框
  public void drawMapFrame ( int x , int y , int wid , int hig , int side ,
                             boolean raised )
  {

    //定义多边形坐标
    int xx[][] =
        {
        {
        x , x + wid , x + wid - side , x + side} , //上
        {
        x + wid - side , x + wid , x + wid , x + wid - side} , //右
        {
        x + side , x + wid - side , x + wid , x} , //下
        {
        x , x + side , x + side , x}
    } ; //左

    int yy[][] =
        {
        {
        y , y , y + side , y + side} , //上
        {
        y + side , y , y + hig , y + hig - side} , //右
        {
        y + hig - side , y + hig - side , y + hig , y + hig} , //下
        {
        y , y + side , y + hig - side , y + hig}
    } ; //左

    //画出多边形
    for ( int i = 0 ; i <= 3 ; i++ )
    {
      if ( raised )
      { //设定框为凸起
        switch ( i )
        { //不同边设定不同颜色
          case 0:
            offGraphics.setColor ( new Color ( 250 , 250 , 250 ) ) ;
            break ;
          case 1:
            offGraphics.setColor ( new Color ( 120 , 120 , 120 ) ) ;
            break ;
          case 2:
            offGraphics.setColor ( new Color ( 100 , 100 , 100 ) ) ;
            break ;
          case 3:
            offGraphics.setColor ( new Color ( 240 , 240 , 240 ) ) ;
            break ;
        }
      }
      else
      { //设定框为凹陷
        switch ( i )
        { //不同边设定不同颜色
          case 0:
            offGraphics.setColor ( new Color ( 100 , 100 , 100 ) ) ;
            break ;
          case 1:
            offGraphics.setColor ( new Color ( 240 , 240 , 240 ) ) ;
            break ;
          case 2:
            offGraphics.setColor ( new Color ( 250 , 250 , 250 ) ) ;
            break ;
          case 3:
            offGraphics.setColor ( new Color ( 120 , 120 , 120 ) ) ;
            break ;
        }
      }
      offGraphics.fillPolygon ( xx[i] , yy[i] , 4 ) ;
    }
    update () ; //更新画面
  }

  public void update ()
  {
    c.getGraphics ().drawImage ( offImage , 0 , 0 , null ) ;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -