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

📄 mapeditorviews.java

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

package risk.tools.mapeditor;

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

import java.awt.event.WindowEvent;
import javax.swing.JDialog;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.awt.event.MouseEvent;
import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.Toolkit;
import java.awt.Dimension;
import javax.swing.JLayeredPane;
import java.awt.event.MouseListener;
import javax.swing.JTextField;
import java.awt.Frame;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTabbedPane;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import java.awt.BorderLayout;
import javax.swing.AbstractListModel;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JToolBar;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.SpinnerNumberModel;
import javax.swing.JSpinner;
import javax.swing.JColorChooser;
import java.awt.Color;
import javax.swing.JTextArea;
import java.util.List;
import java.util.Vector;
import java.util.Arrays;
import java.util.Map;
import java.util.HashMap;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;

/**
 * <p> Options Dialog for SwingGUI </p>
 * @author Yura Mamyrin
 */

public class MapEditorViews extends JDialog implements ActionListener,ListSelectionListener {

    private MapEditorPanel editPanel;

    private RiskGame map;

    private JList countriesList;
    private JList continentsList;
    private JList cardsList;
    private JList missionsList;

    private JTabbedPane tabs;
    private String[] cardTypes;

    private JButton add;
    private JButton remove;
    private JButton edit;

    public MapEditorViews(Frame parent,MapEditorPanel ep) {

        super(parent, "Map Editor Views", false);

	editPanel = ep;

	JToolBar optionspanel = new JToolBar();

	add = new JButton("Add");
	add.setActionCommand("add");
	add.addActionListener( this );
	optionspanel.add(add);

	remove = new JButton("Remove");
	remove.setActionCommand("remove");
	remove.addActionListener( this );
	optionspanel.add(remove);

	edit = new JButton("Edit");
	edit.setActionCommand("edit");
	edit.addActionListener( this );
	optionspanel.add(edit);

	add.setEnabled(false);
	remove.setEnabled(false);
	edit.setEnabled(false);

	tabs = new JTabbedPane();

	countriesList = new JList( new CountriesListModel() );
	continentsList = new JList( new ContinentsListModel() );
	cardsList = new JList( new CardsListModel() );
	missionsList = new JList( new MissionsListModel() );

	tabs.addTab("Countries",new JScrollPane( countriesList ));
	tabs.addTab("Continents",new JScrollPane( continentsList ));
	tabs.addTab("Cards",new JScrollPane( cardsList ));
	tabs.addTab("Missions",new JScrollPane( missionsList ));

	getContentPane().add(optionspanel, BorderLayout.SOUTH);
	getContentPane().add(tabs);

	cardTypes = new String[4];
	cardTypes[0] = Card.CAVALRY;
	cardTypes[1] = Card.INFANTRY;
	cardTypes[2] = Card.CANNON;
	cardTypes[3] = Card.WILDCARD;

	countriesList.addListSelectionListener(this);

    }

    public void valueChanged(ListSelectionEvent e) {

	editPanel.setCountry( (Country)countriesList.getSelectedValue() );

    }

    public void setMap(RiskGame m) {

	map = m;

	((ViewTab)countriesList.getModel()).changed();
	((ViewTab)continentsList.getModel()).changed();
	((ViewTab)cardsList.getModel()).changed();
	((ViewTab)missionsList.getModel()).changed();

	add.setEnabled(true);
	remove.setEnabled(true);
	edit.setEnabled(true);

    }

    public void actionPerformed(ActionEvent a) {

	ViewTab current = ((ViewTab)((JList)((javax.swing.JViewport)((JScrollPane)tabs.getSelectedComponent()).getViewport()).getView()).getModel());


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

		current.add();

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

		current.remove();

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

		current.edit();

	}	
	else {

		throw new RuntimeException(a.getActionCommand());

	}

    }

	public int openOptionDialog(Object[] message,String title) {

			String[] options = {
			    "OK", 
			    "cancel"
			}; 

			int result = JOptionPane.showOptionDialog( 
			    this,                             // the parent that the dialog blocks 
			    message,                                    // the dialog message array 
			    title, // the title of the dialog window 
			    JOptionPane.OK_CANCEL_OPTION,                 // option type 
			    JOptionPane.PLAIN_MESSAGE,            // message type 
			    null,                                       // optional icon, use null to use the default icon 
			    options,                                    // options string array, will be made into buttons 
			    options[0]                                  // option that should be made into a default button 
			);

			return result;

	}

	public void removeCountries(Object[] a) {

		if (a.length>0) {

				Vector countries = new Vector( Arrays.asList( map.getCountries() ) );
				List removeList = Arrays.asList(a);

				countries.removeAll( removeList );

				Country[] newCountries = (Country[])countries.toArray( new Country[countries.size()] );

				Map updateMap = new HashMap();
				updateMap.put(new Integer(255),new Integer(255));

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

					updateMap.put( new Integer(((Country)a[c]).getColor()), new Integer(255) );

				}

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

					updateMap.put( new Integer(newCountries[c].getColor()), new Integer(c+1) );
					newCountries[c].setColor(c+1);
					newCountries[c].getNeighbours().removeAll( removeList );
				}

				Vector cards = map.getCards();

				for (int c=0;c<cards.size();c++) {

					Card card = (Card)cards.elementAt(c);

					if (removeList.contains(card.getCountry())) {

						cards.remove(c);
						c--;
					}

				}


				Continent[] continents = map.getContinents();

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

					continents[c].getTerritoriesContained().removeAll( removeList );

				}


				map.setCountries(newCountries);

				editPanel.update(updateMap);

				//countriesList.revalidate();
				//cardsList.revalidate();
				//countriesList.clearSelection();
				//cardsList.clearSelection();

				((ViewTab)countriesList.getModel()).changed();
				((ViewTab)cardsList.getModel()).changed();

		}

	}

	interface ViewTab {

		void edit();
		void remove();
		void add();
		void changed();
	}


    class CountriesListModel extends AbstractListModel implements ViewTab {

	public void changed() {
		fireContentsChanged(this,0, map.getNoCountries()-1 );
		countriesList.clearSelection();
	}

	public Object getElementAt(int index) {

		if (map==null) { return null; }
		return map.getCountries()[index];

	}

	public int getSize() {

		if (map==null) { return 0; }
		return map.getNoCountries();

	}

	public void edit() {

		int sel = countriesList.getSelectedIndex();

		if (sel != -1) {

			Country country = (Country)getElementAt(sel);
			Continent oldContinent = country.getContinent();

			Object[] message = new Object[4];
			message[0] = new JLabel("Name:");
			message[1] = new JTextField( country.getIdString().replace('_',' ') );
			message[2] = new JLabel("Continent:");
			message[3] = new JComboBox( map.getContinents() );

			((JComboBox)message[3]).setSelectedItem( oldContinent );

			int result = openOptionDialog(message,"Edit country");

			if (result == JOptionPane.OK_OPTION ) {

				Continent newContinent = (Continent)((JComboBox)message[3]).getSelectedItem();

				String name = ((JTextField)message[1]).getText();

				country.setIdString( name.replace(' ','_') );
				country.setName( name );
				country.setContinent( newContinent );

				oldContinent.getTerritoriesContained().remove(country);
				newContinent.addTerritoriesContained(country);

				fireContentsChanged(this,sel, sel );

				editPanel.repaint();
			}

		}

	}
	public void remove() {

		Object[] a = countriesList.getSelectedValues();

		if (a.length!=0) {

			String countriesString="";

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

				countriesString = countriesString + "\n" + a[c];

			}

			int result = JOptionPane.showConfirmDialog(MapEditorViews.this, "Are you sure you want to remove:"+
			countriesString
			, "Remove?", JOptionPane.YES_NO_OPTION);

			if (result == JOptionPane.YES_OPTION) {

				removeCountries( a );

			}



		}

	}
	public void add() {


			if (map.getContinents().length == 0) {

				JOptionPane.showMessageDialog(MapEditorViews.this,"Need to add a continent first");
				return;

			}


			Object[] message = new Object[4];
			message[0] = new JLabel("Names: (each line will be made into a new country)");
			message[1] = new OptionPaneTextArea( "" );
			message[2] = new JLabel("Select continent:");
			message[3] = new JComboBox( map.getContinents() );

			int result = openOptionDialog(message,"New countries");

			if (result == JOptionPane.OK_OPTION ) {

				String[] names = ((OptionPaneTextArea)message[1]).getLines();

				Country[] oldCountries = map.getCountries();

				if (oldCountries.length+names.length > 254) {

					JOptionPane.showMessageDialog(MapEditorViews.this,"Too many countries. 254 is the max number.");
					return;

				}

				Vector newCountries = new Vector( Arrays.asList(oldCountries) );

				Continent continent = (Continent)((JComboBox)message[3]).getSelectedItem();

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

					Country country = new Country(oldCountries.length+c+1,names[c].replace(' ','_'),names[c],continent,20*(c+1),20);
					newCountries.add(country);
					continent.addTerritoriesContained(country);
				}

				map.setCountries( (Country[])newCountries.toArray( new Country[newCountries.size()] ) );

				fireContentsChanged(this,oldCountries.length, oldCountries.length+names.length-1 );

				editPanel.repaint();
			}


	}

    }


    class ContinentsListModel extends AbstractListModel implements ViewTab {

	public void changed() {
		super.fireContentsChanged(this, 0, map.getNoContinents()-1 );
		continentsList.clearSelection();
	}

	public Object getElementAt(int index) {

		if (map==null) { return null; }
		return map.getContinents()[index];

	}

	public int getSize() {

		if (map==null) { return 0; }
		return map.getNoContinents();

	}

	private Color editcolor;

	public void edit() {

		int sel = continentsList.getSelectedIndex();

		if (sel != -1) {

			Continent continent = (Continent)getElementAt(sel);

			final Object[] message = new Object[6];
			message[0] = new JLabel("Name:");
			message[1] = new JTextField( continent.getIdString().replace('_',' ') );
			message[2] = new JLabel("Army Value:");
			message[3] = new JSpinner( new SpinnerNumberModel(continent.getArmyValue(),0,100,1) );
			message[4] = new JLabel("Color:");
			message[5] = new JButton("change");

			editcolor = continent.getColor();

			((JButton)message[5]).setBackground( editcolor );
			((JButton)message[5]).setForeground( RiskUtil.getTextColorFor(editcolor) );

			((JButton)message[5]).addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent a) {

						Color newcolor = JColorChooser.showDialog(MapEditorViews.this,"select color",editcolor);

						if (newcolor!=null) {

							editcolor = newcolor;

							((JButton)message[5]).setBackground( editcolor );
							((JButton)message[5]).setForeground( RiskUtil.getTextColorFor(editcolor) );
						}

					}
				}
			);

			int result = openOptionDialog(message,"Edit continent");

			if (result == JOptionPane.OK_OPTION ) {

				String name = ((JTextField)message[1]).getText();

				continent.setIdString( name.replace(' ','_') );
				continent.setName(name);
				continent.setArmyValue( ((Integer)((JSpinner)message[3]).getValue()).intValue() );
				continent.setColor(editcolor);

				fireContentsChanged(this,sel, sel );

				editPanel.repaint();
			}

		}

	}

⌨️ 快捷键说明

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