📄 mainmenu.java
字号:
myrisk.parser("loadgame " + name );
}
break;
}
case MainMenu.BUTTON_HELP: {
try {
RiskUtil.openDocs( risk.engine.translation.TranslationBundle.getBundle().getString( "helpfiles.flash" ) );
}
catch(Exception e) {
JOptionPane.showMessageDialog( RiskUtil.findParentFrame(this) ,"Unable to open manual: "+e.getMessage(),"Error", JOptionPane.ERROR_MESSAGE);
}
break;
}
case MainMenu.BUTTON_JOIN: {
Frame frame = RiskUtil.findParentFrame(this);
joinDialog = new JoinDialog( frame , true, myrisk);
Dimension frameSize = frame.getSize();
Dimension aboutSize = joinDialog.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;
joinDialog.setLocation(x, y+10);
joinDialog.setVisible(true);
break;
}
case MainMenu.BUTTON_LOBBY: {
if (showLobby) {
RiskUtil.runLobby(myrisk);
}
break;
}
case MainMenu.BUTTON_ABOUT: {
Frame frame = RiskUtil.findParentFrame(this);
RiskUtil.openAbout(frame,product, version);
break;
}
case MainMenu.BUTTON_EXIT: {
exit();
break;
}
case MainMenu.BUTTON_DONATE: {
try {
RiskUtil.donate();
}
catch(Exception e) {
JOptionPane.showMessageDialog( RiskUtil.findParentFrame(this) ,"Unable to open manual: "+e.getMessage(),"Error", JOptionPane.ERROR_MESSAGE);
}
break;
}
}//switch end
currentButton=0;
//do we need this?
// highlightButton=currentButton;
repaint();
}
}//private void activateButton(int thebutton)
/**
* Checks if highlighting is needed
* @param e A mouse event
*/
public void mouseMoved(MouseEvent e) {
int oldhighlightButton = highlightButton;
int newhighlightButton = insideButton(e.getX(),e.getY());
if (oldhighlightButton != newhighlightButton) {
highlightButton = newhighlightButton;
repaint();
}
if (newhighlightButton==BUTTON_DONATE) {
if (getCursor()==defaultCursor) {
setCursor(hand);
}
}
else if (getCursor()==hand) {
setCursor(defaultCursor);
}
}
/**
* Works out what to do when the mouse is dragged
* @param e A mouse event
*/
public void mouseDragged(MouseEvent e) {
currentButton = insideButton(e.getX(),e.getY());
if (pressedButton == currentButton ) {
if (button!=pressedButton) {
button=pressedButton;
repaint();
}
}
else {
if (button !=0) {
button = 0;
repaint();
}
}
}
/**
* Works out what button has been pressed
* @param x x co-ordinate
* @param y y co-ordinate
* @return int The type of button presssed
*/
public int insideButton(int x, int y) {
int W=116;
int H=31;
int B=0;
int yrel = Math.abs(455 - y);
int xrel = (int) (Math.sqrt(2255 - yrel * yrel) * 95 / 95);
if (x >= 65 && x < (65 + W) && y >= 228 && y < (228 + H)) {
B=BUTTON_NEW;
}
else if (x >= 220 && x < (220 + W) && y >= 228 && y < (228 + H)) {
B=BUTTON_SERVER;
}
else if (x >= 65 && x < (65 + W) && y >= 289 && y < (289 + H)) {
B=BUTTON_LOADGAME;
}
else if (x >= 220 && x < (220 + W) && y >= 289 && y < (289 + H)) {
B=BUTTON_HELP;
}
else if (x >= 65 && x < (65 + W) && y >= 350 && y < (350 + H)) {
B=BUTTON_JOIN;
}
else if (x >= 220 && x < (220 + W) && y >= 350 && y < (350 + H)) {
B=BUTTON_ABOUT;
}
else if (x >= 200 - xrel && x < 200 + xrel) {
B=BUTTON_LOBBY;
}
else if (x >= 12 && x < (12 + 58) && y >= 502 && y < (502 + 36)) {
B=BUTTON_DONATE;
}
return B;
}
/**
* key control
*/
/**
* the user has released a key
*/
public void keyReleased( KeyEvent event )
{
switch (event.getKeyCode()) {
//tab to the next button
case KeyEvent.VK_TAB:
if (event.isShiftDown()) {
//Shift + Tab -> backwards
highlightButton--;
} else {
//tab only -> forward
highlightButton++;
}
if (highlightButton > 7) {
highlightButton = 1;
} else if (highlightButton < 1) {
highlightButton = 7;
}
repaint();
break;
//activate the current button
case KeyEvent.VK_SPACE:
case KeyEvent.VK_ENTER:
activateButton( highlightButton);
break;
//new game
case KeyEvent.VK_N:
activateButton( MainMenu.BUTTON_NEW);
break;
//load game
case KeyEvent.VK_L:
activateButton( MainMenu.BUTTON_LOADGAME);
break;
//exit
case KeyEvent.VK_Q:
case KeyEvent.VK_ESCAPE:
activateButton( MainMenu.BUTTON_EXIT);
break;
//join game
case KeyEvent.VK_J:
activateButton( MainMenu.BUTTON_JOIN);
break;
//about
case KeyEvent.VK_A:
activateButton( MainMenu.BUTTON_ABOUT);
break;
//server
case KeyEvent.VK_S:
activateButton( MainMenu.BUTTON_SERVER);
break;
//help
case KeyEvent.VK_H:
activateButton( MainMenu.BUTTON_HELP);
break;
// lobby
case KeyEvent.VK_O:
activateButton( MainMenu.BUTTON_LOBBY);
break;
}//switch keycode
}//public void keyReleased( KeyEvent event )
//I don't want these, but we implement the interface
public void keyPressed( KeyEvent event ) {}
public void keyTyped( KeyEvent event ) {}
private void exit() {
//Frame frame = RiskUtil.findParentFrame(this);
//
//if ( frame instanceof JFrame && ((JFrame)frame).getDefaultCloseOperation() == JFrame.EXIT_ON_CLOSE && RiskUtil.checkForNoSandbox()) {
//
// // not actually needed as it will auto be done
// System.exit(0);
//}
myrisk.deleteRiskListener(fra);
//frame.setVisible(false);
//frame.dispose();
}
public void addLobbyButton() {
if (RiskUtil.getAddLobby(myrisk)) {
lobby.setVisible( true );
showLobby = true;
repaint();
}
}
/**
* This runs the program
* @param argv
*/
public static void main(String[] argv) {
RiskUtil.parseArgs(argv);
MainMenu mm = newMainMenuFrame( new Risk(null),JFrame.EXIT_ON_CLOSE );
mm.addLobbyButton();
}
public static MainMenu newMainMenuFrame(Risk r,int a) {
JFrame gui = new JFrame();
final MainMenu mm = new MainMenu( r,gui );
gui.setContentPane( mm );
gui.setIconImage(Toolkit.getDefaultToolkit().getImage( AboutDialog.class.getResource("icon.gif") ));
gui.setTitle( risk.engine.translation.TranslationBundle.getBundle().getString( "mainmenu.title"));
gui.setResizable(false);
gui.pack();
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.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
mm.exit();
}
});
gui.setDefaultCloseOperation(a);
gui.setVisible(true);
return mm;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -