home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / enxle1f6 / src / games / battle / client / europaclient / europaqueuecomponent.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  6.1 KB  |  255 lines

  1. /*
  2.  * @(#)EuropaQueueComponent.java
  3.  */
  4.  
  5. package games.Battle.client.EuropaClient;
  6.  
  7. import java.awt.*;
  8.  
  9. import games.Battle.shared.sys.Symbols;
  10. import games.Battle.shared.comm.*;
  11.  
  12. /**
  13.  * The EuropaQueueComponent is a graphical representation of a 
  14.  * QueueComponent, giving visual feedback to players on who
  15.  * is currently waiting for or currently playing a game on 
  16.  * the server.
  17.  *
  18.  * This implementation displays an image of the moon in the center
  19.  * of the component with some number of "satellites" surrounding
  20.  * the moon. When a player joins a game, his or her name
  21.  * is added to a satellite with an appropriate player color.
  22.  * 
  23.  * The player ranking is also displayed below the name.
  24.  *
  25.  * @author Alex Nicoloau
  26.  * @author Jay Steele
  27.  */
  28. public class EuropaQueueComponent extends QueueComponent 
  29. {
  30.     /**
  31.      * The media tracker used to wait for the Europa moon image
  32.      */
  33.     MediaTracker tracker = null;
  34.  
  35.     /**
  36.      * The Europa moon image.
  37.      */
  38.     Image europa = null;
  39.  
  40.     /**
  41.      * The maxium number of players in thie queue.
  42.      */
  43.     int maxPlayers;
  44.  
  45.     /**
  46.      * An array representing the positions of all the satellites.
  47.      */
  48.     Point[] satPos;
  49.  
  50.     /**
  51.      * An array of string for the names of player attached to satellites.
  52.      */
  53.     String[] satTitle;
  54.  
  55.     /**
  56.      * An array of string for the ratings of player attached to satellites.
  57.      */
  58.     String[] satRating;
  59.  
  60.     /**
  61.      * Create a EuropaQueueComponent with the given GameInfo and 
  62.      * EuropaClient. Initializes the locations of the satellites
  63.      * in the square and initiazes all of the display strings to
  64.      * nothing.
  65.      * @param inf the game info
  66.      * @param client the "owner" client applet
  67.      */
  68.     public EuropaQueueComponent(GameInfo inf, EuropaClient client) {
  69.         super(inf, client);
  70.  
  71.         maxPlayers = game.numberNeeded();
  72.  
  73.         satPos = new Point[maxPlayers];
  74.         satTitle = new String[maxPlayers];
  75.         satRating = new String[maxPlayers];
  76.  
  77.         tracker = new MediaTracker(this);
  78.         // europa = getToolkit().getImage("images/europa.gif");
  79.         europa = client.getImage(client.getCodeBase(), "images/europa.gif");
  80.         setBackground(Color.lightGray);
  81.         tracker.addImage(europa, 0);
  82.  
  83.         // wait for the image to load
  84.         try {
  85.             tracker.waitForID(0);
  86.         } 
  87.         catch (InterruptedException e) { 
  88.         }
  89.  
  90.         initSatellites();
  91.  
  92.         initFromGame();
  93.  
  94.         setBackground(Color.black);
  95.     }
  96.  
  97.     /**
  98.      * resets all the strings that are displayed in this component 
  99.      */
  100.     void initFromGame() {
  101.         maxPlayers = game.numberNeeded();
  102.         
  103.         for (int i=0; i<maxPlayers; i++) {
  104.             satTitle[i] = new String("");
  105.             satRating[i] = new String("");
  106.         }
  107.  
  108.         for (int i = 0; i < game.numberJoined(); i++) {
  109.             satTitle[i] = game.players[i].getHandle();
  110.             satRating[i] ="["+game.players[i].getRating().toString() + "]";
  111.         }
  112.     }
  113.  
  114.     /**
  115.      * make the component up to date before repainting with superclass method
  116.      */
  117.     public void resetGameInfo(GameInfo g) {
  118.         game = g;
  119.         initFromGame();
  120.         super.resetGameInfo(g);
  121.     }
  122.  
  123.     /**
  124.      * Initialize all of the satellite positions in the panel. They
  125.      * are arranged in a small semicircle to the left of the planet
  126.      * to leave room for the name text.
  127.      */
  128.     void initSatellites() {
  129.         int xradius = (int)((double)(bounds().width)/2 - 10.0);
  130.         int yradius = (int)((double)(bounds().height)/2 - 10.0);
  131.         int cx = (int)((double)bounds().width / 2.0);
  132.         int cy = (int)((double)bounds().height / 2.0);
  133.         double range = (Math.PI/1.5);
  134.         double thetaseg = range / (double)(maxPlayers-1);
  135.         for (int i=0; i<maxPlayers; i++) {
  136.             double theta = -((double)i * thetaseg + (Math.PI/2)) 
  137.                 - ((Math.PI-range)/2);
  138.             int x = (int)(Math.cos(theta) * xradius) + cx;
  139.             int y = (int)(Math.sin(theta) * yradius) + cy;
  140.             satPos[i] = new Point(x, y);
  141.         }
  142.     }
  143.  
  144.     static private final Color playerColor[] = {
  145.         new Color(200, 0, 0),
  146.         new Color(0, 200, 0),
  147.         new Color(0, 0, 200),
  148.         new Color(0, 200, 200),
  149.         new Color(200, 0, 200)
  150.     };
  151.  
  152.     boolean hilit = false;
  153.  
  154.     /**
  155.      * Return true if the clients currently executing game is
  156.      * this component's game id.
  157.      */
  158.     boolean inThisGame() {
  159.         return (game.getId() == client.getGameId());
  160.     }
  161.  
  162.     /**
  163.      * Hilite this component (when the mouse moves inside) with
  164.      * a "Quit" or "Join" box.
  165.      * @param g the graphics context to draw on
  166.      */
  167.     void drawHilite(Graphics g) {
  168.         Rectangle size = bounds();
  169.         g.drawRect(2, 2, size.width - 5, size.height - 5);
  170.         if (inThisGame())
  171.             g.drawString("Quit", size.width-40, size.height-15);
  172.         else
  173.             g.drawString("Join", size.width-40, size.height-15);
  174.     }
  175.  
  176.     /*
  177.     public void update(Graphics g) {
  178.     }
  179.     */
  180.  
  181.     /**
  182.      * Paints the panel. Any satellites with no label are painted
  183.      * gray, all others are painted their appropriate player color.
  184.      * @param g the graphics context to draw on
  185.      */
  186.     public void paint(Graphics g) {
  187.         initSatellites();
  188.  
  189.         g.setFont(new Font("Helvetica", Font.PLAIN, 12));
  190.  
  191.         g.setColor(Color.black);
  192.         Rectangle size = bounds();
  193.         //g.fillRect(0, 0, size.width, size.height);
  194.  
  195.         int w = (int)((double)(bounds().width - europa.getWidth(this)) / 2.0);
  196.         int h = (int)((double)(bounds().height - europa.getHeight(this)) / 2.0);
  197.         g.drawImage(europa, w, h, this);
  198.  
  199.         for (int i=0; i<maxPlayers; i++) {
  200.             if (satTitle[i].equals("")) {
  201.                 g.setColor(Color.lightGray);
  202.             } else {
  203.                 g.setColor(playerColor[i]);
  204.                 g.drawString(satTitle[i], satPos[i].x+5, satPos[i].y);
  205.                 g.drawString(satRating[i], satPos[i].x+5, satPos[i].y + 13);
  206.             }
  207.             g.fillOval(satPos[i].x-2, satPos[i].y-2, 4, 4);
  208.         }
  209.  
  210.         if (hilit) {
  211.             if (inThisGame())
  212.                 g.setColor(Color.red);
  213.             else
  214.                 g.setColor(Color.green);
  215.             drawHilite(g);
  216.         }
  217.     }
  218.  
  219.     /**
  220.      * Hilite this component with the appropriate color and
  221.      * draw.
  222.      */
  223.     public void hilite() {
  224.         hilit = true;
  225.         Graphics g = getGraphics();
  226.         g.setFont(new Font("Helvetica", Font.PLAIN, 12));
  227.         if (inThisGame())
  228.             g.setColor(Color.red);
  229.         else
  230.             g.setColor(Color.green);
  231.         drawHilite(g);
  232.     }
  233.  
  234.     /**
  235.      * Erase any previous hilites in this component by drawing
  236.      * in black.
  237.      */
  238.     public void normal() {
  239.         hilit = false;
  240.         Graphics g = getGraphics();
  241.         g.setFont(new Font("Helvetica", Font.PLAIN, 12));
  242.         g.setColor(Color.black);
  243.         drawHilite(g);
  244.     }
  245.  
  246.     public Dimension minimumSize() {
  247.         return new Dimension(140, 140);
  248.     }
  249.  
  250.     public Dimension preferredSize() {
  251.         return new Dimension(140, 140);
  252.     }
  253.  
  254. }
  255.