📄 riskgui.java
字号:
// Yura Mamyrin, Group D
package risk.ui.SimpleGUI;
import risk.engine.guishared.*;
import risk.engine.*;
import javax.swing.*;
import javax.swing.event.MouseInputListener;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Vector;
/**
* <p> Simple GUI for Risk </p>
* @author Yura Mamyrin
*/
public class RiskGUI extends JFrame implements MouseInputListener {
private final static String version = "1.1.1.1";
private final static String product = "Simple GUI for RISK";
private JTextPane Console;
private StyledDocument doc;
private JTextField Command;
private JButton Submit;
private PicturePanel pp;
private JLabel statusBar;
private JLabel gameStatus;
private JScrollPane Con;
private Risk risk;
private Vector history;
private int pointer;
private String temptext;
private JPanel guiMain;
private JPanel gp;
private JLabel Pix;
private JComboBox mapViewComboBox;
/** Creates new form JFrame */
/**
* Creates a new RiskGUI
* @param r The Risk object for this GUI
*/
public RiskGUI(Risk r) {
risk=r;
// this get all the commands from the game and does what needs to be done
RiskAdapter SimpleRiskAdapter = new RiskAdapter() {
/**
* Checks if redrawing or repainting is needed
* @param output
* @param redrawNeeded If frame needs to be redrawn
* @param repaintNeeded If frame needs to be repainted
*/
public void sendMessage(String output, boolean redrawNeeded, boolean repaintNeeded) {
Style style = doc.addStyle("StyleName", null);
Color c = risk.getCurrentPlayerColor();
if (c != null) {
StyleConstants.setForeground(style, c.darker() );
}
else {
StyleConstants.setForeground(style, Color.black);
}
//StyleConstants.setBold(style, true);
try {
//if (doc.getLength() > 12000) {
//doc.remove(0,2000);
//}
doc.insertString(doc.getLength(), output + System.getProperty("line.separator") , style);
Console.setCaretPosition(doc.getLength());
}
catch (Exception e) { }
if (redrawNeeded) {
pprepaintCountries();
}
if (repaintNeeded) {
repaint();
}
}
/**
* checks if the the frame needs input
* @param s determines what needs input
*/
public void needInput(int s) {
Submit.setEnabled(true);
Command.setEnabled(true);
Command.requestFocus();
statusBar.setText("Done... Ready");
}
/**
* Blocks Input
*/
public void noInput() {
statusBar.setText("Working...");
Submit.setEnabled(false);
Command.setEnabled(false);
}
/**
* Displays a message
*/
public void setGameStatus(String state) {
gameStatus.setText(state);
gameStatus.repaint();
}
/**
* Starts the game
* @param s If the game is a local game
*/
public void startGame(boolean s) {
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
try {
pp.load();
}
catch (IOException e) {
}
mapViewComboBox.setSelectedIndex(0);
guiMain.remove(Pix);
guiMain.add(gp, java.awt.BorderLayout.CENTER );
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
/**
* Closes the game
*/
public void closeGame() {
guiMain.remove(gp);
guiMain.add(Pix, java.awt.BorderLayout.CENTER );
}
};
gameStatus = new JLabel("");
risk.addRiskListener( SimpleRiskAdapter );
history = new Vector();
pointer=-1;
Console = new JTextPane();
doc = (StyledDocument)Console.getDocument();
Command = new JTextField();
Submit = new JButton();
Pix = new JLabel(new javax.swing.ImageIcon( this.getClass().getResource("about.png") ));
pp = new PicturePanel(risk);
gp = new GamePanel();
statusBar = new JLabel("Loading...");
Con = new JScrollPane(Console);
initGUI();
setResizable(false);
pack();
statusBar.setText("Ready");
}
/** This method is called from within the constructor to initialize the form. */
/**
* Initialises the GUI
*/
private void initGUI() {
// set title
setTitle(product);
setIconImage(Toolkit.getDefaultToolkit().getImage( AboutDialog.class.getResource("icon.gif") ));
getContentPane().setLayout(new java.awt.GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new java.awt.Insets(3, 3, 3, 3);
Dimension ppSize = new Dimension(PicturePanel.PP_X,PicturePanel.PP_Y);
pp.setPreferredSize(ppSize);
pp.setMinimumSize(ppSize);
pp.setMaximumSize(ppSize);
pp.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0,0,0),1));
pp.addMouseListener(this);
pp.addMouseMotionListener(this);
Console.setText("");
// Console.setBackground(Color.white); // not needed with swing
Console.setEditable(false);
Con.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
Con.setPreferredSize(new java.awt.Dimension(PicturePanel.PP_X,100));
Con.setMinimumSize(new java.awt.Dimension(PicturePanel.PP_X,100));
Command.setPreferredSize(new java.awt.Dimension(600,20));
Command.setMinimumSize(new java.awt.Dimension(600,20));
Command.setMaximumSize(new java.awt.Dimension(600,20));
Submit.setText("Submit");
guiMain = new JPanel();
Dimension guiMainSize = new Dimension(PicturePanel.PP_X, PicturePanel.PP_Y+30);
guiMain.setPreferredSize(guiMainSize);
guiMain.setMinimumSize(guiMainSize);
guiMain.setMaximumSize(guiMainSize);
guiMain.setLayout(new java.awt.BorderLayout());
guiMain.add(Pix, java.awt.BorderLayout.CENTER );
c.gridx = 0; // col
c.gridy = 0; // row
c.gridwidth = 2; // width
c.gridheight = 2; // height
getContentPane().add(guiMain, c); // Pix
c.fill = GridBagConstraints.BOTH;
c.gridx = 1; // col
c.gridy = 3; // row
c.gridwidth = 1; // width
c.gridheight = 1; // height
getContentPane().add(Submit, c);
c.gridx = 0; // col
c.gridy = 2; // row
c.gridwidth = 2; // width
c.gridheight = 1; // height
getContentPane().add(Con, c);
c.gridx = 0; // col
c.gridy = 3; // row
c.gridwidth = 1; // width
c.gridheight = 1; // height
getContentPane().add(Command, c);
c.gridx = 0; // col
c.gridy = 4; // row
c.gridwidth = 1; // width
c.gridheight = 1; // height
// add status bar
getContentPane().add(gameStatus, c);
c.gridx = 1; // col
c.gridy = 4; // row
c.gridwidth = 1; // width
c.gridheight = 1; // height
// add status bar
getContentPane().add(statusBar, c);
ActionListener readCommand = new ActionListener() {
public void actionPerformed(ActionEvent a) {
String input = Command.getText();
Command.setText("");
history.add(input);
pointer = history.size()-1;
go(input);
}
};
Submit.addActionListener( readCommand );
Command.addActionListener( readCommand );
class CommandKeyAdapter extends KeyAdapter {
RiskGUI adaptee;
CommandKeyAdapter(RiskGUI adaptee) {
this.adaptee = adaptee;
}
public void keyPressed(KeyEvent key) {
if (key.getKeyCode() == 38) {
if (pointer < 0) {
Toolkit.getDefaultToolkit().beep();
}
else {
if (pointer == history.size()-1) { temptext=Command.getText(); }
Command.setText( (String)history.elementAt(pointer) );
pointer--;
}
}
else if(key.getKeyCode() == 40) {
if (pointer > history.size()-2 ) {
Toolkit.getDefaultToolkit().beep();
}
else if (pointer == history.size()-2 ) {
Command.setText(temptext);
pointer++;
}
else {
pointer=pointer+2;
Command.setText( (String)history.elementAt(pointer) );
pointer--;
}
}
else {
pointer = history.size()-1;
}
}
}
Command.addKeyListener( new CommandKeyAdapter(this) );
// add menu bar
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu("File");
menuFile.setMnemonic('F');
// create About menu item
JMenu menuHelp = new JMenu("Help");
menuHelp.setMnemonic('H');
JMenuItem Commands = new JMenuItem("Commands");
Commands.setMnemonic('C');
Commands.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
Commands();
}
});
menuHelp.add(Commands);
JMenuItem helpMan = new JMenuItem("Manual");
helpMan.setMnemonic('M');
helpMan.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
go("manual");
}
});
menuHelp.add(helpMan);
JMenuItem helpAbout = new JMenuItem("About");
helpAbout.setMnemonic('A');
helpAbout.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
openAbout();
}
});
menuHelp.add(helpAbout);
// create Clear menu item
JMenu menuClear = new JMenu("Clear");
menuClear.setMnemonic('C');
JMenuItem ClearConsole = new JMenuItem("Clear Console");
ClearConsole.setMnemonic('C');
ClearConsole.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
Console.setText("");
}
});
menuClear.add(ClearConsole);
JMenuItem ClearHistory = new JMenuItem("Clear History");
ClearHistory.setMnemonic('H');
ClearHistory.addActionListener(
new ActionListener() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -