⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 world.java

📁 一些JAVA的小程序
💻 JAVA
字号:
// CS 582 - Fall 1996 - OSU// Jean-Guy Spetonimport java.awt.*;import java.util.*;public class World extends Canvas{  private static final int WIDTH  = 720;  private static final int HEIGHT = 388;  private Risk risk;  private Country[] countries;  // For double-buffering.  Image offScreenImage;  Dimension offScreenSize;  Graphics offScreenGraphics;    public World(Risk risk)  {    this.risk = risk;    countries = Country.makeWorld();    setBackground(Color.blue.darker());  }  // AWT methods.    public Dimension minimumSize()  {    return new Dimension(WIDTH, HEIGHT);  }  public Dimension preferredSize()  {    return minimumSize();  }    public void paint(Graphics g)  {    super.paint(g);        for (int i = 0; i < countries.length; i++) {      //      g.setColor(Color.red);      countries[i].paint(g);    }    g.setColor(Color.white);    g.drawLine(298, 259, 333, 234);  // Brazil to N. Africa    g.drawLine(240, 110, 256, 92);   // Quebec to Greenland    g.drawLine(173, 106, 231, 52);   // Ontario to Greenland    g.drawLine(177, 64, 209, 40);    // NWT to Greenland    g.drawLine(411, 173, 405, 161);  // Egypt to S. Europe    g.drawLine(387, 162, 394, 156);  // N. Africa to S. Europe    g.drawLine(313, 71, 319, 77);    // Greenland to Iceland    g.drawLine(343, 83, 373, 90);    // Iceland to Scandinavia    g.drawLine(335, 92, 350, 102);   // Iceland to Great Britain    g.drawLine(654, 166, 646, 155);  // Japan to Mongolia    g.drawLine(672, 141, 670, 139);  // Japan to Kamchatka    g.drawLine(464, 277, 457, 264);  // Madagascar to E. Africa    g.drawLine(457, 291, 450, 287);  // Madagascar to S. Africa  }  public void update(Graphics g)  {    Dimension d = size();    if ((offScreenImage == null) ||        (d.width != offScreenSize.width) ||        (d.height != offScreenSize.height)) {      offScreenImage = createImage(d.width, d.height);      offScreenSize = d;      offScreenGraphics = offScreenImage.getGraphics();      offScreenGraphics.setFont(getFont());    }    paint(offScreenGraphics);    g.drawImage(offScreenImage, 0, 0, null);  }  public boolean mouseDown(Event event, int x, int y)  {    Player player = risk.getCurrentPlayer();    int phase = risk.getCurrentPhase();    Country country = pointsInCountry(x, y);    int cc = risk.getCurrentCountry();    if (country != null) {      switch (phase) {      case Risk.PLACE_ARMIES:	if ((player.getArmies() > 0) && (country.getOwner() == player)) {	  // Place all armies if RMB, 1 if LMB.	  if ((event.modifiers & Event.META_MASK) != 0) {	    country.incrementArmies(player.getArmies());	    player.setArmies(0);	  }	  else {	    country.incrementArmies(1);	    player.decrementArmies(1);	  }	  risk.repaint();	  repaint();	  risk.setArmiesToPlace(player.getArmies());	} 	break;	      case Risk.ATTACK_FROM:	if ((country.getOwner() == player) &&	    (country.getArmies() > 1)) {	  risk.setPhase(Risk.ATTACK_TO, country.getNum());	}	break;      case Risk.ATTACK_TO:	// If the user clicks on the same country which s/he chose	// as the attacking country, cancel the attack.	if (country.getNum() == cc) {	  risk.setPhase(Risk.ATTACK_FROM);	}	else if ((country.getOwner() != player) &&	    (country.isAdjacentTo(countries[cc]))) {	  int rolls[] = countries[cc].attackCountry(country);	  risk.setAttackRolls(rolls);	  risk.repaint();	  repaint();	  // If territory captured, allow army movement.	  if ((country.getOwner() == countries[cc].getOwner()) &&	      (countries[cc].getArmies() > 1)) {	    // If the player killed an enemy he took his cards, and	    // thus may need to trade some in.	    if (player.getPile().size() > 4) {	      risk.setPhase(Risk.FORCED_TRADE);	    }	    else {	      risk.setPhase(Risk.ATTACK_MOVE, country.getNum());	    }	    risk.getCurrentPlayer().setCardFlag();	  }	  // Else if depleted armies, cancel attack.	  else if (countries[cc].getArmies() == 1) {	    risk.setPhase(Risk.ATTACK_FROM);	  }	}	break;      case Risk.ATTACK_MOVE:	if (country == countries[risk.getCurrentCountryTarget()]) {	  if ((event.modifiers & Event.META_MASK) != 0) {	    country.incrementArmies(countries[cc].getArmies() - 1);	    countries[cc].setArmies(1);	  }	  else {	    country.incrementArmies(1);	    countries[cc].decrementArmies(1);	  }	  repaint();	  if (countries[cc].getArmies() == 1) {	    risk.setPhase(Risk.ATTACK_FROM);	  }	}	else {	  risk.setPhase(Risk.ATTACK_FROM);	}	break;	      case Risk.FORTIFY_FROM:	if ((country.getOwner() == player) &&	    (country.getArmies() > 1)) {	  risk.setPhase(Risk.FORTIFY_TO, country.getNum());	}	break;      case Risk.FORTIFY_TO:	// If the user clicks on the same country which s/he chose	// as the attacking country, cancel the attack.	if (country.getNum() == cc) {	  risk.setPhase(Risk.FORTIFY_FROM);	}	else if ((country.getOwner() == player) &&		 (country.isAdjacentTo(countries[cc])) &&		 (countries[cc].getArmies() > 1)) {	  // If RMB, transfer all but 1 army.  Else if LMB, transfer 1.	  if ((event.modifiers & Event.META_MASK) != 0) {	    country.incrementArmies(countries[cc].getArmies() - 1);	    countries[cc].setArmies(1);	  }	  else {	    country.incrementArmies(1);	    countries[cc].decrementArmies(1);	  }	  if (countries[cc].getArmies() == 1) {	    risk.setPhase(Risk.FORTIFY_FROM);	  }	  repaint();	}	break;      }    }        return true;  }    public boolean mouseMove(Event event, int x, int y)  {    Graphics g = getGraphics();    g.setColor(getBackground());    //g.fillRect(0, 0, 100, 13);    g.fillRect(0, HEIGHT-14, 200, HEIGHT);    g.setColor(Color.white);    //g.drawString("" + x + ", " + y, 0, 12);    //System.err.println("" + x + ", " + y);    for (int i = 0; i < countries.length; i++) {      if (countries[i].inside(x, y)) {	g.drawString(countries[i].getName(), 0, HEIGHT-2);	break;      }    }        return true;  }  private Country pointsInCountry(int x, int y)  {    for (int i = 0; i < countries.length; i++) {      if (countries[i].inside(x, y)) {	return countries[i];      }    }    return null;  }    // Game methods.  // Generate a random Country ordering permutation.  Useful for the  // initial (random) distribution of countries to players.  public Country[] randomCountryPermutation()  {    Country random[] = new Country[countries.length];    Vector v = new Vector();    Random r = new Random();    // Copy the countries into a Vector so we can remove them.    for (int i = 0; i < countries.length; i++) {      v.addElement(countries[i]);    }    for (int i = 0; i < countries.length; i++) {      int j = Math.abs(r.nextInt()) % v.size();      random[i] = (Country)v.elementAt(j);      v.removeElementAt(j);    }        return random;  }  public Country getCountry(int i)  {    return countries[i];  }    public Player ownerOfContinent(int cont)  {    Player pl = countries[Country.CONTINENT_LO[cont]].getOwner();    for (int i = Country.CONTINENT_LO[cont] + 1;	 i <= Country.CONTINENT_HI[cont];	 i++) {      if (countries[i].getOwner() != pl) {	return null;      }    }    return pl;  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -