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

📄 newgameframe.java

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

package risk.ui.FlashGUI;

import risk.engine.*;

import risk.engine.guishared.RiskFileFilter;
import risk.engine.guishared.AboutDialog;

import javax.swing.JFrame;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.JLayeredPane;

import javax.swing.JFileChooser;

import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.ButtonGroup;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.ImageIcon;
import java.awt.event.*;
import javax.swing.JCheckBox;
import javax.swing.JToggleButton;
import javax.swing.AbstractButton;

import javax.swing.JOptionPane; // just needed for testing
import java.awt.*;
import java.util.ResourceBundle;

import javax.swing.text.PlainDocument;
import javax.swing.text.Document;
import javax.swing.text.AttributeSet;

/**
 * <p> New Game Frame for FlashGUI </p>
 * @author Yura Mamyrin
 * @author Christian Weiske <cweiske@cweiske.de>
 */

public class NewGameFrame extends JFrame implements ActionListener,MouseListener,KeyListener
{

	private BufferedImage newgame;
	private Risk myrisk;
	private boolean localgame;
	private JLabel mapPic;
	private JTextField cardsFile;
	private JPanel PlayersPanel;

	private JButton chooseMap;
	private JButton defaultMap;

	private JButton chooseCards;
	private JButton defaultCards;

	private JButton resetplayers;
	private JButton addplayer;

	private JButton start;
	private JButton help;
	private JButton cancel;

	private JRadioButton domination;
	private JRadioButton capital;
	private JRadioButton mission;

	private JRadioButton human;
	private JRadioButton ai;
	private JRadioButton aismart;
	private JRadioButton aicrap;

	private JRadioButton fixed;
	private JRadioButton increasing;

	private JCheckBox AutoPlaceAll;
	private JCheckBox recycle;

	private JTextField playerName;
	private JToggleButton playerColor;

	private String color;
	private Color thecolor;

	private JPanel nothing;
	private JPanel colorChooser;
	private MyColor[] Colors;

	private ResourceBundle resb;

	/**
	 * the tab focus cycle list
	 * elements are traversed in this order
	 */
	Component[] arCycleList;

	/**
	 * at which position the "remove player" buttons in the focus cycle list begin
	 */
	private int nRemoveButtonPos = 8;



	/**
	 * The NewGameFrame Constructor
	 * @param r The Risk Parser used for playing the game
	 * @param t States whether this game is local
	 */
	public NewGameFrame(Risk r)
	{
		resb = risk.engine.translation.TranslationBundle.getBundle();

		myrisk=r;


		try {
			newgame = ImageIO.read( this.getClass().getResource("newgame.jpg") );
		}
		catch (Exception e) { }

		initGUI();

		setIconImage(Toolkit.getDefaultToolkit().getImage( AboutDialog.class.getResource("icon.gif") ));

		setResizable(false);

		pack();
		chooseCards.requestFocus();

	}

	public void setup(boolean t) {

		localgame=t;

		// set title
		if (localgame) {
			setTitle(resb.getString("newgame.title.local"));
			resetplayers.setVisible(true);
		}
		else {
			setTitle(resb.getString("newgame.title.network"));
			resetplayers.setVisible(false);
		}

		Component[] players = PlayersPanel.getComponents();

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

			PlayersPanel.remove(players[c]);

		}

		nothing.setVisible(false);

	}

	static class LimitedDocument extends PlainDocument {

		public void insertString(int offs, String str, AttributeSet a) throws javax.swing.text.BadLocationException {

			if (str == null) {
				return;
			}

			if ( (getLength() + str.length()) > 15 ) {

				str = str.substring(0, str.length() - ((getLength() + str.length())-15) );
				Toolkit.getDefaultToolkit().beep();

			}

			//System.out.print("inserting: "+str+"\n");

			super.insertString(offs, str.replace('$','S') , a); // $ sign crashes the system?!
		}
	}

	/** This method is called from within the constructor to initialize the form. */

	/**
	 * Initialises the GUI
	 */
	private void initGUI()
	{

		this.setFocusTraversalPolicy( new NewGameFrameFocusTraversalPolicy());


		Colors = new MyColor[12];

		Colors[0] = new MyColor(Color.black	, "black"	, 385, 410, 25, 25, KeyEvent.VK_K);
		Colors[1] = new MyColor(Color.blue	, "blue"	, 385, 435, 25, 25, KeyEvent.VK_B);
		Colors[2] = new MyColor(Color.cyan	, "cyan"	, 385, 460, 25, 25, KeyEvent.VK_C);
		Colors[3] = new MyColor(Color.darkGray	, "darkgray"	, 410, 410, 25, 25, KeyEvent.VK_D);
		Colors[4] = new MyColor(Color.green	, "green"	, 410, 435, 25, 25, KeyEvent.VK_G);
		Colors[5] = new MyColor(Color.lightGray	, "lightgray"	, 410, 460, 25, 25, KeyEvent.VK_L);
		Colors[6] = new MyColor(Color.magenta	, "magenta"	, 435, 410, 25, 25, KeyEvent.VK_M);
		Colors[7] = new MyColor(Color.orange	, "orange"	, 435, 435, 25, 25, KeyEvent.VK_O);
		Colors[8] = new MyColor(Color.pink	, "pink"	, 435, 460, 25, 25, KeyEvent.VK_P);
		Colors[9] = new MyColor(Color.red	, "red"		, 460, 410, 25, 25, KeyEvent.VK_R);
		Colors[10] = new MyColor(Color.white	, "white"	, 460, 435, 25, 25, KeyEvent.VK_W);
		Colors[11] = new MyColor(Color.yellow	, "yellow"	, 460, 460, 25, 25, KeyEvent.VK_Y);


		Dimension d = new Dimension(700, 600);

		JLayeredPane layeredPane = new JLayeredPane();
		layeredPane.setPreferredSize(d);
		layeredPane.setMinimumSize(d);
		layeredPane.setMaximumSize(d);

		JPanel ngp = new NewGamePanel();

		ngp.setLayout(null);
		ngp.setBounds(0,0, (int)d.getWidth() , (int)d.getHeight() );

		mapPic = new JLabel();
		mapPic.setBounds(51, 51, 203 , 127 );

		int w=93;
		int h=32;

		chooseMap = new JButton(resb.getString("newgame.choosemap"));
		sortOutButton( chooseMap , newgame.getSubimage(54, 191, w, h) , newgame.getSubimage(700, 105, w, h) , newgame.getSubimage(700, 137, w, h) );
		chooseMap.addActionListener( this );
		chooseMap.setBounds(54, 192, w , h );

		defaultMap = new JButton(resb.getString("newgame.defaultmap"));
		sortOutButton( defaultMap , newgame.getSubimage(159, 192, w, h) , newgame.getSubimage(700, 169, w, h) , newgame.getSubimage(700, 201, w, h) );
		defaultMap.addActionListener( this );
		defaultMap.setBounds(159, 192, 93 , 32 );

		cardsFile = new JTextField("");
		cardsFile.setEditable(false);
		cardsFile.setBorder(null);
		cardsFile.setOpaque(false);
		cardsFile.setBounds(54, 260, 200 , 27 );

		chooseCards = new JButton(resb.getString("newgame.choosecards"));
		sortOutButton( chooseCards , newgame.getSubimage(54, 191, w, h) , newgame.getSubimage(700, 105, w, h) , newgame.getSubimage(700, 137, w, h) );
		chooseCards.addActionListener( this );
		chooseCards.setBounds(54, 301, 93 , 32 );

		defaultCards = new JButton(resb.getString("newgame.defaultcards"));
		sortOutButton( defaultCards , newgame.getSubimage(159, 192, w, h) , newgame.getSubimage(700, 169, w, h) , newgame.getSubimage(700, 201, w, h) );
		defaultCards.addActionListener( this );
		defaultCards.setBounds(159, 301, 93 , 32 );

		ButtonGroup GameTypeButtonGroup = new ButtonGroup();
		ButtonGroup CardTypeButtonGroup = new ButtonGroup();

		domination = new JRadioButton(resb.getString("newgame.mode.domination"), true);
		sortOutButton( domination );
		domination.setBounds(60, 370, 90 , 25 );
		domination.addActionListener(this);

		capital = new JRadioButton(resb.getString("newgame.mode.capital"));
		sortOutButton( capital );
		capital.setBounds(60, 390, 90 , 25 );
		capital.addActionListener(this);

		mission = new JRadioButton(resb.getString("newgame.mode.mission"));
		sortOutButton( mission );
		mission.setBounds(60, 410, 90 , 25 );
		mission.addActionListener(this);



		AutoPlaceAll = new JCheckBox(resb.getString("newgame.autoplace"));
		sortOutButton( AutoPlaceAll );
		AutoPlaceAll.setBounds(60, 440, 100 , 25 );

		recycle = new JCheckBox(resb.getString("newgame.recycle"));
		sortOutButton( recycle );
		recycle.setBounds(160, 440, 100 , 25 );



		increasing = new JRadioButton(resb.getString("newgame.cardmode.increasing"),true);
		sortOutButton( increasing );
		increasing.setBounds(160,370,90,25);

		fixed = new JRadioButton(resb.getString("newgame.cardmode.fixed"));
		sortOutButton( fixed );
		fixed.setBounds(160,390,90,25);

		//AutoEndGo = new JCheckBox("Auto End Go");
		//sortOutButton( AutoEndGo );
		//AutoEndGo.setBounds(410, 530, 100 , 25 );
		//AutoEndGo.addActionListener( this );
		//AutoEndGo.setSelected( myrisk.getAutoEndGo() );

		GameTypeButtonGroup.add ( domination );
		GameTypeButtonGroup.add ( capital );
		GameTypeButtonGroup.add ( mission );

		CardTypeButtonGroup.add ( fixed );
		CardTypeButtonGroup.add ( increasing );

		PlayersPanel = new JPanel();
		PlayersPanel.setBounds(340, 51, 309 , 210 ); // this will allow 6 players, 30 pixels per player
		PlayersPanel.setOpaque(false);
		PlayersPanel.setLayout(new javax.swing.BoxLayout(PlayersPanel, javax.swing.BoxLayout.Y_AXIS));

		w=115;
		h=31;

		resetplayers = new JButton(resb.getString("newgame.resetplayers"));
		sortOutButton( resetplayers , newgame.getSubimage(705, 488, w, h) , newgame.getSubimage(700, 357, w, h) , newgame.getSubimage(700, 388, w, h) );
		resetplayers.setBounds(437, 268, 115 , 31 );
		resetplayers.addActionListener( this );

		playerName = new JTextField(resb.getString("newgame.newplayername")) {

			protected Document createDefaultModel() {
				return new LimitedDocument();
			}

		};

		playerName.setBorder(null);
		playerName.setOpaque(false);
		playerName.setBounds(403, 335, 97 , 25);

		ButtonGroup playerTypeButtonGroup = new ButtonGroup();

		human = new JRadioButton(resb.getString("newgame.player.type.human"), true);
		sortOutButton( human );
		human.setBounds(520, 325, 100 , 20);

		aicrap = new JRadioButton(resb.getString("newgame.player.type.crapai"));
		sortOutButton( aicrap );
		aicrap.setBounds(520, 345, 100 , 20);

		ai = new JRadioButton(resb.getString("newgame.player.type.easyai"));
		sortOutButton( ai );
		ai.setBounds(520, 365, 100 , 20);

		aismart = new JRadioButton(resb.getString("newgame.player.type.hardai"));
		sortOutButton( aismart );
		aismart.setBounds(520, 385, 100 , 20);



		playerTypeButtonGroup.add ( human );
		playerTypeButtonGroup.add ( ai );
		playerTypeButtonGroup.add ( aismart );
		playerTypeButtonGroup.add ( aicrap );

		color = "black";
		thecolor = Color.black;

		playerColor = new JToggleButton("");
		sortOutButton( playerColor , newgame.getSubimage(793, 105, 19, 19) , newgame.getSubimage(793, 125, 19, 19) , newgame.getSubimage(793, 145, 19, 19) );
		playerColor.addActionListener( this );
		playerColor.setBounds(475, 370, 25 , 25);

		addplayer = new JButton(resb.getString("newgame.addplayer"));
		sortOutButton( addplayer , newgame.getSubimage(437, 413, w, h) , newgame.getSubimage(700, 419, w, h) , newgame.getSubimage(700, 450, w, h) );
		addplayer.addActionListener( this );
		addplayer.setBounds(437, 413, 115 , 31 );

		cancel = new JButton(resb.getString("newgame.cancel"));
		sortOutButton( cancel , newgame.getSubimage(41, 528, w, h) , newgame.getSubimage(700, 233, w, h) , newgame.getSubimage(700, 264, w, h) );
		cancel.addActionListener( this );
		cancel.setBounds(41, 528, 115 , 31 );

		help = new JButton(); // 335 528
		sortOutButton( help , newgame.getSubimage(781, 526, 30 , 30) , newgame.getSubimage(794, 171, 30 , 30) , newgame.getSubimage(794, 202, 30 , 30) );
		help.addActionListener( this );
		help.setBounds(335, 529, 30 , 30 ); // should be 528

		start = new JButton(resb.getString("newgame.startgame"));
		sortOutButton( start , newgame.getSubimage(544, 528, w, h) , newgame.getSubimage(700, 295, w, h) , newgame.getSubimage(700, 326, w, h) );
		start.addActionListener( this );
		start.setBounds(544, 528, 115 , 31 );

		ngp.add(mapPic);
		ngp.add(chooseMap);
		ngp.add(defaultMap);

		ngp.add(cardsFile);
		ngp.add(chooseCards);
		ngp.add(defaultCards);

		ngp.add(domination);
		ngp.add(capital);
		ngp.add(mission);

		ngp.add(fixed);
		ngp.add(increasing);

		ngp.add(PlayersPanel);

		ngp.add(playerName);
		ngp.add(human);
		ngp.add(ai);
		ngp.add(aismart);
		ngp.add(aicrap);
		ngp.add(playerColor);

		ngp.add(resetplayers);
		ngp.add(addplayer);

		ngp.add(AutoPlaceAll);
		ngp.add(recycle);

		ngp.add(cancel);
		ngp.add(help);
		ngp.add(start);

		colorChooser = new colorChooserPanel();
		colorChooser.setBounds(0,0, (int)d.getWidth() , (int)d.getHeight() );
		colorChooser.addMouseListener(this);
		colorChooser.setOpaque(false);
		colorChooser.setVisible(false);
		colorChooser.addKeyListener( this);
		playerColor.addKeyListener( this);


		nothing = new JPanel();
		nothing.setBounds(0,0, (int)d.getWidth() , (int)d.getHeight() );

⌨️ 快捷键说明

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