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

📄 testpanel.java

📁 java 开源游戏源码 RISK 联机对战 战棋类
💻 JAVA
字号:
// Yura Mamyrin

package risk.ui.SwingGUI;

import risk.engine.*;
import risk.engine.core.*;
import risk.engine.ai.AIPlayer;
import risk.engine.guishared.PicturePanel;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.border.*;
import javax.swing.filechooser.*;
import java.awt.image.BufferedImage;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.net.*;
import javax.swing.table.AbstractTableModel;

/**
 * @author Yura Mamyrin
 */

public class TestPanel extends JPanel implements ActionListener, SwingGUITab {

	private Risk myrisk;

	private JToolBar toolbar;

	private AbstractTableModel countriesModel;
	private AbstractTableModel continentsModel;
	private AbstractTableModel cardsModel;
	private AbstractTableModel playersModel;

	private PicturePanel pp;

	public TestPanel(Risk r,PicturePanel p) {

		myrisk = r;
		pp=p;

		setName( "Testing" );

		setOpaque(false);

		toolbar = new JToolBar();

		toolbar.setRollover(true);
		toolbar.setFloatable(false);

		JButton refresh = new JButton("Refresh");
		refresh.setActionCommand("refresh");
		refresh.addActionListener(this);
		toolbar.add(refresh);

		JButton allcards = new JButton("All Cards");
		allcards.setActionCommand("allcards");
		allcards.addActionListener(this);
		toolbar.add(allcards);

		toolbar.addSeparator();

		JButton flash = new JButton("Run FlashGUI with current backend");
		flash.setActionCommand("flash");
		flash.addActionListener(this);
		toolbar.add(flash);

		toolbar.addSeparator();

		JButton changeaiwait = new JButton("Change AI wait");
		changeaiwait.setActionCommand("aiwait");
		changeaiwait.addActionListener(this);
		toolbar.add(changeaiwait);

		countriesModel = new AbstractTableModel() {

			private final String[] columnNames = { "Color/No.","ID", "Name", "Continent", "Owner" , "Armies","No. Neighbours", "x", "y" };

			public int getColumnCount() {
				return columnNames.length;
			}

			public int getRowCount() {

				RiskGame game = myrisk.getGame();

				if (game != null) {

					Country[] countries = game.getCountries();

					if (countries != null) {

						return countries.length;
					}
				}

				return 0;
  			}

			public String getColumnName(int col) {
				return columnNames[col];
			}

			public Object getValueAt(int row, int col) {

				Country country = myrisk.getGame().getCountries()[row];

				switch(col) {

					case 0: return new Integer( country.getColor() );
					case 1: return country.getIdString();
					case 2: return country.getName();
					case 3: return country.getContinent();
					case 4: return country.getOwner();
					case 5: return new Integer( country.getArmies() );
					case 6: return new Integer( country.getNeighbours().size() );
					case 7: return new Integer( country.getX() );
					case 8: return new Integer( country.getY() );
					default: throw new RuntimeException();

				}

			}

		};




		continentsModel = new AbstractTableModel() {

			private final String[] columnNames = { "No.", "ID","Name", "Army Value", "No. Countries","Color" };

			public int getColumnCount() {
				return columnNames.length;
			}

			public int getRowCount() {

				RiskGame game = myrisk.getGame();

				if (game != null) {

					Continent[] continents = game.getContinents();

					if (continents != null) {

						return continents.length;
					}
				}

				return 0;
  			}

			public String getColumnName(int col) {
				return columnNames[col];
			}

			public Object getValueAt(int row, int col) {

				Continent continent = myrisk.getGame().getContinents()[row];

				switch(col) {

					case 0: return new Integer( row+1 );
					case 1: return continent.getIdString();
					case 2: return continent.getName();
					case 3: return new Integer( continent.getArmyValue() );
					case 4: return new Integer( continent.getTerritoriesContained().size() );
					case 5: return RiskUtil.getStringForColor( continent.getColor() );
					default: throw new RuntimeException();

				}

			}

		};


		cardsModel = new AbstractTableModel() {

			private final String[] columnNames = { "No.", "Type", "Country" };

			public int getColumnCount() {
				return columnNames.length;
			}

			public int getRowCount() {

				RiskGame game = myrisk.getGame();

				if (game != null) {

					Vector cards = game.getCards();

					if (cards != null) {

						return cards.size();
					}
				}

				return 0;
  			}

			public String getColumnName(int col) {
				return columnNames[col];
			}

			public Object getValueAt(int row, int col) {

				Card card = (Card)myrisk.getGame().getCards().elementAt(row);

				switch(col) {

					case 0: return new Integer( row+1 );
					case 1: return card.getName();
					case 2: return card.getCountry();
					default: throw new RuntimeException();

				}

			}

		};



		playersModel = new AbstractTableModel() {

			private final String[] columnNames = { "Name", "Color", "Type", "Extra Armies", "No. Cards", "No. Countries", "No. Player Eliminated", "Capital", "Mission", "Address", "autodefend","autoendgo"};

			public int getColumnCount() {
				return columnNames.length;
			}

			public int getRowCount() {

				RiskGame game = myrisk.getGame();

				if (game != null) {

					Vector players = game.getPlayers();

					if (players != null) {

						return players.size();
					}
				}

				return 0;
  			}

			public String getColumnName(int col) {
				return columnNames[col];
			}

			public Object getValueAt(int row, int col) {

				Player player = (Player)myrisk.getGame().getPlayers().elementAt(row);

				switch(col) {

					case 0: return player.getName();
					case 1: return RiskUtil.getStringForColor( player.getColor() );
					case 2: switch( player.getType() ) {

							case Player.PLAYER_HUMAN: return "Human";
							case Player.PLAYER_AI_CRAP: return "AI Crap";
							case Player.PLAYER_AI_EASY: return "AI Easy";
							case Player.PLAYER_AI_HARD: return "AI Hard";
							default: throw new RuntimeException();

						}
					case 3: return new Integer( player.getExtraArmies() );
					case 4: return new Integer( player.getCards().size() );
					case 5: return new Integer( player.getNoTerritoriesOwned() );
					case 6: return new Integer( player.getPlayersEliminated().size() );
					case 7: return player.getCapital();
					case 8: return player.getMission();
					case 9: return player.getAddress();
					case 10: return new Boolean( player.getAutoDefend() );
					case 11: return new Boolean( player.getAutoEndGo() );
					default: throw new RuntimeException();

				}

			}

		};

		JTabbedPane views = new JTabbedPane();

		views.add( "Countries" , new JScrollPane(new JTable(countriesModel)) );
		views.add( "Continents" , new JScrollPane(new JTable(continentsModel)) );
		views.add( "Cards" , new JScrollPane(new JTable(cardsModel)) );
		views.add( "Players" , new JScrollPane(new JTable(playersModel)) );

		setLayout( new BorderLayout() );
		add( views );

	}

	public void actionPerformed(ActionEvent a) {

		if (a.getActionCommand().equals("refresh")) {

			countriesModel.fireTableDataChanged();
			continentsModel.fireTableDataChanged();
			cardsModel.fireTableDataChanged();
			playersModel.fireTableDataChanged();

			repaint();
		}
		else if (a.getActionCommand().equals("flash")) {

			risk.ui.FlashGUI.MainMenu.newMainMenuFrame( myrisk, JFrame.DISPOSE_ON_CLOSE );

		}

		else if (a.getActionCommand().equals("aiwait")) {

			Object[] message = new Object[2];
			message[0] = new JLabel("AI wait time (in milliseconds):");
			message[1] = new JSpinner( new SpinnerNumberModel( AIPlayer.getWait(),0,10000,100 ) );

			String[] options = {
			    "OK", 
			    "cancel"
			}; 

			int result = JOptionPane.showOptionDialog( 
			    this,                             // the parent that the dialog blocks 
			    message,                                    // the dialog message array 
			    "AI Options", // the title of the dialog window 
			    JOptionPane.OK_CANCEL_OPTION,                 // option type 
			    JOptionPane.PLAIN_MESSAGE,            // message type 
			    null,                                       // optional icon, use null to use the default icon 
			    options,                                    // options string array, will be made into buttons 
			    options[0]                                  // option that should be made into a default button 
			);

			if (result == JOptionPane.OK_OPTION ) {

				AIPlayer.setWait( ((Integer)((JSpinner)message[1]).getValue()).intValue() );
			}

		}
		else if (a.getActionCommand().equals("allcards")) {

			if (myrisk.getGame()!=null && myrisk.getGame().getCards()!=null) {

				Frame frame = RiskUtil.findParentFrame(this);

				CardsDialog cardsDialog = new CardsDialog( frame ,pp, false, myrisk , false );
				Dimension frameSize = frame.getSize();
				Dimension aboutSize = cardsDialog.getPreferredSize();
				int x = frame.getLocation().x + (frameSize.width - aboutSize.width) / 2;
				int y = frame.getLocation().y + (frameSize.height - aboutSize.height) / 2;
				if (x < 0) x = 0;
				if (y < 0) y = 0;
				cardsDialog.setLocation(x, y);

				cardsDialog.populate( myrisk.getGame().getCards() );

				cardsDialog.setVisible(true);
			}
		}
		else {

			throw new RuntimeException("TestTab: unknown command found: "+a.getActionCommand());

		}

	}

	public JToolBar getToolBar() {

		return toolbar;

	}
	public JMenu getMenu() {

		return null;

	}

}

⌨️ 快捷键说明

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