swingguipanel.java
来自「java 开源游戏源码 RISK 联机对战 战棋类」· Java 代码 · 共 2,611 行 · 第 1/5 页
JAVA
2,611 行
if (a.getActionCommand().equals("play debug")) {
JFileChooser fc = new JFileChooser();
RiskFileFilter filter = new RiskFileFilter(RiskFileFilter.RISK_LOG_FILES);
fc.setFileFilter(filter);
int returnVal = fc.showOpenDialog( RiskUtil.findParentFrame(this) );
if (returnVal == javax.swing.JFileChooser.APPROVE_OPTION) {
java.io.File file = fc.getSelectedFile();
// Write your code here what to do with selected file
String fileName = file.getAbsolutePath();
go("newgame");
go("play " + fileName );
} else {
// Write your code here what to do if user has canceled Open dialog
}
}
else if (a.getActionCommand().equals("save debug")) {
saveLog(debugText);
}
else if (a.getActionCommand().equals("clear debug")) {
debugText.setText("");
}
else if (a.getActionCommand().equals("save error")) {
saveLog(errText);
}
else if (a.getActionCommand().equals("send error")) {
String email = JOptionPane.showInputDialog(this,"tell me your e-mail please");
if (email == null) { email ="none"; }
try {
RiskUtil.sendText(email , debugText.getText() + errText.getText(), "SwingGUI Bug" );
JOptionPane.showMessageDialog(this, "SENT!");
}
catch(Exception ex) {
showError("unable to send: "+ex.getMessage() );
}
}
else {
System.out.print("command \""+a.getActionCommand()+"\" is not implemented yet\n");
}
}
}
class StatisticsTab extends JPanel implements SwingGUITab,ActionListener {
private JToolBar toolbarStat;
private JMenu sStatistics;
private StatsPanel graph;
private AbstractButton[] statbuttons;
public JToolBar getToolBar() {
return toolbarStat;
}
public JMenu getMenu() {
return sStatistics;
}
public void actionPerformed(ActionEvent a) {
graph.repaintStats( Integer.parseInt( a.getActionCommand() ) );
graph.repaint();
}
public StatisticsTab() {
setName( resbundle.getString("swing.tab.statistics") );
//##################### graph ####################
toolbarStat = new JToolBar();
toolbarStat.setRollover(true);
toolbarStat.setFloatable(false);
graph = new StatsPanel(myrisk);
graph.setBorder( BorderFactory.createLoweredBevelBorder() );
statbuttons = new AbstractButton[24];
statbuttons[0]=new JButton(resbundle.getString("swing.toolbar.countries"));
statbuttons[1]=new JButton(resbundle.getString("swing.toolbar.armies"));
statbuttons[2]=new JButton(resbundle.getString("swing.toolbar.kills"));
statbuttons[3]=new JButton(resbundle.getString("swing.toolbar.casualties"));
statbuttons[4]=new JButton(resbundle.getString("swing.toolbar.reinforcements"));
statbuttons[5]=new JButton(resbundle.getString("swing.toolbar.continents"));
statbuttons[6]=new JButton(resbundle.getString("swing.toolbar.empire"));
statbuttons[7]=new JButton(resbundle.getString("swing.toolbar.attacks"));
statbuttons[8]=new JButton(resbundle.getString("swing.toolbar.retreats"));
statbuttons[9]=new JButton(resbundle.getString("swing.toolbar.victories"));
statbuttons[10]=new JButton(resbundle.getString("swing.toolbar.defeats"));
statbuttons[11]=new JButton(resbundle.getString("swing.toolbar.attacked"));
for (int a=0; a<12; a++) {
statbuttons[a].setActionCommand( (a+1)+"" );
statbuttons[a].addActionListener(this);
toolbarStat.add(statbuttons[a]);
statbuttons[a].setEnabled(false);
}
// create Statistics menu item
sStatistics = new JMenu( resbundle.getString("swing.tab.statistics") );
sStatistics.setMnemonic('S');
statbuttons[12]=new JMenuItem(resbundle.getString("swing.toolbar.countries"));
statbuttons[13]=new JMenuItem(resbundle.getString("swing.toolbar.armies"));
statbuttons[14]=new JMenuItem(resbundle.getString("swing.toolbar.kills"));
statbuttons[15]=new JMenuItem(resbundle.getString("swing.toolbar.casualties"));
statbuttons[16]=new JMenuItem(resbundle.getString("swing.toolbar.reinforcements"));
statbuttons[17]=new JMenuItem(resbundle.getString("swing.toolbar.continents"));
statbuttons[18]=new JMenuItem(resbundle.getString("swing.toolbar.empire"));
statbuttons[19]=new JMenuItem(resbundle.getString("swing.toolbar.attacks"));
statbuttons[20]=new JMenuItem(resbundle.getString("swing.toolbar.retreats"));
statbuttons[21]=new JMenuItem(resbundle.getString("swing.toolbar.victories"));
statbuttons[22]=new JMenuItem(resbundle.getString("swing.toolbar.defeats"));
statbuttons[23]=new JMenuItem(resbundle.getString("swing.toolbar.attacked"));
for (int a=12; a<statbuttons.length; a++) {
statbuttons[a].setActionCommand( (a-11)+"" );
statbuttons[a].addActionListener(this);
sStatistics.add(statbuttons[a]);
statbuttons[a].setEnabled(false);
}
setLayout( new BorderLayout() );
add(graph);
}
public void startGame() {
for (int a=0; a<statbuttons.length; a++) {
statbuttons[a].setEnabled(true);
}
}
public void closeGame() {
for (int a=0; a<statbuttons.length; a++) {
statbuttons[a].setEnabled(false);
}
}
}
public void saveLog(JTextArea textArea) {
JFileChooser fc = new JFileChooser();
RiskFileFilter filter = new RiskFileFilter(RiskFileFilter.RISK_LOG_FILES);
fc.setFileFilter(filter);
int returnVal = fc.showSaveDialog( RiskUtil.findParentFrame(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(textArea.getText());
printer.close();
}
catch(Exception error) {
showError( error.getMessage() );
}
} else {
// Write your code here what to do if user has canceled Save dialog
}
}
/**
* Submits input to parser if neccessary
* @param input The string that is checked
*/
public void go(String input) {
pp.setHighLight(PicturePanel.NO_COUNTRY);
// Testing.append("Submitted: \""+input+"\"\n");
if (gameState!=2 || !myrisk.getGame().getSetup() ) { blockInput(); }
myrisk.parser(input);
// Console.setCaretPosition(Console.getDocument().getLength());
}
/**
* Blocks the game panel
*/
public void blockInput() {
gameState= -1;
inGameCards.show(inGameInput, "nothing");
gameTab.blockInput();
consoleTab.blockInput();
}
/**
* This reads in a file for the commands
*/
public void Commands() {
String commands="";
try {
BufferedReader bufferin=new BufferedReader(new InputStreamReader( RiskUtil.openStream("commands.txt") ));
String input = bufferin.readLine();
while(input != null) {
if (commands.equals("")) { commands = input; }
else { commands = commands + "\n" + input; }
input = bufferin.readLine();
}
bufferin.close();
// Testing.append("Commands Box opened\n");
JOptionPane.showMessageDialog( RiskUtil.findParentFrame(this) , commands, resbundle.getString("swing.message.commands"), JOptionPane.PLAIN_MESSAGE);
}
catch (Exception e) {
showError("error with commands.txt file: "+e.getMessage() );
}
}
/**
* This opens the about dialog box
*/
public void openAbout() {
RiskUtil.openAbout( RiskUtil.findParentFrame(this) ,product, version);
}
/**
* This opens the about dialog box
*/
public void openCards() {
Frame frame = RiskUtil.findParentFrame(this);
CardsDialog cardsDialog = new CardsDialog( frame ,pp, true, myrisk , (gameState==1) );
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.getCurrentCards() );
cardsDialog.setVisible(true);
}
public void showQuestion(int n) {
moveNumber.setMaximum(n);
moveNumber.setMinimum(1);
moveNumber.setValue(1);
String[] options = {
resbundle.getString("swing.move.move"),
resbundle.getString("swing.move.moveall"),
resbundle.getString("swing.move.cancel")
};
int a = JOptionPane.showOptionDialog(
RiskUtil.findParentFrame(this), // the parent that the dialog blocks
moveNumber, // the dialog message array
resbundle.getString("swing.move.title"), // the title of the dialog window
JOptionPane.DEFAULT_OPTION, // option type
JOptionPane.QUESTION_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 (a==0) {
go("movearmies " + pp.getC1() + " " + pp.getC2() + " "+ moveNumber.getValue() );
}
if (a==1) {
go("movearmies " + pp.getC1() + " " + pp.getC2() + " "+ n );
}
}
public void showError(String error) {
JOptionPane.showMessageDialog(this, resbundle.getString("swing.message.error") + " " + error, resbundle.getString("swing.title.error"), JOptionPane.ERROR_MESSAGE);
}
public void showMission(String mission) {
JOptionPane.showMessageDialog(this, resbundle.getString("swing.message.mission") + " " + mission, resbundle.getString("swing.title.mission"), JOptionPane.INFORMATION_MESSAGE);
}
public void pprepaintCountries() {
String tmp = (String)mapViewComboBox.getSelectedItem();
int newview = -1;
if (tmp.equals(resbundle.getString("game.tabs.continents"))) { newview=PicturePanel.VIEW_CONTINENTS; }
else if (tmp.equals(resbundle.getString("game.tabs.ownership"))) { newview=PicturePanel.VIEW_OWNERSHIP; }
else if (tmp.equals(resbundle.getString("game.tabs.borderthreat"))) { newview=PicturePanel.VIEW_BORDER_THREAT; }
else if (tmp.equals(resbundle.getString("game.tabs.cardownership"))) { newview=PicturePanel.VIEW_CARD_OWNERSHIP; }
else if (tmp.equals(resbundle.getString("game.tabs.troopstrength"))) { newview=PicturePanel.VIEW_TROOP_STRENGTH; }
else if (tmp.equals(resbundle.getString("game.tabs.connectedempire"))) { newview=PicturePanel.VIEW_CONNECTED_EMPIRE; }
pp.repaintCountries( newview );
}
private void clearAttackPanel() {
pp.setC1(PicturePanel.NO_COUNTRY);
pp.setC2(PicturePanel.NO_COUNTRY);
attacker.setText(resbundle.getString("game.note.selectattacker"));
c1Id = -1;
}
//############################################################################################################
// this get all the commands from the game and does what needs to be done
class SwingRiskAdapter extends 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) {
// Testing.append("Returned: \""+output+"\"\n");
consoleTab.addOutput(output);
if (redrawNeeded) {
pprepaintCountries();
}
if (repaintNeeded) {
repaint();
}
}
public void sendDebug(String a) {
debugTab.sendDebug(a);
}
public void showMessageDialog(String a) {
showError(a);
}
/**
* Blocks the game panel
*/
public void noInput() {
blockInput();
}
/**
* checks if the the frame needs input
* @param s determines what needs input
*/
public void needInput(int s) {
gameState=s;
if ((gameState != -1) && (gameState != 0) && (gameState != 10) && (gameState != 4)) {
gameTab.getInput();
}
if (gameState == RiskGame.STATE_NEW_GAME) {
inGameCards.show(inGameInput, "nothing");
}
else if (gameState == RiskGame.STATE_TRADE_CARDS) {
// after wiping out someone if you go into trade mode
pp.setC1(PicturePanel.NO_COUNTRY);
pp.setC2(PicturePanel.NO_COUNTRY);
inGameCards.show(inGameInput, "tradeCards");
}
else if (gameState == RiskGame.STATE_PLACE_ARMIES) {
inGameCards.show(inGameInput, "placeArmies");
}
else if (gameState == RiskGame.STATE_ATTACKING) {
clearAttackPanel();
inGameCards.show(inGameInput, "attack");
}
else if (gameState == RiskGame.STATE_ROLLING) {
inGameCards.show(inGameInput, "roll");
}
else if (gameState == RiskGame.STATE_BATTLE_WON) {
inGameCards.show(inGameInput, "move");
}
else if (gameState == RiskGame.STATE_FORTIFYING) {
inGameCards.show(inGameInput, "tacMove");
}
else if (gameState == RiskGame.STATE_END_TURN) {
inGameCards.show(inGameInput, "endgo");
}
else if (gameState == RiskGame.STATE_GAME_OVER) {
inGameCards.show(inGameInput, "winner");
}
else if (gameState == RiskGame.STATE_SELECT_CAPITAL) {
inGameCards.show(inGameInput, "capital");
}
else if (gameState == RiskGame.STATE_DEFEND_YOURSELF) {
inGameCards.show(inGameInput, "defend");
}
consoleTab.getInput();
repaint();
}
/**
* Displays a message
* @param state The message that is needed to be displayed
*/
public void setGameStatus(String state) {
gameTab.setGameStatus(state);
}
public void showMapPic(Image p) {
if (p!=null) {
mapPic.setIcon( new ImageIcon(p) ); // SCALE_DEFAULT
}
else {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?