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

📄 swingguipanel.java

📁 java 开源游戏源码 RISK 联机对战 战棋类
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
				mapPic.setIcon(null);

			}

			SwingGUIPanel.this.setCursor(null); // Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)

		}

		public void showCardsFile(String c, boolean m) {

			cardsFile.setText(c);

			if ( m==false && mission.isSelected() ) { domination.setSelected(true); AutoPlaceAll.setEnabled(true); }

			mission.setEnabled(m);

		}

		public void newGame(boolean t) { // t==true: this is a local game

			localGame = t;

			gameTab.newGame();

		}

		private boolean memoryload;

		/**
		 * Starts the game
		 * @param s If the game is a local game
		 */
		public void startGame(boolean s) {

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

			localGame = s;

			// check maybe we should load from memory
			if (myrisk.getGame().getMapFile() == null && myrisk.getGame().getCardsFile() == null) {

				pp.memoryLoad(editorTab.getImageMap(),editorTab.getImagePic());

			}
			else {

				try {
					pp.load();
				}
				catch(IOException e) {
					e.printStackTrace();
				}

			}



			// YURA: not sure why this needs to be here, used to work without it
			pprepaintCountries();

			gameTab.startGame();

			statisticsTab.startGame();




			SwingGUIPanel.this.setCursor(null); // Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)

		}

		/**
		 * Closes the game
		 */
		public void closeGame() {

			gameTab.closeGame();

			statisticsTab.closeGame();

			System.gc();

		}

		public void setSlider(int min, int c1num, int c2num) {

			int max = myrisk.hasArmiesInt( c1num ) -1;

			slider.setMaximum(max);
			slider.setMinimum(min);
			slider.setValue(min);

		}

		public void armiesLeft(int l, boolean s) {
			armies.setText( resbundle.getString("core.input.armiesleft").replaceAll("\\{0\\}", "" + l));

			if (s) {
				autoplace.setVisible(false);
			}
			else {
				autoplace.setVisible(true);
			}
		}

		public void showDice(int n, boolean w) {

			JPanel p;

			if (w) { p=roll; }
			else { p=defend; }

			p.remove(roll1);
			p.remove(roll2);
			p.remove(roll3);

			GridBagConstraints c = new GridBagConstraints();
			c.insets = new java.awt.Insets(3, 3, 3, 3);
			c.fill = GridBagConstraints.BOTH;

			if (n > 0) {

				c.gridx = 0; // col
				c.gridy = 0; // row
				c.gridwidth = 1; // width
				c.gridheight = 1; // height
				p.add(roll1, c);


				if (n > 1) {

					c.gridx = 1; // col
					c.gridy = 0; // row
					c.gridwidth = 1; // width
					c.gridheight = 1; // height
					p.add(roll2, c);


					if (n > 2) {

						c.gridx = 2; // col
						c.gridy = 0; // row
						c.gridwidth = 1; // width
						c.gridheight = 1; // height
						p.add(roll3, c);

					}

				}

			}

		}

		public void showDiceResults(int[] att, int[] def) {

			String output=resbundle.getString("core.dice.results");

			output = output + " " + resbundle.getString("core.dice.attacker");
			for (int c=0; c< att.length ; c++) {
				output = output + " " + (att[c]+1);
			}

			output = output + " " + resbundle.getString("core.dice.defender");
			for (int c=0; c< def.length ; c++) {
				output = output + " " + (def[c]+1);
			}

			resultsLabel.setText(output);

			inGameCards.show(inGameInput, "results");

		}

		public void closeBattle() {

			blockInput();

		}

		public void serverState(boolean s) {

			gameTab.serverState(s);

		}

		public void addPlayer(int t, String name, Color color, String ip) {

			guiSetup.addPlayer(t, name, color, ip);

		}

		public void delPlayer(String name) {

			guiSetup.delPlayer(name);

		}

/*  // other things that need to be done

public void openBattle(int c1num, int c2num) {}
public void setNODAttacker(int n) {}
public void setNODDefender(int n) {}

*/

	}

	//############################################################################################################

	class playersPanel extends JPanel {

		public void paintComponent(Graphics g) {

			Color[] colors = myrisk.getPlayerColors();

			for (int c=0; c < colors.length ; c++) {
				g.setColor( colors[c] );
				g.fillRect( ((120/colors.length) * c) , 0 , (120/colors.length) , 20);
			}

			g.setColor( RiskUtil.getTextColorFor( colors[0] ) );

			g.drawRect( 2 , 2 , (120/colors.length)-5 , 15);

			g.setColor( Color.black );
			g.drawLine( (120/colors.length)-1 , 0, (120/colors.length)-1, 19);

		}

	}




	public JPanel makeGameOptionsPanel() {

			JPanel gameOptionsPanel = new JPanel();

			gameOptionsPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 10, 0));

			gameOptionsPanel.setOpaque(false);

			JLabel mapLookLabel = new JLabel(resbundle.getString("game.tabs.mapview") + ":");

			mapViewComboBox = new JComboBox();

			Dimension mapViewSize = new Dimension(120 , 20);

			mapViewComboBox.setPreferredSize(mapViewSize);
			mapViewComboBox.setMinimumSize(mapViewSize);
			mapViewComboBox.setMaximumSize(mapViewSize);

			mapViewComboBox.addItem(resbundle.getString("game.tabs.continents"));
			mapViewComboBox.addItem(resbundle.getString("game.tabs.ownership"));
			mapViewComboBox.addItem(resbundle.getString("game.tabs.borderthreat"));
			mapViewComboBox.addItem(resbundle.getString("game.tabs.cardownership"));
			mapViewComboBox.addItem(resbundle.getString("game.tabs.troopstrength"));
			mapViewComboBox.addItem(resbundle.getString("game.tabs.connectedempire"));

			mapViewComboBox.addActionListener(
					new ActionListener() {
						public void actionPerformed(ActionEvent a) {

							pprepaintCountries();

							pp.repaint();
						}
					}
			);

			JLabel playersLabel = new JLabel(resbundle.getString("newgame.label.players"));

			Dimension playerPanelSize = new Dimension(120 , 20);

			JPanel players = new playersPanel();

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

			players.setPreferredSize(playerPanelSize);
			players.setMinimumSize(playerPanelSize);
			players.setMaximumSize(playerPanelSize);

			gameOptionsPanel.add(mapLookLabel);
			gameOptionsPanel.add(mapViewComboBox);
			gameOptionsPanel.add(playersLabel);
			gameOptionsPanel.add(players);
			gameOptionsPanel.add(showMission);
			gameOptionsPanel.add(showCards);
			gameOptionsPanel.add(Undo);

			return gameOptionsPanel;

	}


	class GamePanel extends JPanel implements MouseInputListener {

		public GamePanel() {

			resultsLabel = new JLabel("RESULTS");

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

			pp.setPreferredSize(mapSize);
			pp.setMinimumSize(mapSize);
			pp.setMaximumSize(mapSize);
			pp.addMouseListener(this);
			pp.addMouseMotionListener(this);

			Dimension d = new Dimension(PicturePanel.PP_X , 50);

			inGameCards = new CardLayout();

			inGameInput = new JPanel();
			inGameInput.setLayout( inGameCards );
			inGameInput.setPreferredSize(d);
			inGameInput.setMinimumSize(d);
			inGameInput.setMaximumSize(d);

			JPanel nothing = new JPanel();
			nothing.setPreferredSize(d);
			nothing.setMinimumSize(d);
			nothing.setMaximumSize(d);
			nothing.add(new JLabel(resbundle.getString("game.pleasewaitnetwork")) );

			JPanel results = new JPanel();
			results.setPreferredSize(d);
			results.setMinimumSize(d);
			results.setMaximumSize(d);
			results.add(resultsLabel);

			JPanel placeArmies = new placeArmiesPanel();
			placeArmies.setPreferredSize(d);
			placeArmies.setMinimumSize(d);
			placeArmies.setMaximumSize(d);

			roll = new rollPanel();
			roll.setPreferredSize(d);
			roll.setMinimumSize(d);
			roll.setMaximumSize(d);

			JPanel move = new movePanel();
			move.setPreferredSize(d);
			move.setMinimumSize(d);
			move.setMaximumSize(d);

			JPanel attack = new attackPanel();
			attack.setPreferredSize(d);
			attack.setMinimumSize(d);
			attack.setMaximumSize(d);

			defend = new defendPanel();
			defend.setPreferredSize(d);
			defend.setMinimumSize(d);
			defend.setMaximumSize(d);

			JPanel tacMove = new tacMovePanel();
			tacMove.setPreferredSize(d);
			tacMove.setMinimumSize(d);
			tacMove.setMaximumSize(d);

			JPanel capital = new capitalPanel();
			capital.setPreferredSize(d);
			capital.setMinimumSize(d);
			capital.setMaximumSize(d);

			JPanel tradeCards = new tradeCardsPanel();
			tradeCards.setPreferredSize(d);
			tradeCards.setMinimumSize(d);
			tradeCards.setMaximumSize(d);

			JPanel winner = new winnerPanel();
			winner.setPreferredSize(d);
			winner.setMinimumSize(d);
			winner.setMaximumSize(d);

			JPanel endgo = new endgoPanel();
			endgo.setPreferredSize(d);
			endgo.setMinimumSize(d);
			endgo.setMaximumSize(d);

			inGameInput.setOpaque(false);

			nothing.setOpaque(false);
			placeArmies.setOpaque(false);
			roll.setOpaque(false);
			move.setOpaque(false);
			attack.setOpaque(false);
			defend.setOpaque(false);
			tacMove.setOpaque(false);
			capital.setOpaque(false);
			tradeCards.setOpaque(false);
			winner.setOpaque(false);
			endgo.setOpaque(false);
			results.setOpaque(false);

			inGameInput.add(nothing, "nothing");
			inGameInput.add(placeArmies, "placeArmies");
			inGameInput.add(roll, "roll");
			inGameInput.add(move, "move");
			inGameInput.add(attack, "attack");
			inGameInput.add(defend, "defend");
			inGameInput.add(tacMove, "tacMove");
			inGameInput.add(capital, "capital");
			inGameInput.add(tradeCards, "tradeCards");
			inGameInput.add(winner, "winner");
			inGameInput.add(endgo, "endgo");
			inGameInput.add(results, "results");

			// ################### IN GAME #######################
/*

			this.setLayout(new java.awt.GridBagLayout());

			GridBagConstraints c = new GridBagConstraints();
			c.insets = new java.awt.Insets(3, 3, 3, 3);
			c.fill = GridBagConstraints.BOTH;

			c.gridx = 0; // col
			c.gridy = 0; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(gameOptions, c);

			c.gridx = 0; // col
			c.gridy = 1; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(pp, c);

			c.gridx = 0; // col
			c.gridy = 2; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(inGameInput, c);

*/
			JPanel ppBorder = new JPanel( new BorderLayout() );
			ppBorder.setBorder(
				BorderFactory.createCompoundBorder(
					BorderFactory.createEmptyBorder(5,10,5,10),
					BorderFactory.createLineBorder(Color.BLACK,1)
				)
			);
			ppBorder.setOpaque(false);
			ppBorder.add(pp);

			setLayout(new BorderLayout());
			add(gameOptions, BorderLayout.NORTH);
			add(ppBorder);
			add(inGameInput, BorderLayout.SOUTH);

			setBorder( BorderFactory.createEmptyBorder(5,0,5,0) );

		}

		//**********************************************************************
		//                     MouseListener Interface
		//**********************************************************************

		public void mouseClicked(MouseEvent e) {
		}

		public void mouseEntered(MouseEvent e) {
		}

		public void mouseExited(MouseEvent e) {

			if (pp.getHighLight() != PicturePanel.NO_COUNTRY) {
				pp.setHighLight(PicturePanel.NO_COUNTRY);
				pp.repaint();
			}

			e.consume();

		}

		public void mousePressed(MouseEvent e) {
		}

		public void mouseReleased(MouseEvent e) {

			//if ((e.getX() < PicturePanel.PP_X) && (e.getY() < PicturePanel.PP_Y) && (e.getX() >= 0) && (e.getY() >= 0) ) {

				int pixColor = pp.getCountryNumber(e.getX(),e.getY());
				String name = myrisk.getCountryName( pixColor );

				if ( e.getModifiers() == java.awt.event.InputEvent.BUTTON1_MASK ) {

					// System.out.print(e.getX()+" "+e.getY()+" "+PicturePanel.PP_X+" "+PicturePanel.PP_Y+"\n"); // testing
					// Testing.append("Click (color: " + pixColor + ")\n");

					if (pixColor == PicturePanel.NO_COUNTRY ) {

					}
					else if (gameState == RiskGame.STATE_PLACE_ARMIES) {
						if (myrisk.isOwnedCurrentPlayerInt(pixColor) && (autoplace.isVisible()==false || myrisk.hasArmiesInt(pixColor) == 0)) {
							go( "placearmies " + pixColor + " 1" );
						}
					}
					else if (gameState == RiskGame.STATE_ATTACKING) {
						if ( pixColor == c1Id ) {

							clearAttackPanel();
						}
						else if ( myrisk.isOwnedCurrentPlayerInt(pixColor) && ( myrisk.hasArmiesInt(pixColor) > 1) ) {
							attacker.setText(resbundle.getString("game.note.attackerisseldefender").replaceAll( "\\{0\\}", myrisk.getCountryName( pixColor)));
							c1Id = pixColor;

							pp.setC1(pixColor);
							pp.setC2(PicturePanel.NO_COUNTRY);

							pp.repaint();
						}
						else if ( !(myrisk.isOwnedCurrentPlayerInt(pixColor)) && c1Id != -1 && myrisk.canAttack( c1Id , pixColor) ) {
							pp.setC2(pixColor);

							go("attack " + c1Id

⌨️ 快捷键说明

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