gameframe.java

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

JAVA
1,480
字号
// Yura Mamyrin, Group D

package risk.ui.FlashGUI;

import risk.engine.guishared.*;

import risk.engine.*;
import risk.engine.core.Country;
import risk.engine.core.RiskGame;

import javax.swing.JFrame;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.Toolkit;
import java.awt.Dimension;
import javax.swing.JLayeredPane;
import javax.swing.event.MouseInputListener;

import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.Insets;
import java.awt.event.*;

import java.awt.font.TextLayout;
import java.awt.font.FontRenderContext;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.AlphaComposite;

import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import java.awt.RenderingHints;
import javax.swing.JOptionPane;
import java.awt.BorderLayout;
import java.util.Iterator;
import java.util.List;
import java.util.Arrays;
import java.util.Vector;

/**
 * <p> Game Frame for FlashGUI </p>
 * @author Yura Mamyrin
 */

public class GameFrame extends JFrame implements MouseInputListener,KeyListener {

	private BufferedImage game;
	private Risk myrisk;
	private PicturePanel pp;
	private GameMenuPanel gm;
	private int mapView;
	private String gameStatus;
	private boolean localGame;
	private int gameState;
	private String note;
	private boolean setupDone;

	private JButton cardsbutton;
	private JButton missionbutton;
	private JButton undobutton;
	private JButton menubutton;
	private JButton graphbutton;
	private JButton gobutton;

	private java.util.ResourceBundle resb;
	private StatsDialog graphdialog;

	private boolean menuOn;
	private boolean graphOn;

	private Color[] colors;
	private CardsDialog cardsDialog;

	private int c1Id;
	private MoveDialog movedialog;


	private JButton savebutton;
	private JButton resumebutton;

	private JCheckBox AutoEndGo;
	private JCheckBox AutoDefend;

	private JButton helpbutton;
	private JButton closebutton;


	public GameFrame(Risk r, PicturePanel p) {

		//setResizable(false);

		//setGameStatus(null); // not needed?

		myrisk=r;
		pp=p;

		menuOn=false;
		graphOn=false;

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

		initGUI();

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

		pack();

		graphdialog = null;

		try {

			setMinimumSize( getPreferredSize() );

		}
		catch(NoSuchMethodError ex) {

			// must me java 1.4
			setResizable(false);

		}

	}

	public void setup(boolean s) {

		gameState=0; // -1 or 0 means no input needed
		mapView=PicturePanel.VIEW_CONTINENTS;

		//gameStatus="";
		setGameStatus(null);

		note="";
		setupDone=true;
		c1Id = -1;

		localGame = s;

		if (localGame) { closebutton.setText(resb.getString("game.menu.close")); }
		else { closebutton.setText(resb.getString("game.menu.leave")); }

		repaintCountries();

	}

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

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

		resb = risk.engine.translation.TranslationBundle.getBundle();

		// set title
		setTitle("yura.net " + RiskUtil.getGameName() ); // resb.getString("game.title")

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

		int l=715,m=6;

		final BufferedImage topleft=game.getSubimage(63,0,l,54);
		final BufferedImage topmiddle=game.getSubimage(63+l,0,m,54);
		final BufferedImage topright=game.getSubimage(63+l+m,0,740-(l+m),54);

		JPanel fp = new JPanel() {
		    public void paintComponent(Graphics g) {

			//		  destination		source
			//g.drawImage(game,0,0,740,54,     63,0,803,54,this); // top

			g.drawImage(topleft,0,0,this);
			for (int c=topleft.getWidth();c<getWidth()-topright.getWidth();c=c+topmiddle.getWidth() ) {
				g.drawImage(topmiddle,c,0,this);
			}
			g.drawImage(topright,getWidth()-topright.getWidth(),0,this);

			Graphics2D g2 = (Graphics2D)g;

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

			FontRenderContext frc = g2.getFontRenderContext();
			Font font = g2.getFont();
			g2.setColor( Color.BLACK );
			TextLayout tl;

			tl = new TextLayout( resb.getString("game.tabs.continents") , font, frc);
			tl.draw( g2, (float) (81-tl.getBounds().getWidth()/2), (float)26 );

			tl = new TextLayout( resb.getString("game.tabs.ownership") , font, frc);
			tl.draw( g2, (float) (196-tl.getBounds().getWidth()/2), (float)26 );

			tl = new TextLayout( resb.getString("game.tabs.borderthreat") , font, frc);
			tl.draw( g2, (float) (311-tl.getBounds().getWidth()/2), (float)26 );

			tl = new TextLayout( resb.getString("game.tabs.cardownership") , font, frc);
			tl.draw( g2, (float) (426-tl.getBounds().getWidth()/2), (float)26 );

			tl = new TextLayout( resb.getString("game.tabs.troopstrength") , font, frc);
			tl.draw( g2, (float) (541-tl.getBounds().getWidth()/2), (float)26 );

			tl = new TextLayout( resb.getString("game.tabs.connectedempire") , font, frc);
			tl.draw( g2, (float) (656-tl.getBounds().getWidth()/2), (float)26 );



			if (mapView==PicturePanel.VIEW_CONTINENTS) {
				g.drawImage(game,24,32,139,39,   64,383,179,390,this);
			}
			else if (mapView==PicturePanel.VIEW_OWNERSHIP) {
				g.drawImage(game,139,32,254,39,     64,390,179,397,this);
			}
			else if (mapView==PicturePanel.VIEW_BORDER_THREAT) {
				g.drawImage(game,254,32,369,39,     64,397,179,404,this);
			}
			else if (mapView==PicturePanel.VIEW_CARD_OWNERSHIP) {
				g.drawImage(game,369,32,484,39,     64,404,179,411,this);
			}
			else if (mapView==PicturePanel.VIEW_TROOP_STRENGTH) {
				g.drawImage(game,484,32,599,39,     64,411,179,418,this);
			}
			else if (mapView==PicturePanel.VIEW_CONNECTED_EMPIRE) {
				g.drawImage(game,599,32,714,39,     64,418,179,425,this);
			}

			g.drawLine(30,53,getWidth()-32,53);

		    }
		};
		//fp.setBounds(0,0, (int)d.getWidth() , (int)d.getHeight() );
		fp.addMouseListener(this);
		//fp.addMouseMotionListener(this);


		l=551;
		m=6;

		final BufferedImage bottomleft=game.getSubimage(63,54,l,121);
		final BufferedImage bottommiddle=game.getSubimage(63+l,54,m,121);
		final BufferedImage bottomright=game.getSubimage(63+l+m,54,740-(l+m),121);

		JPanel fpBottom = new JPanel() {
		    public void paintComponent(Graphics g) {

			//g.drawImage(game,0,0,740,121,  63,54,803,175,this); // bottom

			g.drawImage(bottomleft,0,0,this);
			for (int c=bottomleft.getWidth();c<getWidth()-bottomright.getWidth();c=c+bottommiddle.getWidth() ) {
				g.drawImage(bottommiddle,c,0,this);
			}
			g.drawImage(bottomright,getWidth()-bottomright.getWidth(),0,this);

			Graphics2D g2 = (Graphics2D)g;

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


			Color[] colorss = colors;

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

				Color col = colorss[c];

				g.setColor( new Color(col.getRed(),col.getGreen(),col.getBlue(), 100) );

				if (c==0) {
					g.fillArc(8, 89, 24 , 24, 90, 180);
					g.fillRect( 20 , 89 , (getWidth()-173)-(24*(colorss.length-c)) , 24);
				}
				else {
					g.fillRect( (getWidth()-177)-(24*(colorss.length-c)) , 89 , 24 , 24);
				}

			}

			if (gameStatus!=null) {

				g.setColor( RiskUtil.getTextColorFor( colors[0] ) );
				g.setFont( new java.awt.Font("SansSerif", java.awt.Font.PLAIN, 11) );
				g.drawString(gameStatus, 22, 105);
			}

			g2.setColor( Color.BLACK );

			TextLayout tl=null;
			FontRenderContext frc = g2.getFontRenderContext();
			Font font = g2.getFont();

			if ( gameState==RiskGame.STATE_NEW_GAME ) {
				tl = new TextLayout( resb.getString("game.pleasewait") , font, frc);
			}
			else if ( (gameState==RiskGame.STATE_TRADE_CARDS || gameState==RiskGame.STATE_PLACE_ARMIES || gameState==RiskGame.STATE_ATTACKING || gameState==RiskGame.STATE_SELECT_CAPITAL || gameState==RiskGame.STATE_FORTIFYING ) && !(note.equals("")) ) {
				tl = new TextLayout( note , font, frc);
			}

			if (tl!=null) {
				tl.draw( g2, (float) (getWidth()-84-tl.getBounds().getWidth()/2), (float)57 );
			}


			g.drawLine(30,0,getWidth()-32,0);

		    }

		    public void setBounds(int x,int y,int w,int h) {
			super.setBounds(x,y,w,h);
			gobutton.setBounds(getWidth()-140, 74, 115 , 31 );
		    }
		};
		fpBottom.setLayout(null);

		JPanel fpLeft = new JPanel() {
		    public void paintComponent(Graphics g) {
			// getHeight() = 425
			g.drawImage(game,0,0,31,getHeight(),    0,0,31,425,this); // left
			g.setColor(Color.BLACK);
			g.drawLine(30,0,30,getHeight());
		    }
		};

		JPanel fpRight = new JPanel() {
		    public void paintComponent(Graphics g) {
			// getHeight() = 425
			g.drawImage(game,0,0,32,getHeight(), 31,0,63,425,this); // right
			g.setColor(Color.BLACK);
			g.drawLine(0,0,0,getHeight());
		    }
		};

		int w=114;
		int h=66;
		// normal - pressed - rollover - disabled
		int x=63;
		int y=77;

		graphbutton = makeRiskButton(game.getSubimage(x, y, w, h), game.getSubimage(x, y+230, w, h), game.getSubimage(x, y+164, w, h), game.getSubimage(x, y+98, w, h), true );
		graphbutton.setBounds(x-63, y-54, w , h );
		graphbutton.addActionListener( buttonActionListener );
		graphbutton.setToolTipText( resb.getString("game.button.statistics") );

		x=x+w;

		cardsbutton = makeRiskButton(game.getSubimage(x, y, w, h), game.getSubimage(x, y+230, w, h), game.getSubimage(x, y+164, w, h), game.getSubimage(x, y+98, w, h), false );
		cardsbutton.setBounds(x-63, y-54, w , h );
		cardsbutton.addActionListener( buttonActionListener );
		cardsbutton.setToolTipText(resb.getString("game.button.cards"));

		x=x+w;

		missionbutton = makeRiskButton(game.getSubimage(x, y, w, h), game.getSubimage(x, y+230, w, h), game.getSubimage(x, y+164, w, h), game.getSubimage(x, y+98, w, h), false );
		missionbutton.setBounds(x-63, y-54, w , h );
		missionbutton.addActionListener( buttonActionListener );
		missionbutton.setToolTipText(resb.getString("game.button.mission"));

		x=x+w;

		undobutton = makeRiskButton(game.getSubimage(x, y, w, h), game.getSubimage(x, y+230, w, h), game.getSubimage(x, y+164, w, h), game.getSubimage(x, y+98, w, h), false );
		undobutton.setBounds(x-63, y-54, w , h );
		undobutton.addActionListener( buttonActionListener );
		undobutton.setToolTipText(resb.getString("game.button.undo"));

		x=x+w;

		menubutton = makeRiskButton(game.getSubimage(x, y, w, h), game.getSubimage(x, y+230, w, h), game.getSubimage(x, y+164, w, h), game.getSubimage(x, y+98, w, h), true );
		menubutton.setBounds(x-63, y-54, w , h );
		menubutton.addActionListener( buttonActionListener );
		menubutton.setToolTipText( resb.getString("game.button.menu") );



		w=115;
		h=31;
		gobutton = makeRiskButton(game.getSubimage(663, 128, w, h), game.getSubimage(412, 394, w, h), game.getSubimage(296, 394, w, h), game.getSubimage(180, 394, w, h), false );
		gobutton.addActionListener( buttonActionListener );

		fpBottom.add(graphbutton);
		fpBottom.add(cardsbutton);
		fpBottom.add(missionbutton);
		fpBottom.add(undobutton);
		fpBottom.add(menubutton);
		fpBottom.add(gobutton);

		//pp.setBounds(31, 54, PicturePanel.PP_X , PicturePanel.PP_Y);
		//pp.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0,0,0),1));
		pp.addMouseListener(this);
		pp.addMouseMotionListener(this);
		pp.setBackground(Color.BLACK);

		gm = new GameMenuPanel();
		gm.setVisible(false);
		gm.setBounds(285, 141, 170 , 250);
		//gm.addMouseListener(this);
		//gm.addMouseMotionListener(this);
		getRootPane().getLayeredPane().add(gm , JLayeredPane.MODAL_LAYER);

		//layeredPane.add(gm, 0);
		//layeredPane.add(pp, 1);
		//layeredPane.add(fp, 2);

		//getContentPane().add(layeredPane);

		setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

		addWindowListener(
			new java.awt.event.WindowAdapter() {
				public void windowClosing(java.awt.event.WindowEvent evt) {
					closeleave();
				}
			}
		);

		this			.addKeyListener( this);
		cardsbutton		.addKeyListener( this);
		missionbutton		.addKeyListener( this);
		undobutton		.addKeyListener( this);
		menubutton		.addKeyListener( this);
		graphbutton		.addKeyListener( this);
		gobutton		.addKeyListener( this);

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

		cardsDialog = new CardsDialog(GameFrame.this, true, myrisk, pp);

		Dimension frameSize = cardsDialog.getSize();
		frameSize.height = ((frameSize.height > screenSize.height) ? screenSize.height : frameSize.height);
		frameSize.width = ((frameSize.width > screenSize.width) ? screenSize.width : frameSize.width);
		cardsDialog.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);


		movedialog = new MoveDialog(this, false);

		frameSize = movedialog.getSize();
		frameSize.height = ((frameSize.height > screenSize.height) ? screenSize.height : frameSize.height);
		frameSize.width = ((frameSize.width > screenSize.width) ? screenSize.width : frameSize.width);
		movedialog.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);

		// #################################################################################
		// sort out size and add to main panel

		Dimension d = new Dimension(740, 54);
		fp.setPreferredSize(d);
		fp.setMinimumSize(d);
		fp.setMaximumSize(d);

		d = new Dimension(740, 121);
		fpBottom.setPreferredSize(d);
		fpBottom.setMinimumSize(d);
		fpBottom.setMaximumSize(d);

		d = new Dimension(31,425);
		fpLeft.setPreferredSize(d);
		fpLeft.setMinimumSize(d);
		fpLeft.setMaximumSize(d);

		d = new Dimension(32,425);
		fpRight.setPreferredSize(d);
		fpRight.setMinimumSize(d);
		fpRight.setMaximumSize(d);

		JPanel flashPanel = new JPanel( new BorderLayout() );
		flashPanel.add(fp,BorderLayout.NORTH);
		flashPanel.add(pp);
		flashPanel.add(fpBottom,BorderLayout.SOUTH);
		flashPanel.add(fpLeft,BorderLayout.WEST);
		flashPanel.add(fpRight,BorderLayout.EAST);

		getContentPane().add( flashPanel );

	}

	ActionListener buttonActionListener = new ActionListener() {
		public void actionPerformed(ActionEvent e) {

			if (e.getSource()==cardsbutton) {
				displayCards();
			}
			else if (e.getSource()==missionbutton) {
				displayMission();
			}
			else if (e.getSource()==undobutton) {
				doUndo();
			}
			else if (e.getSource()==menubutton) {
				displayMenu();
			}

⌨️ 快捷键说明

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