📄 mainmenu.java
字号:
// Yura Mamyrin, Group D
package risk.ui.FlashGUI;
import risk.engine.guishared.*;
import risk.engine.Risk;
import risk.engine.RiskUtil;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.MouseInputListener;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.image.BufferedImage;
/**
* <p> Main Menu for FlashGUI </p>
* @author Yura Mamyrin <yura@yura.net>
* @author Christian Weiske <cweiske@cweiske.de>
*/
public class MainMenu extends JPanel implements MouseInputListener, KeyListener {
private final static String version = "1.0.3.4";
private final static String product;
static {
product = "Flash GUI for " + RiskUtil.getGameName();
}
private BufferedImage MenuImage;
private BufferedImage Server;
private Risk myrisk;
private FlashRiskAdapter fra;
private boolean serverRunning;
private JoinDialog joinDialog;
private final static int BUTTON_EXIT = 100;
private final static int BUTTON_NEW = 1;
private final static int BUTTON_SERVER = 2;
private final static int BUTTON_LOADGAME = 3;
private final static int BUTTON_HELP = 4;
private final static int BUTTON_JOIN = 5;
private final static int BUTTON_ABOUT = 6;
private final static int BUTTON_LOBBY = 7;
private final static int BUTTON_DONATE = 8;
private JLabel lobby;
private java.util.ResourceBundle resBundle = risk.engine.translation.TranslationBundle.getBundle();
private boolean showLobby;
private Cursor hand;
private Cursor defaultCursor;
/**
* Creates a new MainMenu
* @param r the risk main program
*/
public MainMenu(Risk r,Frame gui) {
myrisk = r;
fra = new FlashRiskAdapter(this, myrisk);
try {
MenuImage = ImageIO.read( this.getClass().getResource("menu.jpg") );
Server = MenuImage.getSubimage(400, 490, 60, 60);
} catch (Exception e) { }
Dimension menuSize = new Dimension(400,550);
addMouseListener(this);
addMouseMotionListener(this);
setPreferredSize(menuSize);
setMinimumSize(menuSize);
setMaximumSize(menuSize);
//key control
highlightButton=0;
serverRunning = false;
gui.setFocusTraversalKeysEnabled( false);
gui.addKeyListener( this );
setLayout(null);
// (Risk.applet == null)?"mainmenu.online":"mainmenu.loading"
lobby = new JLabel( resBundle.getString("mainmenu.online"), new javax.swing.ImageIcon( this.getClass().getResource("earth.gif") ),JLabel.CENTER );
lobby.setBounds(152,409,95,95);
lobby.setHorizontalTextPosition(JLabel.CENTER);
lobby.setFont( new java.awt.Font("Arial", java.awt.Font.BOLD, 18) );
lobby.setVisible(false);
lobby.setForeground( Color.BLACK );
add(lobby);
hand = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
defaultCursor = getCursor();
}
/**
* Checks the server's state
* @param s The server's state
*/
public void setServerRunning(boolean s) {
serverRunning = s;
repaint();
}
public void hideJoinDialog(boolean a) {
if (!a) {
joinDialog.exitForm();
joinDialog = null;
}
addMouseListener(this);
addMouseMotionListener(this);
//loading.setVisible(false);
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
/**
* Paints the panel
* @param g The graphics
*/
public void paintComponent(Graphics g) {
g.drawImage( MenuImage ,0 ,0 ,400 ,550 ,0 ,0 ,400 ,550 ,this );
if (highlightButton==BUTTON_NEW) {
g.drawImage( MenuImage ,57 ,219 ,187 ,269 ,400 ,0 ,530 ,50 ,this );
}
else if (highlightButton==BUTTON_SERVER) {
g.drawImage( MenuImage ,212 ,219 ,342 ,269 ,400 ,50 ,530 ,100 ,this );
}
else if (highlightButton==BUTTON_LOADGAME) {
g.drawImage( MenuImage ,57 ,279 ,187 ,329 ,400 ,100 ,530 ,150 ,this );
}
else if (highlightButton==BUTTON_HELP) {
g.drawImage( MenuImage ,212 ,279 ,342 ,329 ,400 ,150 ,530 ,200 ,this );
}
else if (highlightButton==BUTTON_JOIN) {
g.drawImage( MenuImage ,57 ,339 ,187 ,389 ,400 ,200 ,530 ,250 ,this );
}
else if (highlightButton==BUTTON_ABOUT) {
g.drawImage( MenuImage ,212 ,339 ,342 ,389 ,400 ,250 ,530 ,300 ,this );
}
//else if (highlightButton==BUTTON_LOBBY) {
// g.drawImage( MenuImage ,145 ,401 ,255 ,511 ,400 ,300 ,510 ,410 ,this );
//}
else if (button==BUTTON_NEW) {
g.drawImage( MenuImage ,57 ,219 ,187 ,269 ,530 ,0 ,660 ,50 ,this );
}
else if (button==BUTTON_SERVER) {
g.drawImage( MenuImage ,212 ,219 ,342 ,269 ,530 ,50 ,660 ,100 ,this );
}
else if (button==BUTTON_LOADGAME) {
g.drawImage( MenuImage ,57 ,279 ,187 ,329 ,530 ,100 ,660 ,150 ,this );
}
else if (button==BUTTON_HELP) {
g.drawImage( MenuImage ,212 ,279 ,342 ,329 ,530 ,150 ,660 ,200 ,this );
}
else if (button==BUTTON_JOIN) {
g.drawImage( MenuImage ,57 ,339 ,187 ,389 ,530 ,200 ,660 ,250 ,this );
}
else if (button==BUTTON_ABOUT) {
g.drawImage( MenuImage ,212 ,339 ,342 ,389 ,530 ,250 ,660 ,300 ,this );
}
//else if (button==BUTTON_LOBBY) {
// g.drawImage( MenuImage ,145 ,401 ,255 ,511 ,530 ,300 ,640 ,410 ,this );
//}
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
FontRenderContext frc = g2.getFontRenderContext();
Font font = g2.getFont();
g2.setColor( Color.black );
TextLayout tl;
/* This adds underlines to the strings, but it doesn't look good and is too much effort for that
java.text.AttributedString as = new java.text.AttributedString(resBundle.getString( "mainmenu.newgame"));
as.addAttribute(java.awt.font.TextAttribute.UNDERLINE, java.awt.font.TextAttribute.UNDERLINE_ON, 0,1);
as.addAttribute(java.awt.font.TextAttribute.FONT, font);
tl = new TextLayout( as.getIterator(), frc);
*/
tl = new TextLayout( resBundle.getString( "mainmenu.newgame") , font, frc);
tl.draw( g2, (float) (122-tl.getBounds().getWidth()/2), (float)247 );
if (serverRunning) {
tl = new TextLayout( resBundle.getString( "mainmenu.stopserver") , font, frc);
tl.draw( g2, (float) (277-tl.getBounds().getWidth()/2), (float)247 );
g.drawImage( Server, 340, 490, this );
}
else {
tl = new TextLayout( resBundle.getString( "mainmenu.startserver") , font, frc);
tl.draw( g2, (float) (277-tl.getBounds().getWidth()/2), (float)247 );
}
tl = new TextLayout( resBundle.getString( "mainmenu.loadgame"), font, frc);
tl.draw( g2, (float) (122-tl.getBounds().getWidth()/2), (float)309 );
tl = new TextLayout( resBundle.getString( "mainmenu.help") , font, frc);
tl.draw( g2, (float) (277-tl.getBounds().getWidth()/2), (float)309 );
tl = new TextLayout( resBundle.getString( "mainmenu.joingame") , font, frc);
tl.draw( g2, (float) (122-tl.getBounds().getWidth()/2), (float)369 );
tl = new TextLayout( resBundle.getString( "mainmenu.about") , font, frc);
tl.draw( g2, (float) (277-tl.getBounds().getWidth()/2), (float)369 );
font = new java.awt.Font("Arial", java.awt.Font.BOLD, 24);
//tl = new TextLayout( resBundle.getString( "mainmenu.quit") , font, frc);
//tl.draw( g2, (float) (200-tl.getBounds().getWidth()/2), (float)465 );
}
private int button;
private int currentButton;
private int pressedButton;
private int highlightButton;
//**********************************************************************
// MouseListener Interface
//**********************************************************************
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
/**
* Works out what to do when the move has been presed
* @param e A mouse event
*/
public void mousePressed(MouseEvent e) {
highlightButton = 0;
currentButton=insideButton(e.getX(),e.getY());
if (currentButton != 0) {
pressedButton = currentButton;
button = currentButton;
repaint();
}
}
/**
* Works out what to do when a mouse has been released
* @param e A mouse event
*/
public void mouseReleased(MouseEvent e) {
int thebutton=0;
if (pressedButton == currentButton) {
thebutton = pressedButton;
}
if (button != 0) {
button=0;
}
highlightButton=currentButton;
repaint();
activateButton( thebutton);
}//public void mouseReleased(MouseEvent e)
/**
* a button shall be activated
*/
private void activateButton(int thebutton) {
if (thebutton != 0) {
switch (thebutton) {
case MainMenu.BUTTON_NEW:{
myrisk.parser("newgame");
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
removeMouseListener(this);
removeMouseMotionListener(this);
//loading.setVisible(true);
break;
}
case MainMenu.BUTTON_SERVER: {
if (serverRunning) {
myrisk.parser("killserver");
}
else {
myrisk.parser("startserver");
}
break;
}
case MainMenu.BUTTON_LOADGAME: {
String name = RiskUtil.getLoadFileName(
RiskUtil.findParentFrame(this),
RiskUtil.SAVES_DIR,
RiskFileFilter.RISK_SAVE_FILES
);
if (name!=null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -