java面向对象实验——扫雷+24点
扫雷
窗口绘制:
GameWin
package com.sxt;import javax.swing.*;public class GameWin extends JFrame {void launch(){this.setVisible(true);this.setSize(500, 500);this.setLocationRelativeTo(null);this.setTitle("SWE23070扫雷游戏");this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}public static void main(String[] args) {GameWin gameWin = new GameWin();gameWin.launch();}
}
//窗口绘制
雷区绘制:
GameWin
package com.sxt;import javax.swing.*;
import java.awt.*;public class GameWin extends JFrame {MapBottom mapBottom = new MapBottom();void launch(){this.setVisible(true);this.setSize(500, 500);this.setLocationRelativeTo(null);this.setTitle("SWE23070扫雷游戏");this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}@Overridepublic void paint(Graphics g) {mapBottom.paintSelf(g);}public static void main(String[] args) {GameWin gameWin = new GameWin();gameWin.launch();}
}
MapBottom
package com.sxt;import java.awt.*;public class MapBottom {void paintSelf(Graphics g){for (int i = 0; i < 500; i=i+50) {g.setColor(Color.red);g.drawLine(0,i,500,i);g.drawLine(i,0,i,500);}}
}
界面规划:
GameWin
package com.sxt;import javax.swing.*;
import java.awt.*;public class GameWin extends JFrame {int width = 2*GameUtil.OFFSET+GameUtil.MAP_W*GameUtil.SQUARE_LENGTH;int height = 4*GameUtil.OFFSET+GameUtil.MAP_H*GameUtil.SQUARE_LENGTH;MapBottom mapBottom = new MapBottom();void launch(){this.setVisible(true);this.setSize(width, height);this.setLocationRelativeTo(null);this.setTitle("SWE23070扫雷游戏");this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}@Overridepublic void paint(Graphics g) {mapBottom.paintSelf(g);}public static void main(String[] args) {GameWin gameWin = new GameWin();gameWin.launch();}
}
MapBottom
package com.sxt;import java.awt.*;public class MapBottom {void paintSelf(Graphics g){g.setColor(Color.red);//画竖线for(int i = 0; i<= GameUtil.MAP_W; i++){g.drawLine(GameUtil.OFFSET+i*GameUtil.SQUARE_LENGTH,3*GameUtil.OFFSET,GameUtil.OFFSET+i*GameUtil.SQUARE_LENGTH,3*GameUtil.OFFSET+GameUtil.MAP_H*GameUtil.SQUARE_LENGTH);}//画横线for(int i = 0; i<= GameUtil.MAP_H; i++){g.drawLine(GameUtil.OFFSET,3*GameUtil.OFFSET+i*GameUtil.SQUARE_LENGTH,GameUtil.OFFSET+GameUtil.MAP_W*GameUtil.SQUARE_LENGTH,3*GameUtil.OFFSET+i*GameUtil.SQUARE_LENGTH);}}
}
GameUtil
package com.sxt;public class GameUtil {//地图的宽static int MAP_W = 11;//地图的高static int MAP_H = 11;//雷区偏移量static int OFFSET = 45;//格子边长static int SQUARE_LENGTH = 50;
}
底层地图:
GameWin
package com.sxt;import javax.swing.*;
import java.awt.*;public class GameWin extends JFrame {int width = 2*GameUtil.OFFSET+GameUtil.MAP_W*GameUtil.SQUARE_LENGTH;int height = 4*GameUtil.OFFSET+GameUtil.MAP_H*GameUtil.SQUARE_LENGTH;MapBottom mapBottom = new MapBottom();void launch(){this.setVisible(true);this.setSize(width, height);this.setLocationRelativeTo(null);this.setTitle("SWE23070扫雷游戏");this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}@Overridepublic void paint(Graphics g) {mapBottom.paintSelf(g);}public static void main(String[] args) {GameWin gameWin = new GameWin();gameWin.launch();}
}
MapBottom
package com.sxt;import java.awt.*;public class MapBottom {void paintSelf(Graphics g){g.setColor(Color.red);//画竖线for(int i = 0; i<= GameUtil.MAP_W; i++){g.drawLine(GameUtil.OFFSET+i*GameUtil.SQUARE_LENGTH,3*GameUtil.OFFSET,GameUtil.OFFSET+i*GameUtil.SQUARE_LENGTH,3*GameUtil.OFFSET+GameUtil.MAP_H*GameUtil.SQUARE_LENGTH);}//画横线for(int i = 0; i<= GameUtil.MAP_H; i++){g.drawLine(GameUtil.OFFSET,3*GameUtil.OFFSET+i*GameUtil.SQUARE_LENGTH,GameUtil.OFFSET+GameUtil.MAP_W*GameUtil.SQUARE_LENGTH,3*GameUtil.OFFSET+i*GameUtil.SQUARE_LENGTH);}for(int i = 1; i<= GameUtil.MAP_W; i++){for(int j = 1; j<= GameUtil.MAP_H; j++){g.drawImage(GameUtil.lei,GameUtil.OFFSET+(i-1)*GameUtil.SQUARE_LENGTH+1,GameUtil.OFFSET*3+(j-1)*GameUtil.SQUARE_LENGTH+1,GameUtil.SQUARE_LENGTH-2,GameUtil.SQUARE_LENGTH-2,null);}}}
}
GameUtil
package com.sxt;import java.awt.*;public class GameUtil {//地图的宽static int MAP_W = 11;//地图的高static int MAP_H = 11;//雷区偏移量static int OFFSET = 45;//格子边长static int SQUARE_LENGTH = 50;//底层元素 -1 雷 0 空 1-8 表示对应数字static int[][] MAP_BOTTOM = new int[MAP_W+2][MAP_H+2];//载入图片static Image lei = Toolkit.getDefaultToolkit().getImage("imgs/lei.png");}