用java编写一个魔塔地图生成器

之前有闲功夫的时候,用Android写了一款魔塔小游戏,但是对地图的设计却一直没有进行,这次在电脑上写一款专门用来生成地图的魔塔地图生成器。运行之后的效果是这样的:

用java编写一个魔塔地图生成器_第1张图片

在左面的框里点击,便会将右侧选择的绘制图案绘制在点击的位置,比如在最左侧蝙蝠的下面添加一个盾牌

用java编写一个魔塔地图生成器_第2张图片

嗯,就是这样子的。

主要的代码是这样的

public class GeneratorPanel extends JPanel implements MouseListener{

	JLabel[][] label;
	JLabel select_filling;
	JLabel select_layer;
	ImageIcon[] image;
	JButton button_generate;
	JButton button_save;
	JButton button_surrounding;
	JButton button_crear;
	JComboBox floor_box;
	JComboBox image_box;
	public GeneratorPanel()
	{
		this.setLayout(null);
		image=new ImageIcon[64];
		for(int i=0;i<64;i++)
		{
			image[i]=new ImageIcon("./p"+((i>9)?i:("0"+i))+".png");
			image[i].setImage(image[i].getImage().getScaledInstance(40, 40, Image.SCALE_DEFAULT));
		}

		label=new JLabel[13][13];
		 for(int i=0;i<13;i++){
	            for(int j=0;j<13;j++){
	                label[i][j]=new JLabel(image[dto.point[0][i][j]]);
	                label[i][j].addMouseListener(this);
	                label[i][j].setBounds(40*i, 40*j, 40, 40);
	                this.add(label[i][j]);
	            }
	        }
		 button_generate=new JButton("生成代码");
		 button_generate.setBounds(540, 490, 200, 20);
		 this.add(button_generate);
		 floor_box=new JComboBox();
		 for(int i=0;i<25;i++)
		 floor_box.addItem(i);
		 floor_box.setBounds(540, 390, 200, 20);
		 this.add(floor_box);
		 image_box=new JComboBox(image);
		 image_box.setBounds(540, 290, 200, 50);
		 this.add(image_box);
		 select_filling=new JLabel("选择绘制图案:");
		 select_filling.setBounds(540, 270, 200, 15);
		 this.add(select_filling);
		 select_layer=new JLabel("选择当前层数");
		 select_layer.setBounds(540, 370, 200, 15);
		 this.add(select_layer);
		 button_save=new JButton("保存");
		 button_save.setBounds(540, 90, 200, 20);
		 this.add(button_save);
		 button_surrounding=new JButton("增加环绕");
		 button_surrounding.setBounds(540, 40, 200, 20);
		 this.add(button_surrounding);
		 button_crear=new JButton("清空当前层");
		 button_crear.setBounds(540, 140, 200, 20);
		 this.add(button_crear);
		 floor_box.addItemListener(new ItemListener() {
			
			@Override
			public void itemStateChanged(ItemEvent e) {
				if(e.getStateChange()==ItemEvent.SELECTED)
				{
					 for(int i=0;i<13;i++){
				            for(int j=0;j<13;j++){
				            	if(!label[i][j].getIcon().equals(image[dto.point[dto.stair][i][j]]))
				            	{
				            		Icon icon=label[i][j].getIcon();
				            		for(int k=0;k<64;k++)
				            		{
				            			if(icon.equals(image[k]))
				            				dto.point[dto.stair][i][j]=k;
				            		}
				            	}
				            }
					 }
					dto.stair=(int)e.getItem();
					 for(int i=0;i<13;i++){
				            for(int j=0;j<13;j++){
				            	label[i][j].setIcon(image[dto.point[dto.stair][i][j]]);
				            }
					 }
				}
				
			}
		});
		 button_save.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				File filename=new File("hehe.txt");
				try {
					filename.createNewFile();
					StringBuffer sb=new StringBuffer();
					 for(int i=0;i<13;i++){
				            for(int j=0;j<13;j++){
				            	if(!label[i][j].getIcon().equals(image[dto.point[dto.stair][i][j]]))
				            	{
				            		Icon icon=label[i][j].getIcon();
				            		for(int k=0;k<64;k++)
				            		{
				            			if(icon.equals(image[k]))
				            				dto.point[dto.stair][i][j]=k;
				            		}
				            	}
				            }
					 }
					for(int i=0;i<25;i++)
						for(int j=0;j<13;j++)
							for(int k=0;k<13;k++)
							{
								sb.append(dto.point[i][j][k]+" ");
							}
					sb.append("\r\n");
					BufferedWriter out=new BufferedWriter(new FileWriter(filename));
					out.write(sb.toString());
					out.flush();
					out.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				
			}
		});
		 button_surrounding.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				for(int j=0;j<13;j++)
					for(int k=0;k<13;k++)
					{
						if(j==0||k==0||j==12||k==12)
						{
							label[j][k].setIcon(image[1]);
						}
					}
				
			}
		});
		 button_crear.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				for(int j=0;j<13;j++)
					for(int k=0;k<13;k++)
					{
						label[j][k].setIcon(image[0]);
					}
				
			}
		});
		 button_generate.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				for(int i=0;i<25;i++)
					for(int j=0;j<13;j++)
						for(int k=0;k<13;k++)
						{
							if(dto.point[i][j][k]>0)
							{
								if(dto.point[i][j][k]<33)
								{
								System.out.println("wallpoint["+i+"]["+j+"]["+k+"]=-"+dto.point[i][j][k]+";");
								}
								else 
								{
									System.out.println("wallpoint["+i+"]["+j+"]["+k+"]="+(dto.point[i][j][k]-32)+";");
								}
							}
						}
				File filename=new File("config");
				try {
					filename.createNewFile();
					StringBuffer sb=new StringBuffer();
					 for(int i=0;i<13;i++){
				            for(int j=0;j<13;j++){
				            	if(!label[i][j].getIcon().equals(image[dto.point[dto.stair][i][j]]))
				            	{
				            		Icon icon=label[i][j].getIcon();
				            		for(int k=0;k<64;k++)
				            		{
				            			if(icon.equals(image[k]))
				            				dto.point[dto.stair][i][j]=k;
				            		}
				            	}
				            }
					 }
					for(int i=0;i<25;i++)
						for(int j=0;j<13;j++)
							for(int k=0;k<13;k++)
							{
								if(dto.point[i][j][k]<33)
									sb.append("-"+dto.point[i][j][k]+" ");
								else
									sb.append((dto.point[i][j][k]-32)+" ");
							}
					sb.append("\r\n");
					BufferedWriter out=new BufferedWriter(new FileWriter(filename));
					out.write(sb.toString());
					out.flush();
					out.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});
		 
	}

	@Override
	public void mouseClicked(MouseEvent e) {


	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		int i=((JLabel)(e.getSource())).getX()/40;
		int j=((JLabel)(e.getSource())).getY()/40;
		label[i][j].setIcon((Icon) image_box.getSelectedItem());
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
}

程序太简单了,主要是监听器的设计等等,没有什么难点。

可以运行的工程在这里:

https://download.csdn.net/download/dongze2/11225032

欢迎讨论!

 

你可能感兴趣的:(魔塔)