java开发俄罗斯方块学习笔记 Day-8 配置

游戏界面配置解析
本文介绍了一种通过XML配置文件来管理游戏界面布局的方法,并详细展示了如何利用Java的DOM4J库解析配置文件,以及如何设计类来封装这些配置信息。
     在开发过程中要尽量避免硬编码的问题,通常将数字或者字符串定义成常量或写进配置文件。这么多数据如果写进常量,则常量文件会非常大,在这里我们使用配置文件。
	首先新建一个config文件夹,然后在里面建一个XML配置文件,它是一个层级配置文件,可以将数据进行由大到小进行归类。
<?xml version="1.0" encoding="UTF-8"?>
<game>
	<frame width="1168" height="680" padding="16" windowSize="7">
	//界面的配置
		<layer className="ui.LayerBackground" x="0" y="0" w="0" h="0"/>
		<layer className="ui.LayerDataBase" x="40" y="32" w="334" h="279"/>
		<layer className="ui.LayerDisk" x="40" y="343" w="334" h="279"/>
		<layer className="ui.LayerGame" x="414" y="32" w="334" h="590"/>
		<layer className="ui.LayerButton" x="788" y="32" w="334" h="124"/>
		<layer className="ui.LayerNext" x="788" y="188" w="176" h="148"/>
		<layer className="ui.LayerLevel" x="964" y="188" w="158" h="148"/>
		<layer className="ui.LayerPoint" x="788" y="368" w="334" h="200"/>
	</frame>
	<system>
	//游戏业务逻辑的配置(比如俄罗斯方块下落速度,满分等常量)
	</system>
	<data>
	//数据库连接参数,文件规格
	</data>
</game>
	有了配置文件以后,我们需要知道如何读取配置文件,这里需要导入第三方的jar包。使用第三方jar包一般在工程的根目录下建立一个lib文件夹,然后将dom4j-1.6.1jar放入该文件夹内,然后点击右键BuildPath,AddToBuildPath,引入到工程文件。
	在config包里面新建一个ConfigReader类,它是XML文件的配置读取器
package config;


import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


public class ConfigReader {


	public static void readConfig() throws Exception{
		//XML文件读取器
		SAXReader reader=new SAXReader();
		//获得xml文件
		Document doc=reader.read("config/cfg.xml");
		//得到<game>标签里面的所有东西
		Element game=doc.getRootElement();
		//取得<frame>里面的东西
		Element frame=game.element("frame");
		//因为frame里面有多个layer,需要调用另一个方法elements(),用List接收返回的一组元素
		List<Element> layers=frame.elements("layer");
		for(Element layer:layers){
			System.out.print(layer.attributeValue("className")+",");
			System.out.print(layer.attributeValue("x")+",");
			System.out.print(layer.attributeValue("y")+",");
			System.out.print(layer.attributeValue("w")+",");
			System.out.print(layer.attributeValue("h")+",");
			System.out.println();
		}
		
	}
	
	public static void main(String[] args) throws Exception {
		readConfig();
	}
}
	再新建一个GameConfig类,用来接收属性值。
package config;


import java.util.ArrayList;
import java.util.List;


import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


/**
 * 接收属性
 * @author Administrator
 *
 */
public class GameConfig {
	
	private int width;
	private int height;
	private int windowSize;
	private int padding;
	
	private List<LayerConfig> layersConfig;
	
	//让GameConfig创建对象时自动读取XML文件
	public GameConfig() throws Exception{
		SAXReader reader=new SAXReader();
		//获得XML文件
		Document doc=reader.read("config/cfg.xml");
		//得到<game>标签里面的所有东西
		Element game=doc.getRootElement();
		//取得<frame>里面的东西
		Element frame=game.element("frame");
		this.width=Integer.parseInt(frame.attributeValue("width"));
		this.height=Integer.parseInt(frame.attributeValue("height"));
		this.windowSize=Integer.parseInt(frame.attributeValue("windowSize"));
		this.padding=Integer.parseInt(frame.attributeValue("padding"));
		List<Element> layers=frame.elements("layer");
		layersConfig=new ArrayList<LayerConfig>();
		for(Element layer:layers){
			LayerConfig lc=new LayerConfig();
			//将读到的字符串全部set
			lc.setClassName(layer.attributeValue("className"));
			lc.setX(Integer.parseInt(layer.attributeValue("x")));
			lc.setY(Integer.parseInt(layer.attributeValue("y")));
			lc.setW(Integer.parseInt(layer.attributeValue("w")));
			lc.setH(Integer.parseInt(layer.attributeValue("h")));
			layersConfig.add(lc);
		}
		
	}
}
	新建一个LayerConfig类,用来封装配置
package config;


public class LayerConfig {


		private String className;
		
		private int x;
		
		private int y;
		
		private int w;
		
		private int h;
		
		public String getClassName() {
			return className;
		}


		public void setClassName(String className) {
			this.className = className;
		}


		public int getX() {
			return x;
		}


		public void setX(int x) {
			this.x = x;
		}


		public int getY() {
			return y;
		}


		public void setY(int y) {
			this.y = y;
		}


		public int getW() {
			return w;
		}


		public void setW(int w) {
			this.w = w;
		}


		public int getH() {
			return h;
		}


		public void setH(int h) {
			this.h = h;
		}
		
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值