gameframe.java

来自「java 开源游戏源码 RISK 联机对战 战棋类」· Java 代码 · 共 1,480 行 · 第 1/3 页

JAVA
1,480
字号
							note=resb.getString("game.note.selectdestination");

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

							pp.setC2(pixColor);
							note="";
							repaint();

							openMove(1, pp.getC1() , pixColor, true);

							movedialog.setVisible(true);

							pp.setC1(255);
							pp.setC2(255);

							c1Id = -1;
							note=resb.getString("game.note.selectsource");
							repaint();
						}

					}
					else if (gameState == RiskGame.STATE_SELECT_CAPITAL) {

						if ( myrisk.isOwnedCurrentPlayerInt(pixColor) ) {

							c1Id = pixColor;
							pp.setC1(pixColor);
							pp.repaint();
						}
					}

				}
				else if ( e.getModifiers() == java.awt.event.InputEvent.BUTTON3_MASK ) {

					if (pixColor == 255 ) {

					}
					else if (gameState == RiskGame.STATE_PLACE_ARMIES) {
						if ( myrisk.isOwnedCurrentPlayerInt(pixColor) && (setupDone || myrisk.hasArmiesInt(pixColor) == 0) ) {
							go( "placearmies " + pixColor + " 10" );
						}
					}
				}
			//}
		}
		else { // click in the window

			int click=insideButton(e.getX(),e.getY());

			if (click != -1) { // this means it was one of the view buttons

				if (mapView !=click) {

					setMapView(click);

				}

			}

		}

	}//public void mouseReleased()

	public void mouseDragged(MouseEvent e) {
	}

	/**
	 * This method is used in the general playing of the game
	 * It highlights the country the mouse is over
	 * @param e The Mousevent object
	 */
	public void mouseMoved(MouseEvent e) {

		if (e.getComponent() == gm) { // move on the menu

		}
		else if (e.getComponent() == pp) { // move on the picture panel

			//System.out.print("Map Move\n");

			int pixColor = pp.getCountryNumber(e.getX(),e.getY());
			int cc;

			if (pixColor == 255 ) {
				cc = 255;
			}
			else if (gameState == RiskGame.STATE_PLACE_ARMIES) {
				if ( myrisk.isOwnedCurrentPlayerInt(pixColor) && (setupDone || myrisk.hasArmiesInt(pixColor) == 0) ) {
					cc = pixColor;
				}
				else {
					cc = 255;
				}
			}
			else if ( gameState == RiskGame.STATE_ATTACKING) {

				if ( myrisk.isOwnedCurrentPlayerInt(pixColor) && (myrisk.hasArmiesInt(pixColor) > 1) ) {
					cc = pixColor;
				}
				else if ( !(myrisk.isOwnedCurrentPlayerInt(pixColor)) && c1Id != -1 && myrisk.canAttack( c1Id , pixColor) ) {
					cc = pixColor;
				}
				else {
					cc = 255;
				}

			}
			else if ( gameState == RiskGame.STATE_FORTIFYING) {

				if ( myrisk.isOwnedCurrentPlayerInt(pixColor) && (myrisk.hasArmiesInt(pixColor) > 1 && c1Id == -1 ) ) {
					cc = pixColor;
				}
				else if ( myrisk.isOwnedCurrentPlayerInt(pixColor) && c1Id != -1 && myrisk.canAttack( c1Id , pixColor) ) {
					cc = pixColor;
				}
				else {
					cc = 255;
				}

			}
			else if (gameState == RiskGame.STATE_SELECT_CAPITAL) {
				if ( myrisk.isOwnedCurrentPlayerInt(pixColor) ) {
					cc = pixColor;
				}
				else {
					cc = 255;
				}
			}
			else {
				cc = 255;

			}

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

			e.consume();


		}
		else { // move on the window

		}


	}//public void mouseMoved(...)



	/**
	 * the map view has changed
	 * (normal, border threat, ...)
	 * @param click		The tab number the user has clicked on
	 */
	private void setMapView(int click)
	{
		mapView = click;
		repaintCountries();
		repaint();
	}//private void setMapView(int click)



	/**
	 * displays the cards dialog
	 */
	private void displayCards() {

		cardsDialog.setup( (gameState==RiskGame.STATE_TRADE_CARDS) );

		cardsDialog.setVisible(true);

	}



	/**
	 * displays the mission window
	 */
	private void displayMission()
	{
		MissionDialog missiondialog = new MissionDialog(GameFrame.this, true, myrisk);

		Dimension frameSize = getSize();
		Dimension aboutSize = missiondialog.getSize();
		int x = getLocation().x + (frameSize.width - aboutSize.width) / 2;
		int y = getLocation().y + (frameSize.height - aboutSize.height) / 2;
		if (x < 0) x = 0;
		if (y < 0) y = 0;
		missiondialog.setLocation(x, y);

		missiondialog.setVisible(true);
	}//private void displayMission()



	/**
	 * does an "undo"
	 */
	private void doUndo()
	{
		go("undo");
	}//private void doUndo()



	/**
	 * displays the menu
	 */
	private void displayMenu() {

	    if(menuOn) {

		gm.setVisible(false);
		pp.addMouseListener(GameFrame.this);
		pp.addMouseMotionListener(GameFrame.this);

		menuOn=false;

	    }
	    else {

		pp.removeMouseListener(GameFrame.this);
		pp.removeMouseMotionListener(GameFrame.this);

		if (myrisk.getGame().getCurrentPlayer()!=null) {

			AutoEndGo.setSelected( myrisk.getAutoEndGo() );
			AutoDefend.setSelected( myrisk.getAutoDefend() );

		}

		gm.setVisible(true);
		menuOn=true;
	    }

	}

	public void displayGraph() {

		if (graphOn) {
			graphdialog.setVisible(false);
			graphOn=false;
		}
		else {

			if (graphdialog==null) {

				graphdialog = new StatsDialog(GameFrame.this, false, myrisk);

				Dimension frameSize = (GameFrame.this).getPreferredSize();
				Dimension graphSize = graphdialog.getSize();
				int a = (GameFrame.this).getLocation().x + (frameSize.width - graphSize.width) / 2;
				int b = (GameFrame.this).getLocation().y + (frameSize.height - graphSize.height) / 2;
				if (a < 0) a = 0;
				if (b < 0) b = 0;
				graphdialog.setLocation(a, b);

			}

			graphdialog.setVisible(true);
			graphOn=true;
		}

	}

	/**
	 * the user has clicked the "go" button
	 */
	private void goOn()
	{
		if (gameState==RiskGame.STATE_TRADE_CARDS) {
			go("endtrade");
		}
		else if (gameState==RiskGame.STATE_PLACE_ARMIES) {
			go("autoplace");
		}
		else if (gameState==RiskGame.STATE_ATTACKING) {
			pp.setC1(255);
			go("endattack");
		}
		else if (gameState==RiskGame.STATE_FORTIFYING) {
			pp.setC1(255);
			go("nomove");
		}
		else if (gameState==RiskGame.STATE_END_TURN) {
			go("endgo");
		}
		else if (gameState==RiskGame.STATE_GAME_OVER) {
			closeleave();
		}
		else if (gameState == RiskGame.STATE_SELECT_CAPITAL) {
			pp.setC1(255);
			go("capital " + c1Id);
		}
	}//private void goOn()



	public static JButton makeRiskButton(Image gobutton1, Image gobutton2, Image gobutton3, Image gobutton4, boolean startstate) {

		JButton button = new JButton();

		NewGameFrame.sortOutButton( button, gobutton1, gobutton3, gobutton2 );

		button.setDisabledIcon( new ImageIcon( gobutton4 ) );
		button.setEnabled(startstate);

		return button;

	}



	/**
	 * The user has released a key
	 */
	public void keyReleased( KeyEvent event ) {

		//if (event.isControlDown()) {

			//with CTRL down

			// when the ctrl+number bit was here is gave the "?" key when the number 6 was pressed

		//} else {

			switch (event.getKeyCode())
			{
				case KeyEvent.VK_1: this.setMapView(PicturePanel.VIEW_CONTINENTS); break;
				case KeyEvent.VK_2: this.setMapView(PicturePanel.VIEW_OWNERSHIP); break;
				case KeyEvent.VK_3: this.setMapView(PicturePanel.VIEW_BORDER_THREAT); break;
				case KeyEvent.VK_4: this.setMapView(PicturePanel.VIEW_CARD_OWNERSHIP); break;
				case KeyEvent.VK_5: this.setMapView(PicturePanel.VIEW_TROOP_STRENGTH); break;
				case KeyEvent.VK_6: this.setMapView(PicturePanel.VIEW_CONNECTED_EMPIRE); break;

					// can not use this as it may be not a int
					//Integer.parseInt( event.getKeyChar() + ""));
					
			}

			//no modifier button pressed
			switch (event.getKeyCode())
			{
				case KeyEvent.VK_C:
					//cards
					if (gameState!=RiskGame.STATE_NEW_GAME) this.displayCards();
					break;
				case KeyEvent.VK_M:
					//mission
					if (gameState!=RiskGame.STATE_NEW_GAME) this.displayMission();
					break;
				case KeyEvent.VK_U:
					//undo
					if (gameState!=RiskGame.STATE_NEW_GAME && gameState!=RiskGame.STATE_DEFEND_YOURSELF) this.doUndo();
					break;
				case KeyEvent.VK_F10:
					this.displayMenu();
					//menu
					break;
				case KeyEvent.VK_G:
					//go button
					if (gameState!=RiskGame.STATE_NEW_GAME) this.goOn();
					break;
			}
		//}
	}//public void keyReleased( KeyEvent event )

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

	/**
	 * the game menu
	 * which you get when pressing the "Menu" button
	 */
	class GameMenuPanel extends JPanel
	{

		public GameMenuPanel()
		{

			setLayout(null);

			int w=100;

			savebutton = makeRiskButton(game.getSubimage(480, 373, w, 21), game.getSubimage(380, 373, w, 21), game.getSubimage(280, 373, w, 21), game.getSubimage(180, 373, w, 21), false );
			savebutton.setText(resb.getString("game.menu.save"));
			savebutton.setBounds(35, 50, w , 20 );
			savebutton.addActionListener( buttonActionListener );

			closebutton = makeRiskButton(game.getSubimage(480, 373, w, 21), game.getSubimage(380, 373, w, 21), game.getSubimage(280, 373, w, 21), game.getSubimage(180, 373, w, 21), true );

			closebutton.setBounds(35, 80, w , 20 );
			closebutton.addActionListener( buttonActionListener );





			AutoEndGo = new JCheckBox(resb.getString("game.menu.autoendgo"));
			AutoEndGo.setToolTipText( resb.getString("game.menu.autoendgo"));

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

			AutoEndGo.setBounds(35, 110, w , 20 );
			AutoEndGo.addActionListener( buttonActionListener );
			AutoEndGo.setBackground( Color.lightGray );

			AutoEndGo.setEnabled(false);





			AutoDefend = new JCheckBox(resb.getString("game.menu.autodefend"));
			AutoDefend.setToolTipText( resb.getString("game.menu.autodefend"));

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

			AutoDefend.setBounds(35, 140, w , 20 );
			AutoDefend.addActionListener( buttonActionListener );
			AutoDefend.setBackground( Color.lightGray );

			AutoDefend.setEnabled(false);




			helpbutton = makeRiskButton(game.getSubimage(480, 373, w, 21), game.getSubimage(380, 373, w, 21), game.getSubimage(280, 373, w, 21), game.getSubimage(180, 373, w, 21), true );
			helpbutton.setText(resb.getString("game.menu.manual"));
			helpbutton.setBounds(35, 170, w , 20 );
			helpbutton.addActionListener( buttonActionListener );




			resumebutton = makeRiskButton(game.getSubimage(480, 373, w, 21), game.getSubimage(380, 373, w, 21), game.getSubimage(280, 373, w, 21), game.getSubimage(180, 373, w, 21), true );
			resumebutton.setText(resb.getString("game.menu.closemenu"));
			resumebutton.setBounds(35, 200, w , 20 );
			resumebutton.addActionListener( buttonActionListener );

			add(savebutton);
			add(AutoDefend);
			add(helpbutton);
			add(AutoEndGo);
			add(closebutton);
			add(resumebutton);

		}

		public void paintComponent(Graphics g) {

			Graphics2D g2 = (Graphics2D)g;

			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

			AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f);
			g2.setComposite(ac);

//					  destination		source
			g2.drawImage(game,0,0,170,250,     633,175,803,425,this); // top

			FontRenderContext frc = g2.getFontRenderContext();

			Font font = new java.awt.Font("Arial", java.awt.Font.BOLD, 24);
			g2.setColor( Color.black );
			TextLayout tl;

			tl = new TextLayout( resb.getString("game.menu.title") , font, frc);
			tl.draw( g2, (float) (85-tl.getBounds().getWidth()/2), (float)40 );

		}

	}




}

⌨️ 快捷键说明

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