📄 picturepanel.java
字号:
// Yura Mamyrin, Group D
package risk.engine.guishared;
import risk.engine.*;
import risk.engine.core.Card;
import risk.engine.core.Country;
import risk.engine.core.Player;
import risk.engine.core.RiskGame;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.font.TextLayout;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.awt.image.RescaleOp;
import java.io.IOException;
import java.util.Vector;
import java.net.URL;
import java.awt.geom.AffineTransform;
/**
* <p> Picture Panel </p>
* @author Yura Mamyrin
*/
public class PicturePanel extends JPanel {
public final static int NO_COUNTRY = 255;
public final static int PP_X = 677;
public final static int PP_Y = 425;
public final static int VIEW_CONTINENTS = 0;
public final static int VIEW_OWNERSHIP = 1;
public final static int VIEW_BORDER_THREAT = 2;
public final static int VIEW_CARD_OWNERSHIP = 3;
public final static int VIEW_TROOP_STRENGTH = 4;
public final static int VIEW_CONNECTED_EMPIRE = 5;
private countryImage[] CountryImages;
private Risk myrisk;
private BufferedImage original;
private BufferedImage img;
private BufferedImage tempimg;
private int[][] map;
private int c1,c2,cc;
private String strCountry;
private RescaleOp HighLight;
/**
* Creates an Picture Panel
*/
public PicturePanel(Risk r) {
myrisk=r;
this.strCountry = risk.engine.translation.TranslationBundle.getBundle().getString( "picturepanel.country");
img = null;
map = null;
// YURA YURA YURA MAYBE CHANGE 1.0F SO THAT FLAT COLORS HIGHLIGHT TOO
// 0-2 0-255
HighLight = new RescaleOp(1.5f, 1.0f, null);
setupSize(PicturePanel.PP_X , PicturePanel.PP_Y);
}
private void setupSize(int x,int y) {
if (map==null || map.length!=x || map[0].length!=y) {
//System.out.println("MAKING NEW SIZE!!!!");
Dimension size = new Dimension(x,y);
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
img = new BufferedImage(x,y, java.awt.image.BufferedImage.TYPE_INT_RGB );
tempimg = new BufferedImage(x,y, java.awt.image.BufferedImage.TYPE_INT_RGB );
map = new int[x][y];
}
}
/**
* Adds the images related to the game to the picture panel
*/
public void load() throws IOException {
RiskGame game = myrisk.getGame();
BufferedImage m = ImageIO.read(RiskUtil.openMapStream(game.getImageMap()) );
BufferedImage O = ImageIO.read(RiskUtil.openMapStream(game.getImagePic()) );
memoryLoad(m,O);
}
public void memoryLoad(BufferedImage m, BufferedImage O) {
RiskGame game = myrisk.getGame();
original = O;
cc=NO_COUNTRY;
c1=NO_COUNTRY;
c2=NO_COUNTRY;
setupSize(m.getWidth(),m.getHeight());
//System.out.print("loading: "+(game.getImagePic()).getAbsolutePath()+" "+(game.getImageMap()).getAbsolutePath() +" "+((Vector)game.getCountries()).size()+"\n");
int noc = game.getCountries().length;
{ Graphics zg = img.getGraphics(); zg.drawImage(original, 0, 0, this); zg.dispose(); }
//int[] pix = new int[ m.getWidth() ];
CountryImages = new countryImage[noc];
for (int c=0; c < noc; c++) {
CountryImages[c] = new countryImage();
}
countryImage cci;
int[] pixels = m.getRGB(0,0,m.getWidth(),m.getHeight(),null,0,m.getWidth());
// create a very big 2d array with all the data from the image map
for(int x=0; x < m.getWidth(); x++) {
for(int y=0; y < m.getHeight(); y++) {
int num = pixels[ (m.getWidth()*y) + x ] & 0xff; // (m.getRGB(x,y))&0xff;
// if ( num > noc && num !=NO_COUNTRY ) System.out.print("map error: "+x+" "+y+"\n"); // testing map
map[x][y]=num;
if ( num != NO_COUNTRY ) {
cci = CountryImages[num-1];
if (x < cci.getX1() ) { cci.setX1(x); }
if (x > cci.getX2() ) { cci.setX2(x); }
if (y < cci.getY1() ) { cci.setY1(y); }
if (y > cci.getY2() ) { cci.setY2(y); }
}
}
}
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp Gray = new ColorConvertOp( cs , null);
// create the bufferd image for each country
for (int c=0; c < CountryImages.length ; c++) {
cci = CountryImages[c];
int x1=cci.getX1();
// int x2=cci.getX2();
int y1=cci.getY1();
// int y2=cci.getY2();
int w=cci.getWidth();
int h=cci.getHeight();
// System.out.print( "Country: "+ (c+1) +" X1: "+ x1 +" Y1: "+y1 +" Width: "+ w +" Height: "+ h +"\n");
BufferedImage source = original.getSubimage(x1, y1, w, h);
BufferedImage gray = new BufferedImage(w, h, java.awt.image.BufferedImage.TYPE_INT_RGB );
Gray.filter(source , gray);
cci.setSourceImage(source);
cci.setGrayImage(gray);
cci.setNormalImage( new BufferedImage( w ,h, java.awt.image.BufferedImage.TYPE_INT_ARGB ) );
cci.setHighLightImage( new BufferedImage(w, h, java.awt.image.BufferedImage.TYPE_INT_ARGB ) );
cci.setTemp1( new BufferedImage( w ,h, java.awt.image.BufferedImage.TYPE_INT_RGB ) );
cci.setTemp2( new BufferedImage(w, h, java.awt.image.BufferedImage.TYPE_INT_RGB ) );
}
}
/**
* Paints the components
* @param g a Graphics object.
*/
public void paintComponent(Graphics g) {
super.paintComponent(g);
try {
if (img != null) {
//System.out.print("#################################################### Repainted\n");
//super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
double s = getScale();
//System.out.println("scale: "+s);
g2.translate(getDrawImageX(s),getDrawImageY(s));
g2.scale(s,s);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.drawImage(img,0,0,this);
if (c1 != NO_COUNTRY) {
g2.drawImage( CountryImages[c1-1].getHighLightImage() ,CountryImages[c1-1].getX1() ,CountryImages[c1-1].getY1() ,this);
}
if (c2 != NO_COUNTRY) {
g2.drawImage(CountryImages[c2-1].getHighLightImage() ,CountryImages[c2-1].getX1() ,CountryImages[c2-1].getY1() ,this);
}
if (cc != NO_COUNTRY) {
g2.drawImage( CountryImages[cc-1].getHighLightImage() ,CountryImages[cc-1].getX1() ,CountryImages[cc-1].getY1() ,this);
}
drawArmies(g2);
if (cc != NO_COUNTRY) {
TextLayout tl = new TextLayout( this.strCountry + " "+ myrisk.getCountryName( cc ) , g2.getFont() , g2.getFontRenderContext() );
int w = (int)tl.getAdvance();
int h = (int)tl.getAscent() + (int)tl.getDescent();
g2.setColor( new Color(255,255,255, 150) );
g2.fill(new Rectangle2D.Float( 5 , 5, w+3, h+1 ));
g2.setColor( Color.black );
tl.draw( g2, (float)6, (float)15);
}
}
}
catch(Exception e) { } // an excpetion here really does not matter
}
private int getDrawImageX(double ratio) {
return (int) (getWidth()-(map.length*ratio) )/2;
}
private int getDrawImageY(double ratio) {
return (int) (getHeight()-(map[0].length*ratio) )/2;
}
private double getScale() {
return Math.min(getHeight()/(double)map[0].length,getWidth()/(double)map.length);
}
/**
* Paints the army components
* @param g2 a 2D Graphics object.
*/
public void drawArmies(Graphics2D g2) {
RiskGame game = myrisk.getGame();
Country[] v = game.getCountries();
Country t;
int r=10;
if (game.getState()==4 || game.getState()==5 || game.getState()==10) {
int a=game.getAttacker().getColor();
int b=game.getDefender().getColor();
g2.drawImage(CountryImages[a-1].getHighLightImage() ,CountryImages[a-1].getX1() ,CountryImages[a-1].getY1() ,this);
g2.drawImage(CountryImages[b-1].getHighLightImage() ,CountryImages[b-1].getX1() ,CountryImages[b-1].getY1() ,this);
Color ac = game.getAttacker().getOwner().getColor();
g2.setColor( new Color(ac.getRed(),ac.getGreen(), ac.getBlue(), 150) );
//g2.setStroke(new BasicStroke(3));
if ( Math.abs( game.getAttacker().getX() - game.getDefender().getX() ) > (map.length / 2) ) {
if ( ((Country)game.getAttacker()).getX() > (map.length / 2) ) { // ie the attacker is on the right
g2.fillPolygon( makeArrow( game.getAttacker().getX(), ((Country)game.getAttacker()).getY(), ((Country)game.getDefender()).getX()+map.length, ((Country)game.getDefender()).getY(), r ));
g2.fillPolygon( makeArrow( game.getAttacker().getX()-map.length, ((Country)game.getAttacker()).getY(), ((Country)game.getDefender()).getX(), ((Country)game.getDefender()).getY(), r ));
}
else { // the attacker is on the left
g2.fillPolygon( makeArrow( game.getAttacker().getX(), ((Country)game.getAttacker()).getY(), ((Country)game.getDefender()).getX()-map.length, ((Country)game.getDefender()).getY(), r ));
g2.fillPolygon( makeArrow( game.getAttacker().getX()+map.length, ((Country)game.getAttacker()).getY(), ((Country)game.getDefender()).getX(), ((Country)game.getDefender()).getY(), r ));
}
}
else {
g2.fillPolygon( makeArrow( ((Country)game.getAttacker()).getX(), ((Country)game.getAttacker()).getY(), ((Country)game.getDefender()).getX(), ((Country)game.getDefender()).getY(), r ));
}
//g2.setStroke(new BasicStroke(1));
}
for (int c=0; c< v.length ; c++) {
t = v[c];
if ( ((Player)t.getOwner()) != null ) {
g2.setColor( ((Player)t.getOwner()).getColor() );
Ellipse2D ellipse = new Ellipse2D.Double();
ellipse.setFrame( t.getX()-r , t.getY()-r , (r*2), (r*2) );
g2.fill(ellipse);
//g.fillOval( t.getX()-r , t.getY()-r, (r*2), (r*2) );
g2.setColor( RiskUtil.getTextColorFor( ((Player)t.getOwner()).getColor() ) );
g2.setFont( new java.awt.Font("Arial", java.awt.Font.PLAIN, 11) );
int noa=t.getArmies();
if (noa < 10) {
g2.drawString( String.valueOf( noa ) , t.getX()-3, t.getY()+4 );
}
else if (noa < 100) {
g2.drawString( String.valueOf( noa ) , t.getX()-6, t.getY()+4 );
}
else {
g2.drawString( String.valueOf( noa ) , t.getX()-9, t.getY()+4 );
}
}
}
if (game.getGameMode() == 2 && game.getSetup() && game.getState() !=9 ) {
g2.setStroke(new BasicStroke(2));
Vector players = game.getPlayers();
for (int c=0; c< players.size() ; c++) {
if ( ((Player)players.elementAt(c)).getCapital() !=null ) {
Country capital = ((Country)((Player)players.elementAt(c)).getCapital());
g2.setColor( RiskUtil.getTextColorFor( ((Player)capital.getOwner()).getColor() ) );
Ellipse2D ellipse = new Ellipse2D.Double();
ellipse.setFrame( capital.getX()-10 , capital.getY()-10 , 19, 19);
g2.draw(ellipse);
g2.setColor( ((Player)players.elementAt(c)).getColor() );
Ellipse2D ellipse2 = new Ellipse2D.Double();
ellipse2.setFrame( capital.getX()-12 , capital.getY()-12 , 23, 23);
g2.draw(ellipse2);
}
}
g2.setStroke(new BasicStroke(1));
}
}
/**
* Paints the arrows for the game, ie - when attacking
* @param x1i x point of the attacker's co-ordinates.
* @param y1i y point of the attacker's co-ordinates.
* @param x2i x point of the defender's co-ordinates.
* @param y2i y point of the defender's co-ordinates.
* @param ri the radius of the circle
*/
public Polygon makeArrow(int x1i, int y1i, int x2i, int y2i, int ri) {
Polygon arrow;
double x1 = x1i;
double y1 = y1i;
double x2 = x2i;
double y2 = y2i;
double xd = x2-x1;
double yd = y1-y2;
double r = ri;
double l = Math.sqrt( Math.pow(xd, 2d) + Math.pow(yd, 2d) );
double a = Math.acos( (r/l) );
double b = Math.atan( (yd/xd) );
double c = Math.atan( (xd/yd) );
double x3 = r * Math.cos( a - b );
double y3 = r * Math.sin( a - b );
double x4 = r * Math.sin( a - c );
double y4 = r * Math.cos( a - c );
//System.out.print("x3="+x3+" y3="+y3+" x4="+x4+" y4="+y4+"\n");
/*
3
/|\
2-- | --3
|\ | /|
\ | /
\ | /
\ - /
/ / | \ \
2----------|--+--|----------3
\ \ | / /
/ - \
/ | \
/ | \
|/ | \|
4-- | --1
\|/
1
*/
if (x2 >= x1 && y2 <= y1) {
//System.out.print("3\n");
int xCoords[] = { (int)x1, (int)Math.round(x1+x3) , (int)x2 , (int)Math.round(x1-x4) };
int yCoords[] = { (int)y1, (int)Math.round(y1+y3) , (int)y2 , (int)Math.round(y1-y4) };
arrow = new Polygon(xCoords, yCoords, xCoords.length);
}
else if (x2 >= x1 && y2 >= y1) {
//System.out.print("1\n");
int xCoords[] = { (int)x1, (int)Math.round(x1+x3) , (int)x2 , (int)Math.round(x1+x4) };
int yCoords[] = { (int)y1, (int)Math.round(y1+y3) , (int)y2 , (int)Math.round(y1+y4) };
arrow = new Polygon(xCoords, yCoords, xCoords.length);
}
else if (x2 <= x1 && y2 <= y1) {
//System.out.print("2\n");
int xCoords[] = { (int)x1, (int)Math.round(x1-x3) , (int)x2 , (int)Math.round(x1-x4) };
int yCoords[] = { (int)y1, (int)Math.round(y1-y3) , (int)y2 , (int)Math.round(y1-y4) };
arrow = new Polygon(xCoords, yCoords, xCoords.length);
}
else { // if (x2 < x1 && y2 > y1)
//System.out.print("4\n");
int xCoords[] = { (int)x1, (int)Math.round(x1-x3) , (int)x2 , (int)Math.round(x1+x4) };
int yCoords[] = { (int)y1, (int)Math.round(y1-y3) , (int)y2 , (int)Math.round(y1+y4) };
arrow = new Polygon(xCoords, yCoords, xCoords.length);
}
return arrow;
}
/**
* Repaints the countries for each of the different views
* @param view The name of each of the map views.
*/
public synchronized void repaintCountries(int view) { // synchronized
RiskGame game = myrisk.getGame();
{ Graphics zg = tempimg.getGraphics(); zg.drawImage(original ,0 ,0 ,this); zg.dispose(); }
Vector b=null;
if (view == VIEW_CONNECTED_EMPIRE) {
Vector players = game.getPlayers();
b = new Vector();
for (int c=0; c<players.size(); c++) {
b.addAll( game.getConnectedEmpire( (Player)players.elementAt(c) ) );
}
}
for (int c=0; c < CountryImages.length ; c++) {
Color val=null;
if (view == VIEW_CONTINENTS) {
val = new Color(0,true);
}
else if (view == VIEW_OWNERSHIP) {
if ( ((Country)game.getCountryInt( c+1 )).getOwner() != null ) {
val = ((Player)((Country)game.getCountryInt( c+1 )).getOwner()).getColor();
}
else {
val = Color.GRAY;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -