📄 swingguipanel.java
字号:
// Yura Mamyrin, Group D
package risk.ui.SwingGUI;
import risk.engine.*;
import risk.engine.ai.AIPlayer;
import risk.engine.core.RiskGame;
import risk.engine.core.Continent;
import risk.engine.guishared.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.MouseInputListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Vector;
import java.util.ResourceBundle;
import java.net.URL;
import risk.tools.mapeditor.MapEditor;
/**
* <p> Swing GUI Main Frame </p>
* @author Yura Mamyrin
*/
public class SwingGUIPanel extends JPanel implements ActionListener{
public final static String version = "1.0.6.4";
public final static String product;
static {
product = "Swing GUI for " + RiskUtil.getGameName();
}
// TRUE GUI GLOBAL THINGS:
private ResourceBundle resbundle = risk.engine.translation.TranslationBundle.getBundle();
private Risk myrisk;
private int gameState;
private boolean localGame;
private JTabbedPane tabbedpane;
private JToolBar currentToolbar;
private JMenuBar gMenuBar;
private GameTab gameTab;
private ConsoleTab consoleTab;
private StatisticsTab statisticsTab;
private DebugTab debugTab;
private MapEditor editorTab;
// should not really be here
private PicturePanel pp;
private int c1Id;
private JLabel mapPic;
private JTextField cardsFile;
private JLabel attacker;
private JTextField country1;
private JTextField country2;
private JPanel inGameInput;
private CardLayout inGameCards;
// these are needed here as they need to be changed to display different amount of buttons
private JPanel defend;
private JPanel roll;
private JSlider slider;
private JSlider moveNumber;
private JLabel armies;
private JButton autoplace;
private JLabel resultsLabel;
private SetupPanel guiSetup;
private JPanel gameOptions;
private JButton roll1;
private JButton roll2;
private JButton roll3;
private JComboBox mapViewComboBox;
private JButton showMission;
private JButton showCards;
private JButton Undo;
private JRadioButton domination;
private JRadioButton capital;
private JRadioButton mission;
private JCheckBox AutoPlaceAll;
private JCheckBox recycle;
private JRadioButton fixed;
private JRadioButton increasing;
private JButton lobby;
/**
* Creates a new SwingGUI
* @param r The Risk object for this GUI
*/
public SwingGUIPanel(Risk r) {
if (RiskUtil.checkForNoSandbox()) { Toolkit.getDefaultToolkit().getSystemEventQueue().push( new TCPopupEventQueue() ); }
myrisk= r;
c1Id = -1;
gameState=-1; // (-1 means no game)
pp = new PicturePanel(myrisk);
setLayout(new java.awt.BorderLayout());
// set the border of the window
//setDefaultLookAndFeelDecorated(true);
//setUndecorated(true);
//getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
// add menu bar
gMenuBar = new JMenuBar();
tabbedpane = new JTabbedPane();
gameTab = new GameTab();
consoleTab = new ConsoleTab();
statisticsTab = new StatisticsTab();
debugTab = new DebugTab();
editorTab = new MapEditor(myrisk);
addTab(gameTab);
addTab( new FX3DPanel(pp) );
addTab(consoleTab);
addTab(statisticsTab);
addTab(debugTab);
addTab( new TestPanel(myrisk,pp) );
addTab(editorTab);
addTab( new risk.tools.translation.MessageTool() );
addTab(new BugsPanel());
final JPanel oddpanelbug = new JPanel();
oddpanelbug.setLayout(new java.awt.BorderLayout());
oddpanelbug.add(tabbedpane, java.awt.BorderLayout.CENTER );
add(oddpanelbug,java.awt.BorderLayout.CENTER);
ChangeListener changeMenu = new ChangeListener() {
public void stateChanged(ChangeEvent e) {
SwingGUITab sgt = (SwingGUITab)tabbedpane.getSelectedComponent();
if (currentToolbar!=null) { oddpanelbug.remove(currentToolbar); }
currentToolbar = sgt.getToolBar();
currentToolbar.setOrientation( javax.swing.JToolBar.HORIZONTAL );
oddpanelbug.add( currentToolbar, java.awt.BorderLayout.NORTH );
repaint();
}
};
tabbedpane.addChangeListener( changeMenu );
//oddpanelbug.add(toolbarGUI, java.awt.BorderLayout.NORTH );
changeMenu.stateChanged(null);
// create Help menu item
JMenu gHelp = new JMenu(resbundle.getString("swing.menu.help"));
gHelp.setMnemonic('H');
JMenuItem gmManual = new JMenuItem(resbundle.getString("swing.menu.manual"));
gmManual.setMnemonic('M');
gmManual.setActionCommand("manual");
gmManual.addActionListener( this );
gHelp.add(gmManual);
JMenuItem cmCommands = new JMenuItem(resbundle.getString("swing.menu.console.commands"));
cmCommands.setMnemonic('C');
cmCommands.setActionCommand("commands");
cmCommands.addActionListener( this );
gHelp.add(cmCommands);
JMenuItem gmAbout = new JMenuItem(resbundle.getString("swing.menu.about"));
gmAbout.setMnemonic('A');
gmAbout.setActionCommand("about");
gmAbout.addActionListener( this );
gHelp.add(gmAbout);
gMenuBar.add(gHelp);
add(gMenuBar, java.awt.BorderLayout.NORTH );
// sets menu bar
//setJMenuBar(gMenuBar);
//setBounds(new java.awt.Rectangle(0,0,905,629));
// now gui is setup u can listen
myrisk.addRiskListener( new SwingRiskAdapter() );
}
public void setupLobbyButton() {
if (RiskUtil.getAddLobby(myrisk)) {
lobby.setVisible(true);
}
revalidate();
repaint();
}
public void actionPerformed(ActionEvent a) {
if (a.getActionCommand().equals("manual")) {
try {
RiskUtil.openDocs( resbundle.getString("helpfiles.swing") );
}
catch(Exception e) {
showError("Unable to open manual: "+e.getMessage() );
}
}
else if (a.getActionCommand().equals("about")) {
openAbout();
}
else if (a.getActionCommand().equals("quit")) {
System.exit(0);
}
else if (a.getActionCommand().equals("commands")) {
Commands();
}
else {
System.out.print("command \""+a.getActionCommand()+"\" is not implemented yet\n");
}
}
public void addTab(SwingGUITab a) {
//tabbedpane.addTab(a.getName(),(Component)a);
tabbedpane.add((Component)a);
JMenu menu = a.getMenu();
if (menu!=null) {
gMenuBar.add(menu);
}
}
class ConsoleTab extends JPanel implements SwingGUITab, ActionListener {
private String temptext;
private Vector history;
private int pointer;
JMenu cConsole;
JToolBar toolbarCon;
private JTextArea Console;
private JTextField Command;
private JButton Submit;
private JLabel statusBar;
private JScrollPane Con;
public ConsoleTab() {
setName( resbundle.getString("swing.tab.console") );
// ################### CONSOLE #######################
history = new Vector();
pointer=-1;
statusBar = new JLabel(resbundle.getString("swing.status.loading"));
Console = new JTextArea();
Con = new JScrollPane(Console);
Dimension conSize = new Dimension(PicturePanel.PP_X,PicturePanel.PP_Y+60);
Con.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
Con.setPreferredSize(conSize);
Con.setMinimumSize(conSize);
// Console.setBackground(Color.white); // not needed with swing
Console.setEditable(false);
Command = new JTextField("");
// Command.setColumns(75); // dont use because it goes odd in linux
Dimension CommandSize = new Dimension(PicturePanel.PP_X-50 , 20);
//c.ipadx = 600; // width
//c.ipadx = 0; // width
Command.setPreferredSize(CommandSize);
Command.setMinimumSize(CommandSize);
Command.setMaximumSize(CommandSize);
Submit = new JButton(resbundle.getString("swing.button.submit"));
Submit.setActionCommand("read command");
Submit.addActionListener( this );
Command.setActionCommand("read command");
Command.addActionListener( this );
// make tool bar
toolbarCon = new JToolBar();
toolbarCon.setRollover(true);
toolbarCon.setFloatable(false);
JButton cRunScript = new JButton(resbundle.getString("swing.menu.console.runscript"));
JButton cSaveConsole = new JButton(resbundle.getString("swing.menu.console.save"));
JButton cClearConsole = new JButton(resbundle.getString("swing.menu.console.clear"));
JButton cClearHistory = new JButton(resbundle.getString("swing.menu.console.histclear"));
JButton cCommands = new JButton(resbundle.getString("swing.menu.console.commands"));
JButton cManual = new JButton(resbundle.getString("swing.menu.manual"));
JButton cAbout = new JButton(resbundle.getString("swing.menu.about"));
JButton cQuit = new JButton(resbundle.getString("swing.menu.quit"));
cRunScript.setActionCommand("run script");
cRunScript.addActionListener( this );
cSaveConsole.setActionCommand("save console");
cSaveConsole.addActionListener( this );
cClearConsole.setActionCommand("clear console");
cClearConsole.addActionListener( this );
cClearHistory.setActionCommand("clear history");
cClearHistory.addActionListener( this );
cCommands.setActionCommand("commands");
cCommands.addActionListener( this );
cManual.setActionCommand("manual");
cManual.addActionListener( this );
cAbout.setActionCommand("about");
cAbout.addActionListener( this );
cQuit.setActionCommand("quit");
cQuit.addActionListener( this );
toolbarCon.add(cRunScript);
toolbarCon.add(cSaveConsole);
toolbarCon.add(cClearConsole);
toolbarCon.add(cClearHistory);
toolbarCon.addSeparator();
toolbarCon.add(cCommands);
toolbarCon.add(cManual);
toolbarCon.add(cAbout);
if (RiskUtil.checkForNoSandbox()) { toolbarCon.add(cQuit); }
// create Console menu item
cConsole = new JMenu(resbundle.getString("swing.menu.console"));
cConsole.setMnemonic('C');
JMenuItem cmRunScript = new JMenuItem(resbundle.getString("swing.menu.console.runscript"));
cmRunScript.setMnemonic('R');
cmRunScript.setActionCommand("run script");
cmRunScript.addActionListener( this );
cConsole.add(cmRunScript);
JMenuItem cmSaveConsole = new JMenuItem(resbundle.getString("swing.menu.console.save"));
cmSaveConsole.setMnemonic('S');
cmSaveConsole.setActionCommand("save console");
cmSaveConsole.addActionListener( this );
cConsole.add(cmSaveConsole);
JMenuItem cmClearConsole = new JMenuItem(resbundle.getString("swing.menu.console.clear"));
cmClearConsole.setMnemonic('C');
cmClearConsole.setActionCommand("clear console");
cmClearConsole.addActionListener( this );
cConsole.add(cmClearConsole);
JMenuItem cmClearHistory = new JMenuItem(resbundle.getString("swing.menu.console.histclear"));
cmClearHistory.setMnemonic('H');
cmClearHistory.setActionCommand("clear history");
cmClearHistory.addActionListener( this );
cConsole.add(cmClearHistory);
GridBagConstraints c = new GridBagConstraints();
c.insets = new java.awt.Insets(3, 3, 3, 3);
c.fill = GridBagConstraints.BOTH;
setLayout(new java.awt.GridBagLayout());
c.gridx = 0; // col
c.gridy = 0; // row
c.gridwidth = 2; // width
c.gridheight = 1; // height
add(Con, c);
c.gridx = 0; // col
c.gridy = 1; // row
c.gridwidth = 1; // width
c.gridheight = 1; // height
add(Command, c);
c.gridx = 1; // col
c.gridy = 1; // row
c.gridwidth = 1; // width
c.gridheight = 1; // height
add(Submit, c);
c.gridx = 0; // col
c.gridy = 2; // row
c.gridwidth = 2; // width
c.gridheight = 1; // height
add(statusBar, c);
Command.addKeyListener( new KeyAdapter() {
public void keyPressed(KeyEvent key) {
if (key.getKeyCode() == 38) {
// Testing.append("up key (history)\n");
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) {
// Testing.append("down key (history)\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -