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

📄 newgameframe.java

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

			Component[] players = PlayersPanel.getComponents();

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

				myrisk.parser("delplayer " + ((playerPanel)players[c]).getName() );

			}

			resetPlayers();

		}

		else if (e.getSource()==addplayer) {

			String type="";

			if (human.isSelected())	{ type = "human"; }
			else if (ai.isSelected())	{ type = "ai easy"; }
			else if (aismart.isSelected())	{ type = "ai hard"; }
			else			{ type = "ai crap"; }

			myrisk.parser("newplayer "+ type +" "+ color +" "+ playerName.getText() );

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

			Component[] players = PlayersPanel.getComponents();

			if (
					(players.length >= 2 && players.length <= risk.engine.core.RiskGame.MAX_PLAYERS )
					// || (players.length == 2 && domination.isSelected() && ((playerPanel)players[0]).getType() == 0 && ((playerPanel)players[1]).getType() == 0 )
			) {

				String type="";
				if (domination.isSelected()) type = "domination";
				else if (capital.isSelected()) type = "capital";
				else if (mission.isSelected()) type = "mission";

				if (increasing.isSelected()) type += " increasing";
				else if (fixed.isSelected()) type += " fixed";

				myrisk.parser("startgame " + type + (( AutoPlaceAll.isSelected() )?(" autoplaceall"):("")) + (( recycle.isSelected() )?(" recycle"):("")) );

			}
			else {

				JOptionPane.showMessageDialog(this, resb.getString("newgame.error.numberofplayers") , resb.getString("newgame.error.title"), JOptionPane.ERROR_MESSAGE );

			}

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

			try {
				RiskUtil.openDocs( resb.getString("helpfiles.flash") );
			}
			catch(Exception er) {
				JOptionPane.showMessageDialog(this,"Unable to open manual: "+er.getMessage(),"Error", JOptionPane.ERROR_MESSAGE);
			}

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


			exitForm();


		}
		else if (e.getSource()==playerColor) {
			colorChooser.setVisible(playerColor.isSelected());

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

			AutoPlaceAll.setEnabled(false);

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

			AutoPlaceAll.setEnabled(true);

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

			AutoPlaceAll.setEnabled(true);

		}
	}

	/**
	 * Applies the correct command to the button pressed
	 * @param e The MouseEvent Object
	 */
	public void mouseClicked(MouseEvent e) {

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

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

				if (e.getX() >= Colors[c].getX() && e.getX() < Colors[c].getX()+Colors[c].getWidth() && e.getY() >= Colors[c].getY() && e.getY() < Colors[c].getY()+Colors[c].getHeight() ) {
					setSelectedPlayerColor( Colors[c]);
					break;
				}
			}

			colorChooser.setVisible(false);//this line is absolutely required
			playerColor.setSelected(false);
		}
	}

	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
	public void mousePressed(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) {}


	/**
	 * the user has selected a player color on the player color panel
	 */
	protected void setSelectedPlayerColor(MyColor col)
	{
		color		= col.getName();
		thecolor	= col.getColor();
	}//protected void setSelectedPlayerColor(MyColor col)



	/**
	 * Resets the players
	 */
	public void resetPlayers()
	{

		myrisk.parser("autosetup");
/*
		String strUsername = "appletuser";

		if (Risk.applet == null) {

			strUsername = System.getProperty("user.name");
		}

		myrisk.parser("newplayer human   green   " + strUsername);
		myrisk.parser("newplayer ai easy blue    bob");
		myrisk.parser("newplayer ai easy red     fred");
		myrisk.parser("newplayer ai easy yellow  ted");
		myrisk.parser("newplayer ai hard magenta yura");
		myrisk.parser("newplayer ai hard cyan    lala");
*/
	}//public void resetPlayers()

	class MyColor {

		private Color color;
		private String name;
		private int myX;
		private int myY;
		private int myW;
		private int myH;
		private int keyCode;

		/**
		 * Sets the players details accorind to color
		 * @param c The color
		 * @param n The name
		 * @param x The x-coordinate
		 * @param y The y-coordinate
		 * @param w The width
		 * @param h The height
		 */
		public MyColor(Color c, String n, int x, int y, int w, int h, int keyCode) {

			color=c;
			name=n;
			myX=x;
			myY=y;
			myW=w;
			myH=h;
			this.keyCode = keyCode;

		}

		/**
		 * Returns the color
		 * @return Color
		 */
		public Color getColor() {
			return color;
		}

		/**
		 * Returns the name
		 * @return String
		 */
		public String getName() {
			return name;
		}

		/**
		 * Returns the x-coordinate
		 * @return int
		 */
		public int getX() {
			return myX;
		}

		/**
		 * Returns the y-coordinate
		 * @return int
		 */
		public int getY() {
			return myY;
		}

		/**
		 * Returns the width
		 * @return int
		 */
		public int getWidth() {
			return myW;
		}

		/**
		 * Returns the height
		 * @return int
		 */
		public int getHeight() {
			return myH;
		}

		/**
		 * returns the key code for this color
		 */
		public int getKeyCode() {
			return this.keyCode;
		}

	}

	/**
	 * Block the gamepanel
	 */
	public void noInput() {

		nothing.setVisible(true);
		//System.out.print("BLOCK INPUT\n");

	}

	/**
	 * Sorts the buttons out and adds rollover images to the button
	 * @param button Button
	 * @param button1 Image
	 * @param button2 Image
	 * @param button3 Image
	 */
	public static void sortOutButton(AbstractButton button, Image button1, Image button2, Image button3) {

		button.setIcon( new ImageIcon( button1 ) );
		button.setSelectedIcon( button.getIcon());
		button.setRolloverIcon( new ImageIcon( button2 ) );
		button.setPressedIcon( new ImageIcon( button3 ) );

		button.setMargin(new Insets(0,0,0,0));
		button.setBorderPainted(false);
		button.setFocusPainted(false);
		button.setContentAreaFilled(false);

		button.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

		button.addFocusListener( new ImageButtonFocusListener(button));
	}

	/**
	 * Makes the button look neat
	 * @param button Button
	 */
	public static void sortOutButton(AbstractButton button) {

		button.setMargin(new Insets(0,0,0,0));
		button.setBorderPainted(false);
//		button.setFocusPainted(false);
		button.setContentAreaFilled(false);

		button.addFocusListener( new ImageButtonFocusListener(button));
	}


	/**
	 * That the user can tab through the tabs in the right order
	 * @author Christian Weiske <cweiske@cweiske.de>
	 */
	class NewGameFrameFocusTraversalPolicy extends FocusTraversalPolicy
	{

		public Component getComponentAfter( Container container, Component component )
		{
			int nPos = getComponentIndex( component) + 1;
			if (nPos >= arCycleList.length) {
				nPos = 0;
			}

			//remove buttons could be null, so ignore them
			while (arCycleList[nPos] == null && nPos < arCycleList.length) {
				nPos++;
			}

			return arCycleList[nPos];
		}

		public Component getComponentBefore( Container container, Component component )
		{
			int nPos = getComponentIndex( component) - 1;
			if (nPos < 0) {
				nPos = arCycleList.length - 1;
			}

			//remove buttons could be null, so ignore them
			while (arCycleList[nPos] == null && nPos > 0) {
				nPos--;
			}

			return arCycleList[nPos];
		}

		/**
		 * returns the index of the component in the cycle array
		 */
		private int getComponentIndex(Component comp)
		{
			for( int nA = 0; nA < arCycleList.length; nA++ ) {
				if (arCycleList[nA] == comp) {
					return nA;
				}
			}
			return -1;
		}//private int getComponentIndex(Component comp)

		public Component getFirstComponent( Container container )
		{
			return arCycleList[0];
		}

		public Component getLastComponent( Container container )
		{
			return arCycleList[arCycleList.length - 1];
		}

		public Component getDefaultComponent( Container container )
		{
			return arCycleList[0];
		}
	}//class NewGameFrameFocusTraversalPolicy extends FocusTraversalPolicy



	/**
	 * the user has released a key on the colorchooser panel
	 */
	public void keyReleased( KeyEvent event )
	{
		if (event.getSource() != colorChooser
			&& event.getSource() != playerColor) {
			return;
		}

		for( int nA = 0; nA < Colors.length; nA++ ) {
			if (Colors[nA].getKeyCode() == event.getKeyCode()) {
				this.colorChooser.setVisible( true);
				this.setSelectedPlayerColor( Colors[nA]);
				this.colorChooser.setVisible( false);
				this.playerColor.setSelected( false);
				break;
			}
		}
	}//public void keyReleased( KeyEvent event )


	public void keyTyped( KeyEvent event ) {}
	public void keyPressed( KeyEvent event ){}

}//public class NewGameFrame extends JFrame implements ActionListener,MouseListener,KeyListener



/**
 * Makes the button highlighted when they have focus
 * and resets the image if they loose it
 */
class ImageButtonFocusListener implements FocusListener
{
	AbstractButton button;
	ImageButtonFocusListener(AbstractButton button) {
		this.button = button;
	}
	public void focusGained( FocusEvent event )
	{
		if (button.getIcon() == null) {
			//no icon, so change the bg color
			//button.setBackground( Color.WHITE );
			//button.setContentAreaFilled( true);
		} else {
			button.setIcon( button.getRolloverIcon());
		}
	}

	public void focusLost( FocusEvent event )
	{
		//this works only because we set the selected icon
		//to the same as the normal icon
		if (button.getSelectedIcon() == null) {
			//no icon, so reset bg color
			//button.setBackground( null);
			//button.setContentAreaFilled( false);
		} else {
			button.setIcon( button.getSelectedIcon());
		}
	}
}//class ImageButtonFocusListener


⌨️ 快捷键说明

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