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

📄 mapeditor.java

📁 java 开源游戏源码 RISK 联机对战 战棋类
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// Yura Mamyrin

package risk.tools.mapeditor;

import risk.engine.*;
import risk.engine.core.*;
import risk.engine.guishared.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.border.*;
import javax.swing.colorchooser.*;
import javax.swing.filechooser.*;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.Frame;
import java.awt.Color;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
import java.awt.Cursor;

import risk.ui.SwingGUI.SwingGUITab;

/**
 * @author Yura Mamyrin
 */

public class MapEditor extends JPanel implements ActionListener, ChangeListener, SwingGUITab {

	private final static String IMAGE_MAP_EXTENSION;
	private final static String IMAGE_PIC_EXTENSION = "png";
	private final static int ZOOM_MAX = 8;
	private final static int ZOOM_MIN = 1;

	private Risk myrisk;
	private RiskGame myMap;
	private MapEditorPanel editPanel;
	private JToolBar toolbar;

	private JRadioButton move;
	private JRadioButton moveall;
	private JRadioButton join;
	private JRadioButton join1way;
	private JRadioButton disjoin;
	private JRadioButton draw;

	private JSlider fader;
	private JSlider brush;

	private JButton save;
	private JButton play;
	private JButton loadimagepic;
	private JButton loadimagemap;
	private JButton fixButton;

	private JButton zoomin;
	private JButton zoomout;
	private JTextField zoom;
	private int zoomint;

	// force cards to be in the same order as countries
	// right now risk does NOT require this
	// if ever set to true, there must be a check for this added to the check method
	private boolean strictcards;

	// i still bother with gif, coz java still is wrong sometimes when reading png color
	// not from files it saves itself but from files saved from other programs
	static {

		boolean usegif = false;

		String writerNames[] = ImageIO.getWriterFormatNames();

		for (int c=0;c<writerNames.length;c++) {

			if ("gif".equalsIgnoreCase(writerNames[c])) {

				usegif = true;
				break;
			}
		}

		if (usegif) {

			IMAGE_MAP_EXTENSION = "gif";
		}
		else {
			IMAGE_MAP_EXTENSION = "png";
		}
	}

	public MapEditor(Risk r) {

		setName( "Map Editor" );

		setOpaque(false);

		myrisk = r;

		toolbar = new JToolBar();

		toolbar.setRollover(true);
		toolbar.setFloatable(false);

		JButton newmap = new JButton("New map");
		newmap.setActionCommand("newmap");
		newmap.addActionListener(this);
		toolbar.add(newmap);

		JButton load = new JButton("Load map");
		load.setActionCommand("load");
		load.addActionListener(this);
		toolbar.add(load);

		save = new JButton("Save map");
		save.setActionCommand("save");
		save.addActionListener(this);
		toolbar.add(save);

		play = new JButton("Play Map");
		play.setActionCommand("play");
		play.addActionListener(this);
		toolbar.add(play);

		toolbar.addSeparator();

		loadimagepic = new JButton("Load Image Pic");
		loadimagepic.setActionCommand("loadimagepic");
		loadimagepic.addActionListener(this);
		toolbar.add(loadimagepic);

		loadimagemap = new JButton("Load Image Map");
		loadimagemap.setActionCommand("loadimagemap");
		loadimagemap.addActionListener(this);
		toolbar.add(loadimagemap);

		toolbar.addSeparator();

		zoomin = new JButton("Zoom in");
		zoomin.setActionCommand("zoomin");
		zoomin.addActionListener(this);
		toolbar.add(zoomin);

		zoomout = new JButton("Zoom out");
		zoomout.setActionCommand("zoomout");
		zoomout.addActionListener(this);
		toolbar.add(zoomout);

		toolbar.add( Box.createHorizontalGlue() );

		zoom = new JTextField(3);
		zoom.setEditable(false);

		Dimension size = new Dimension(25,25);

		zoom.setMaximumSize(size);
		zoom.setMinimumSize(size);
		zoom.setPreferredSize(size);

		toolbar.add(new JLabel("Zoom:"));
		toolbar.add(zoom);

		save.setEnabled(false);
		play.setEnabled(false);
		loadimagepic.setEnabled(false);
		loadimagemap.setEnabled(false);

		editPanel = new MapEditorPanel(this);

		//editPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0,0,0),1));

		JScrollPane scroll = new JScrollPane(editPanel);

		size = new Dimension(PicturePanel.PP_X , PicturePanel.PP_Y);

		scroll.setPreferredSize(size);
		scroll.setMinimumSize(size);
		scroll.setMaximumSize(size);

		scroll.setBorder(null);

		setLayout( new BorderLayout() );

		JPanel tmp = new JPanel( new BorderLayout() );
		tmp.setBorder(
			BorderFactory.createCompoundBorder(
				BorderFactory.createEmptyBorder(5,10,5,10),
				BorderFactory.createLineBorder(Color.BLACK,1)
			)
		);
		tmp.setOpaque(false);
		tmp.add(scroll);
		add( tmp );


		ButtonGroup modes = new ButtonGroup();
		JPanel modesPanel = new JPanel();
		modesPanel.setOpaque(false);

		modesPanel.add( new JLabel("Tools: ") );

		move = newJRadioButton("move",true,modes,modesPanel);
		moveall = newJRadioButton("move all",false,modes,modesPanel);
		join = newJRadioButton("join",false,modes,modesPanel);
		join1way = newJRadioButton("join 1 way",false,modes,modesPanel);
		disjoin = newJRadioButton("disjoin",false,modes,modesPanel);
		draw = newJRadioButton("draw",false,modes,modesPanel);

		fixButton = new JButton("del bad map colors");
		fixButton.setActionCommand("fix");
		fixButton.addActionListener(this);
		modesPanel.add(fixButton);
		fixButton.setEnabled(false);

		add(modesPanel, BorderLayout.SOUTH );

		JPanel topPanel = new JPanel();
		topPanel.setOpaque(false);

		fader = new JSlider(0,100,0);
		fader.addChangeListener(this);
		fader.setOpaque(false);
		fader.setMajorTickSpacing(20);
		fader.setPaintLabels(true);

		brush = new JSlider(0,100,0);
		brush.addChangeListener(this);
		brush.setOpaque(false);
		brush.setMajorTickSpacing(20);
		brush.setPaintLabels(true);

		topPanel.add( new JLabel("Image Map Fade:") );
		topPanel.add(fader);
		topPanel.add( new JLabel("Draw Brush Size:") );
		topPanel.add(brush);


		add(topPanel, BorderLayout.NORTH );

		setZoom(1);
	}

	private JRadioButton newJRadioButton(String a,boolean sel, ButtonGroup bg,JPanel jp) {

		JRadioButton b = new JRadioButton(a,sel);

		b.setActionCommand("mode");

		b.addActionListener(this);

		b.setOpaque(false);

		bg.add(b);

		jp.add(b);

		return b;

	}

	public void stateChanged(ChangeEvent e) {

		if (e.getSource() == fader) {

			editPanel.setAlpha(fader.getValue());
			editPanel.repaint();

		}
		else if (e.getSource() == brush) {

			editPanel.setBrush(brush.getValue());

		}
	}

	public JToolBar getToolBar() {

		return toolbar;

	}
	public JMenu getMenu() {

		return null;

	}


	private MapEditorViews views;
	public void setVisible(boolean v) {

		super.setVisible(v);

		if (v && views == null ) {

			views = new MapEditorViews( RiskUtil.findParentFrame(this) , editPanel );

		}

		if (views!= null) {

			if (v) {

				Frame frame = RiskUtil.findParentFrame(this);

				Dimension frameSize = frame.getSize();
				Point frameLocation = frame.getLocation();

				views.setLocation(frameLocation.x+frameSize.width, frameLocation.y);
				views.setSize(200, frameSize.height);

			}

			views.setVisible(v);

		}


	}

	public void setNewMap(RiskGame m,BufferedImage ip,BufferedImage im) {

		myMap = m;

		editPanel.setMap(myMap);
		views.setMap(myMap);

		editPanel.setImagePic(ip);
		editPanel.setImageMap(im);

		save.setEnabled(true);
		play.setEnabled(true);
		loadimagepic.setEnabled(true);
		loadimagemap.setEnabled(true);
		fixButton.setEnabled(true);

		revalidate();
		repaint();

	}

	public RiskGame makeNewMap() throws Exception {

		RiskGame rg = new RiskGame();



		for (int c=1;c<=RiskGame.MAX_PLAYERS;c++) {

			rg.addPlayer(
				Player.PLAYER_HUMAN,
				"PLAYER"+c,
				risk.engine.core.RiskGame.getColor( myrisk.getRiskConfig("default.player"+c+".color") ),
				null
			);



		}

		return rg;
	}

	public void actionPerformed(ActionEvent a) {

		if (a.getActionCommand().equals("newmap")) {

			try {

				RiskGame map = makeNewMap();
				map.setupNewMap();

				BufferedImage ipic = new BufferedImage(PicturePanel.PP_X , PicturePanel.PP_Y, BufferedImage.TYPE_INT_BGR);
				BufferedImage imap = new BufferedImage(PicturePanel.PP_X , PicturePanel.PP_Y, BufferedImage.TYPE_INT_BGR); // @YURA:TODO only works with this, but should be something else
				Graphics g = imap.getGraphics();
				g.setColor( Color.WHITE );
				g.fillRect(0,0,PicturePanel.PP_X , PicturePanel.PP_Y);
				g.dispose();

				setNewMap(map,ipic,imap);

			}
			catch(Exception ex) {

				showError(ex);

			}

		}
		else if (a.getActionCommand().equals("load")) {


		    try {

			String name = RiskUtil.getNewFile( RiskUtil.findParentFrame(this), "map" );

			setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

			if (name!=null) {

				RiskGame map = makeNewMap();
				map.setMapfile(name); // this is here just to update the cards option
				map.loadMap(name);
				map.loadCards( map.getCardsFile() );

				BufferedImage ipic = makeRGBImage( ImageIO.read(RiskUtil.openMapStream(map.getImagePic()) ) );
				BufferedImage imap = makeRGBImage( ImageIO.read(RiskUtil.openMapStream(map.getImageMap()) ) );

				map.setMemoryLoad();

				setNewMap(map,ipic,imap);
			}

		    }
		    catch(Exception ex) {

			showError(ex);

		    }

		    setCursor(null);

		}
		else if (a.getActionCommand().equals("save")) {

		    checkMap();

		    try {

			JFileChooser fc = new JFileChooser( new File(new URI(RiskUtil.mapsdir.toString())) );
			RiskFileFilter filter = new RiskFileFilter(RiskFileFilter.RISK_MAP_FILES);
			fc.setFileFilter(filter);

			int returnVal = fc.showSaveDialog( RiskUtil.findParentFrame(this) );

			if (returnVal == JFileChooser.APPROVE_OPTION) {

				File file = fc.getSelectedFile();


				String fileName = file.getName();

				if (!(fileName.endsWith( "." + RiskFileFilter.RISK_MAP_FILES ))) {
					fileName = fileName + "." + RiskFileFilter.RISK_MAP_FILES;
				}


				saveMap( fileName );

			}

		    }
		    catch(Throwable ex) {

			showError(ex);

		    }

		}
		else if (a.getActionCommand().equals("play")) {

			if ( checkMap() ) {

				myrisk.newMemoryGame(myMap,editPanel.getImagePic() );

			}

		}
		else if (a.getActionCommand().equals("loadimagepic")) {

		    try {


			JFileChooser fc = new JFileChooser( new File(new URI(RiskUtil.mapsdir.toString())) );

			int returnVal = fc.showOpenDialog( RiskUtil.findParentFrame(this) );

			if (returnVal == JFileChooser.APPROVE_OPTION) {

				editPanel.setImagePic( makeRGBImage(ImageIO.read( fc.getSelectedFile() )) );

				revalidate();
				repaint();


			}

		    }
		    catch(Throwable ex) {

			showError(ex);
		    }

		}
		else if (a.getActionCommand().equals("loadimagemap")) {

		    try {

			JFileChooser fc = new JFileChooser( new File(new URI(RiskUtil.mapsdir.toString())) );

			int returnVal = fc.showOpenDialog( RiskUtil.findParentFrame(this) );

			if (returnVal == JFileChooser.APPROVE_OPTION) {

				editPanel.setImageMap( makeRGBImage(ImageIO.read( fc.getSelectedFile() )) );

				repaint();

			}

		    }
		    catch(Throwable ex) {

			showError(ex);
		    }

		}
		else if (a.getActionCommand().equals("mode")) {

			if (move.isSelected()) {

				editPanel.setMode(MapEditorPanel.MODE_MOVE);

			}
			else if (moveall.isSelected()) {

				editPanel.setMode(MapEditorPanel.MODE_MOVEALL);

			}
			else if (join.isSelected()) {

				editPanel.setMode(MapEditorPanel.MODE_JOIN);

			}
			else if (join1way.isSelected()) {

				editPanel.setMode(MapEditorPanel.MODE_JOIN1WAY);

⌨️ 快捷键说明

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