📄 battledialog.java
字号:
// Yura Mamyrin, Group D
package risk.ui.FlashGUI;
import risk.engine.Risk;
import risk.engine.RiskUtil;
import risk.engine.core.Country;
import risk.engine.core.RiskGame;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.util.Random;
/**
* <p> Battle Dialog for FlashGUI </p>
* @author Yura Mamyrin
*/
public class BattleDialog extends JDialog implements MouseListener {
private GameFrame gui;
private Risk myrisk;
private BufferedImage c1img;
private BufferedImage c2img;
private int c1num;
private int c2num;
private BufferedImage Battle;
private BufferedImage Back;
private JButton button;
private JButton retreat;
private Country country1;
private Country country2;
private Color color1;
private Color color2;
private boolean canRetreat;
private javax.swing.Timer timer;
private int[] att;
private int[] def;
private int max;
private int nod;
private int noda;
private int nodd;
private BufferedImage[] attackerSpins;
private BufferedImage[] defenderSpins;
private java.util.ResourceBundle resb;
private Polygon arrow;
private JPanel battle;
private RiskGame game;
/**
* Creates a new BattleDialog
* @param parent decides the parent of the frame
* @param modal
* @param r the risk main program
*/
public BattleDialog(GameFrame parent, boolean modal, Risk r)
{
super(parent, modal);
gui = parent;
myrisk = r;
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
try {
Battle = ImageIO.read( this.getClass().getResource("battle.jpg") );
}
catch (Exception e) {
}
Back = Battle.getSubimage(0, 0, 480, 350);
int w=29;
int h=29;
attackerSpins = new BufferedImage[6];
attackerSpins[0] = Battle.getSubimage(481, 43, w, h);
attackerSpins[1] = Battle.getSubimage(481, 73, w, h);
attackerSpins[2] = Battle.getSubimage(481, 103, w, h);
attackerSpins[3] = Battle.getSubimage(541, 43, w, h);
attackerSpins[4] = Battle.getSubimage(541, 73, w, h);
attackerSpins[5] = Battle.getSubimage(541, 103, w, h);
defenderSpins = new BufferedImage[6];
defenderSpins[0] = Battle.getSubimage(511, 43, w, h);
defenderSpins[1] = Battle.getSubimage(511, 73, w, h);
defenderSpins[2] = Battle.getSubimage(511, 103, w, h);
defenderSpins[3] = Battle.getSubimage(571, 43, w, h);
defenderSpins[4] = Battle.getSubimage(571, 73, w, h);
defenderSpins[5] = Battle.getSubimage(571, 103, w, h);
initGUI();
pack();
}
/*
* @param a the number of attacking armies
* @param b the number of defending armies
* @param ai the image of attacker
* @param bi the image of defender
* @param country1 attacking country
* @param country2 defending country
* @param c1 color of the attacker
* @param c2 color of the defender
*/
public void setup(int a, int b, BufferedImage ai, BufferedImage bi, Country country1, Country country2, Color c1, Color c2) {
c1num=a;
c2num=b;
c1img = ai;
c2img = bi;
this.country1 = country1;
this.country2 = country2;
color1=c1;
color2=c2;
att=null;
def=null;
noda=0;
nodd=0;
// this should NOT be here, but as its only used for simone and thats not used yet, its ok
// breaks undo
game = myrisk.getGame();
}
/** This method is called from within the constructor to initialize the dialog. */
private void initGUI()
{
resb = risk.engine.translation.TranslationBundle.getBundle();
setTitle(resb.getString("battle.title"));
setResizable(false);
battle = new BattlePanel();
battle.setLayout(null);
battle.addMouseListener(this);
Dimension bSize = new Dimension(480, 350);
battle.setPreferredSize( bSize );
battle.setMinimumSize( bSize );
battle.setMaximumSize( bSize );
int w=88;
int h=31;
button = GameFrame.makeRiskButton( Battle.getSubimage(196, 270, w, h), Battle.getSubimage(481, 270, w, h), Battle.getSubimage(481, 238, w, h), Battle.getSubimage(481, 302, w, h), false );
button.setText(resb.getString("battle.roll"));
button.setBounds(196, 270, 88, 31);
retreat = GameFrame.makeRiskButton( Battle.getSubimage(487, 138, w, h), Battle.getSubimage(481, 206, w, h), Battle.getSubimage(481, 174, w, h), Battle.getSubimage(487, 138, w, h), true );
retreat.setText(resb.getString("battle.retreat"));
retreat.setBounds(342, 270, 88, 31);
retreat.setVisible(false);
button.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
gui.go( "roll " + nod );
// nod=0; // NEVER PUT THIS HERE
}
}
);
retreat.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
gui.go( "retreat" );
}
}
);
battle.add(retreat);
battle.add(button);
getContentPane().add(battle);
timer = new javax.swing.Timer(10, spinDiceAction());
addWindowListener(
new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
if (canRetreat) {
gui.go( "retreat" );
}
}
}
);
int x=110;
int y=40;
int xaCoords[] = {x+60, x+130, x+130, x+200, x+130, x+130, x+60};
int yaCoords[] = {y+40, y+40, y+20, y+60, y+100, y+80, y+80};
arrow = new Polygon(xaCoords, yaCoords, xaCoords.length);
}
public void exitForm() {
//System.out.println("BattleDialog.exitForm");
button.setEnabled(false);
retreat.setVisible(false);
canRetreat=false;
max=0;
(BattleDialog.this).setTitle(resb.getString("battle.title"));
}
/**
* Sets number of attacking dice
* @param n number of dice
*/
public void setNODAttacker(int n) {
att=null;
def=null;
noda = n;
battle.repaint();
timer.start();
}
/**
* Sets number of defending dice
* @param n number of dice
*/
public void setNODDefender(int n) {
nodd = n;
}
/**
* Shows the dice results
* @param atti the attacking results
* @param defi the defending results
*/
public void showDiceResults(int[] atti, int[] defi) {
if( timer.isRunning() ) {
timer.stop();
}
noda=0;
nodd=0;
att=atti;
def=defi;
battle.repaint();
}
/**
* Spins the images of the dice
* @return Action
*/
public Action spinDiceAction() {
return new AbstractAction("spin dice action") {
public void actionPerformed (ActionEvent e) {
// repaint here slows the game down a lot!
//repaint();
drawDiceAnimated( battle.getGraphics() );
}
};
}
public void drawDiceAnimated(Graphics g) {
if (noda != 0) {
g.drawImage( attackerSpins[ r.nextInt( 6 ) ] , 116, 176, this);
if (noda > 1) {
g.drawImage( attackerSpins[ r.nextInt( 6 ) ] , 116, 207, this);
}
if (noda > 2) {
g.drawImage( attackerSpins[ r.nextInt( 6 ) ] , 116, 238, this);
}
//g.drawString("ROLLING ATTACKER " + noda +" " + Math.random() , 50, 100);
}
if (nodd != 0) {
g.drawImage( defenderSpins[ r.nextInt( 6 ) ] , 335, 176, this);
if (nodd > 1) {
g.drawImage( defenderSpins[ r.nextInt( 6 ) ] , 335, 207, this);
}
if (game.getSimone()) {
if (nodd > 2) {
g.drawImage( defenderSpins[ r.nextInt( 6 ) ] , 335, 238, this);
}
}
//g.drawString("ROLLING DEFENDER " + nodd +" " + Math.random(), 300, 100);
}
}
/**
* Checks to see if input is needed
* @param n Maximum number of dice
* @param c If you can retreat
*/
public void needInput(int n, boolean c) {
button.setEnabled(true);
max=n;
nod=max;
canRetreat=c;
att=null;
def=null;
if (canRetreat) {
retreat.setVisible(true);
setTitle(resb.getString("battle.select.attack"));
}
else {
setTitle(resb.getString("battle.select.defend"));
}
battle.repaint();
}
private static Random r = new Random();
class BattlePanel extends JPanel {
/**
* Paints the frame
* @param g The graphics
*/
public void paintComponent(Graphics g) {
// just in case in the middle of the draw the att and def get set to null
int[] atti=att;
int[] defi=def;
//super.paintComponent(g);
g.drawImage( Back ,0 ,0 ,this );
if (canRetreat) {
g.drawImage( Battle.getSubimage(481, 133, 98, 40) ,336 ,265 ,this );
}
g.drawImage(c1img, 130-(c1img.getWidth()/2), 100-(c1img.getHeight()/2), this);
g.drawImage(c2img, 350-(c2img.getWidth()/2), 100-(c2img.getHeight()/2), 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;
tl = new TextLayout( country1.getName(), font, frc); // Display
tl.draw( g2, (float) (130-(tl.getBounds().getWidth()/2)), 40f );
tl = new TextLayout( country2.getName(), font, frc); // Display
tl.draw( g2, (float) (350-(tl.getBounds().getWidth()/2)), 40f );
tl = new TextLayout( resb.getString("battle.select.dice") , font, frc);
tl.draw( g2, (float) (240-(tl.getBounds().getWidth()/2)), 320f );
Ellipse2D ellipse;
g2.setColor( color1 );
ellipse = new Ellipse2D.Double();
ellipse.setFrame( 120 , 90 , 20, 20);
g2.fill(ellipse);
g2.setColor( color2 );
ellipse = new Ellipse2D.Double();
ellipse.setFrame( 340 , 90 , 20, 20);
g2.fill(ellipse);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -