home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / unuy2wen / cybcerone / orient / orientmap.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  3.8 KB  |  161 lines

  1. // OrientMap.java
  2. // 16.03.96
  3. //
  4. // the map on the orientation screen
  5.  
  6. package cybcerone.orient;
  7.  
  8. import java.util.Vector;
  9. import java.util.Enumeration;
  10. import java.awt.Rectangle;
  11. import java.awt.Point;
  12. import java.awt.Graphics;
  13. import java.awt.Event;
  14. import java.awt.Color;
  15. import java.awt.Image;
  16.  
  17. import cybcerone.utils.Appletlike;
  18. import cybcerone.utils.IdPanel;
  19.  
  20. /**
  21.  * The pretty complex and poorly commented heart of the mapping module.
  22.  */
  23. class OrientMap extends IdPanel implements Runnable {
  24.   private static final Rectangle placement = new Rectangle (0, 135, 734, 533);
  25.  
  26.   private static final String id = "OrientMap";
  27.   private static final String statusText = "Click to browse";
  28.  
  29.   private Maplikes theMaps;
  30.   private Maplike currentMap;
  31.   private Zone currentZone;
  32.  
  33.   private Thread flasher;
  34.   private boolean flashOn;
  35.  
  36.   /** to speed up painting */
  37.   private boolean redrawRedBox;
  38.   private Rectangle zone = new Rectangle ();
  39.   private Rectangle clip = new Rectangle ();
  40.   private Point boxPoint = new Point (0, 0);
  41.  
  42.   private final Point anchor = new Point (scale (732), scale (275));
  43.  
  44.   OrientMap (Appletlike app) {
  45.     super (id, statusText, app);
  46.     reshape (placement);
  47.   }
  48.  
  49.   public void setMaps (Maplikes theMaps) {
  50.     this.theMaps = theMaps;
  51.   }
  52.   
  53.   public void start () {
  54.     if (flasher == null) {
  55.       flasher = new Thread (this);
  56.       flasher.start ();
  57.     }
  58.   }
  59.  
  60.   public void stop () {
  61.     if (flasher != null)
  62.       flasher.stop ();
  63.     flasher = null;
  64.   }
  65.  
  66.   public void run () {
  67.     long now;
  68.     long updateTime = 0;
  69.  
  70.     while (flasher != null) {
  71.       if ((updateTime += 333) < (now = System.currentTimeMillis ()))
  72.     updateTime = now;
  73.  
  74.       if (currentZone != null) {
  75.     zone = currentZone.getCoords ();
  76.     clip.reshape (zone.x, zone.y, zone.width, zone.height);
  77.     if (currentMap instanceof LeafMap)
  78.       clip.add (anchor);
  79.     redrawRedBox = true;
  80.     repaint ();
  81.     if (flashOn == true)
  82.       flashOn = false;
  83.     else
  84.       flashOn = true;
  85.       }
  86.  
  87.       try {
  88.     flasher.sleep (updateTime - System.currentTimeMillis ());
  89.       } catch (InterruptedException e) {
  90.       }
  91.     }
  92.   }
  93.  
  94.   public void paint (Graphics g) {
  95.     if (currentMap != null && currentMap.getMap () != null) {
  96.       if (redrawRedBox && currentZone != null && flashOn) {
  97.     redrawRedBox = false;
  98.     g.clipRect (clip.x, clip.y - 1, clip.width + 2, clip.height + 3);
  99.       }
  100.       g.drawImage (currentMap.getMap (), 0, 0, this);
  101.     }
  102.     
  103.     if (currentZone != null && flashOn) {
  104.       g.setColor (Color.red);
  105.       zone = currentZone.getCoords ();
  106.       g.drawRect (zone.x, zone.y, zone.width, zone.height);
  107.       g.drawRect (zone.x + 1, zone.y - 1, zone.width, zone.height);
  108.       
  109.       if (currentMap instanceof LeafMap) {
  110.     boxPoint.move (zone.x + zone.width, zone.y + (zone.height/2));
  111.     
  112.     g.drawLine (boxPoint.x, boxPoint.y, anchor.x, anchor.y);
  113.     g.drawLine (boxPoint.x, boxPoint.y + 1, anchor.x, anchor.y + 1);
  114.       }
  115.     }
  116.   }
  117.  
  118.   private int min (int a, int b) {
  119.     return (a < b ? a : b);
  120.   }
  121.  
  122.   private int max (int a, int b) {
  123.     return (a > b ? a : b);
  124.   }
  125.   
  126.   public void update (Maplike newMap) {
  127.     currentMap = newMap;
  128.     if (newMap instanceof LeafMap)
  129.       update ((LeafMap) newMap);
  130.     else
  131.       currentZone = null;
  132.     redrawRedBox = false;
  133.     repaint ();
  134.   }
  135.  
  136.   public void update (LeafMap newMap) {
  137.     currentZone = newMap.getParent().find(newMap.getName());
  138.   }
  139.  
  140.  
  141.   /** set this as the new highlight zone */
  142.   public void update (Zone highlightZone) {
  143.     currentZone = highlightZone;
  144.     redrawRedBox = false;
  145.     repaint ();
  146.   }
  147.  
  148.   public boolean mouseDown (Event evt, int x, int y) {
  149.     Zone clickedZone = currentMap.find (x, y);
  150.     if (clickedZone != null) {
  151.       Maplike clickedMap = theMaps.findMap (clickedZone.getName ());
  152.       if (clickedMap != null)
  153.     app.update (clickedMap);
  154.     } else if (currentMap instanceof LeafMap) {
  155.       app.update (currentMap.getParent ());
  156.     }
  157.     return true;
  158.   }
  159.  
  160. }
  161.