You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.2 KiB

package com.sheep.view;
/*
游戏界面
完成游戏界面的创建和接下来的游戏事件监听
*/
import com.sheep.model.Brand;
import com.sheep.model.Cell;
import com.sheep.model.Layer;
import com.sheep.model.Map;
import com.sheep.util.MapUtil;
import javax.swing.*;
import java.util.List;
public class Game extends JFrame {
private Map map;//用于存储游戏地图中的牌
//游戏界面的构造函数
public Game(Integer gameMode) {
map = MapUtil.MapBuild(3,gameMode);//创建地图
init();//初始化窗口
//渲染图层
List<Layer> list = map.getList();
for(int i = 0; i < list.size(); i++) {
renderLayer(list.get(i));
}
//图标置灰
MapUtil.compareAll(map);
//添加背景
addBackgound(gameMode);
aotoRefresh();//自动刷新
}
//窗口初始化
private void init(){
this.setTitle("java羊了个羊");//标题
this.setSize(425,800);//窗口大小
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭java界面退出程序
this.setLocationRelativeTo(null);//窗体剧中
this.setLayout(null);//设置绝对布局
this.setBounds(0,0,450,800);//窗口内部容器
this.setVisible(true);//当前窗体显示
}
//图标坐标位置偏移
private void renderLayer(Layer layer){
Cell[][] cells = layer.getCells();
for(int row = 0;row < cells.length;row++){
for(int col = 0;col<cells[row].length;col++){
Brand brands = cells[row][col].getBrand();
int x = col * 50 + layer.getOffsetx() + 50;
int y = row * 50 + layer.getOffsety() + 50;
brands.setBounds(x,y,50,50);
this.getContentPane().add(brands);
}
}
}
//根据Start传入的游戏模式添加背景
private void addBackgound(int gameMode){
JLabel backgroungLabel;
switch(gameMode){
case 1:
backgroungLabel = new JLabel(new ImageIcon("imgs/背景1_1.png"));
backgroungLabel.setBounds(0,0,450,800);
this.add(backgroungLabel);
break;
case 2:
backgroungLabel = new JLabel(new ImageIcon("imgs/背景2_1.png"));
backgroungLabel.setBounds(0,0,450,800);
this.add(backgroungLabel);
break;
case 3:
backgroungLabel = new JLabel(new ImageIcon("imgs/背景3_1.png"));
backgroungLabel.setBounds(0,0,450,800);
this.add(backgroungLabel);
break;
}
}
//重复刷新函数
private void aotoRefresh(){
JFrame start = this;
new Thread(new Runnable() {
@Override
public void run() {
while(true){
start.repaint();//重新绘制
//设置刷新延迟
try {
Thread.sleep(40);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}).start();
}
}