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

📄 riskgui.java

📁 java 开源游戏源码 RISK 联机对战 战棋类
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
					public void actionPerformed(ActionEvent e) {
						history.clear();
						pointer = -1;
					}
				});
		menuClear.add(ClearHistory);

		// create Open menu item

		JMenuItem openFile = new JMenuItem("Run Script");
		openFile.setMnemonic('R');
		openFile.addActionListener(
				new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {

						final JFileChooser fc = new JFileChooser();
						RiskFileFilter filter = new RiskFileFilter(RiskFileFilter.RISK_SCRIPT_FILES);
						fc.setFileFilter(filter);

						int returnVal = fc.showDialog(RiskGUI.this, "Run");
						if (returnVal == javax.swing.JFileChooser.APPROVE_OPTION) {
							java.io.File file = fc.getSelectedFile();
							// Write your code here what to do with selected file

							try {

								FileReader filein = new FileReader(file);
								BufferedReader bufferin = new BufferedReader(filein);

								String input = bufferin.readLine();
								while(input != null) {

									go(input);
									input = bufferin.readLine();

								}
								bufferin.close();

							}
							catch(Exception error) {

							}

						} else {
							// Write your code here what to do if user has canceled Open dialog
						}
					}
				});
		menuFile.add(openFile);

		// create Save menu item
		JMenuItem saveFile = new JMenuItem("Save Console");
		saveFile.setMnemonic('S');
		saveFile.addActionListener(
				new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {

						final JFileChooser fc = new JFileChooser();
						RiskFileFilter filter = new RiskFileFilter(RiskFileFilter.RISK_LOG_FILES);
						fc.setFileFilter(filter);

						int returnVal = fc.showSaveDialog(RiskGUI.this);
						if (returnVal == JFileChooser.APPROVE_OPTION) {
							java.io.File file = fc.getSelectedFile();
							// Write your code here what to do with selected file

							String fileName = file.getAbsolutePath();

							if (!(fileName.endsWith( "." + RiskFileFilter.RISK_LOG_FILES ))) {
								fileName = fileName + "." + RiskFileFilter.RISK_LOG_FILES;
							}

							try {

								FileWriter fileout = new FileWriter(fileName);
								BufferedWriter buffer = new BufferedWriter(fileout);
								PrintWriter printer = new PrintWriter(buffer);

								printer.write( doc.getText(0, doc.getLength() ) );

								printer.close();

							}

							catch(Exception error) {

							}

						} else {
							// Write your code here what to do if user has canceled Save dialog
						}
					}
				});
		menuFile.add(saveFile);

		// create Exit menu item
		JMenuItem fileExit = new JMenuItem("Exit");
		fileExit.setMnemonic('E');
		fileExit.addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						System.exit(0);
					}
				});
		menuFile.add(fileExit);

		menuBar.add(menuFile);
		menuBar.add(menuClear);
		menuBar.add(menuHelp);

		// sets menu bar
		setJMenuBar(menuBar);
		setBounds(new java.awt.Rectangle(0,0,905,629));
		addWindowListener(
				new java.awt.event.WindowAdapter() {
					public void windowClosing(java.awt.event.WindowEvent evt) {
						exitForm();
					}
				});

		// pack();

	}

	/** Exit the Application */

	/**
	 * Closes the GUI
	 */
	private void exitForm() {

		System.exit(0);
	}

	/**
	 * Submits input to parser if neccessary
	 * @param input The string that is checked
	 */
	public void go(String input) {

		if (input.equals("exit") ) {
			System.exit(0);
		}
		else if (input.equals("help") ) {
			Commands();
		}
		else if (input.equals("about") ) {
			openAbout();
		}
		else if (input.equals("clear") ) {
			Console.setText("");
		}
		else if (input.equals("manual") ) {
			try {
				RiskUtil.openDocs("help/index_commands.htm");
			}
			catch(Exception e) {
				JOptionPane.showMessageDialog(this,"Unable to open manual: "+e.getMessage(),"Error", JOptionPane.ERROR_MESSAGE);
			}

		}
		else {
			//statusBar.setText("Working...");
			//Submit.setEnabled(false);
			//Command.setEnabled(false);
			risk.parser(input);
		}

	}

	/**
	 * This reads in a file for the commands
	 */
	public void Commands() {

		String commands="";

		try {
			FileReader filein = new FileReader("commands.txt");
			BufferedReader bufferin = new BufferedReader(filein);
			String input = bufferin.readLine();
			while(input != null) {
				if (commands.equals("")) { commands = input; }
				else { commands = commands + "\n" + input; }
				input = bufferin.readLine();
			}
			bufferin.close();

			JOptionPane.showMessageDialog(this, commands, "Commands:", JOptionPane.PLAIN_MESSAGE);
		}
		catch (FileNotFoundException e) {
			//Testing.append("Unable to find file commands.txt\n");
		}
		catch (IOException e) {
			//Testing.append("Unable to read file commands.txt\n");
		}

	}

	/**
	 * This opens the about dialog box
	 */
	public void openAbout() {

		RiskUtil.openAbout(RiskGUI.this,product, version);

	}

	class playersPanel extends JPanel {

		public void paintComponent(Graphics g) {

			Color[] colors = risk.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);

		}

	}

	class GamePanel extends JPanel {

		public GamePanel() {

			JPanel OptionsPanel = new JPanel();

			OptionsPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 5, 0));

			JLabel mapLookLabel = new JLabel("Map Look:");

			mapViewComboBox = new JComboBox();
			JButton closegame = new JButton("closegame");
			JButton about = new JButton("About");

			//JButton leave = new JButton("leave");

			Dimension mapViewSize = new Dimension(150 , 20);

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

			mapViewComboBox.addItem("Continents");
			mapViewComboBox.addItem("Ownership");
			mapViewComboBox.addItem("Border Threat");
			mapViewComboBox.addItem("Risk Card Ownership");
			mapViewComboBox.addItem("Troop Strength");
			mapViewComboBox.addItem("Connected Empire");

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

							pprepaintCountries();
							pp.repaint();

						}
					}
			);

			JLabel playersLabel = new JLabel("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);

			closegame.addActionListener(
					new ActionListener() {
						public void actionPerformed(ActionEvent a) {
							go("closegame");
						}
					}
			);

			//leave.addActionListener(
			//		new ActionListener() {
			//			public void actionPerformed(ActionEvent a) {
			//				go("leave");
			//			}
			//		}
			//);

			about.addActionListener(
					new ActionListener() {
						public void actionPerformed(ActionEvent a) {
							openAbout();
						}
					}
			);

			Dimension size = new Dimension(PicturePanel.PP_X , 30);

			OptionsPanel.setPreferredSize(size);
			OptionsPanel.setMinimumSize(size);
			OptionsPanel.setMaximumSize(size);

			OptionsPanel.add(mapLookLabel);
			OptionsPanel.add(mapViewComboBox);
			OptionsPanel.add(playersLabel);
			OptionsPanel.add(players);
			OptionsPanel.add(closegame);
			OptionsPanel.add(about);
			//OptionsPanel.add(leave);

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

			this.add(OptionsPanel, java.awt.BorderLayout.NORTH );
			this.add(pp, java.awt.BorderLayout.CENTER);

		}
	}

	public void pprepaintCountries() {

		int tmp = mapViewComboBox.getSelectedIndex();
		int newview = -1;

        	switch (tmp) {
		    case 0: newview=PicturePanel.VIEW_CONTINENTS; break;
		    case 1: newview=PicturePanel.VIEW_OWNERSHIP; break;
		    case 2: newview=PicturePanel.VIEW_BORDER_THREAT; break;
		    case 3: newview=PicturePanel.VIEW_CARD_OWNERSHIP; break;
		    case 4: newview=PicturePanel.VIEW_TROOP_STRENGTH; break;
		    case 5: newview=PicturePanel.VIEW_CONNECTED_EMPIRE; break;
		}

		pp.repaintCountries( newview );

	}

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

	public void mouseClicked(MouseEvent e) {
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {

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

	}

	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());

			if (pixColor != 255 ) {

				String name = pixColor+""; // risk.getCountryInt( pixColor );
				Command.setText( Command.getText() + " " + name );

			}
		}

	}

	public void mouseDragged(MouseEvent e) {
	}
	public void mouseMoved(MouseEvent e) {

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

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

	}

	/**
	 * This runs the program
	 * @param argv
	 */
	public static void main(String[] argv) {

		RiskGUI gui = new RiskGUI( new Risk(null) );

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = gui.getSize();
		frameSize.height = ((frameSize.height > screenSize.height) ? screenSize.height : frameSize.height);
		frameSize.width = ((frameSize.width > screenSize.width) ? screenSize.width : frameSize.width);
		gui.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
		gui.setVisible(true);

	}

}

⌨️ 快捷键说明

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