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

  1. // Paper.java
  2. // 27.02.96
  3. //
  4. // Duh, paper.
  5.  
  6. package cybcerone.utils;
  7.  
  8. import java.awt.Color;
  9. import java.awt.Image;
  10. import java.awt.Graphics;
  11. import java.awt.Rectangle;
  12. import java.awt.Event;
  13. import java.util.Enumeration;
  14.  
  15. /**
  16.  * This is the SubPanel that looks like a sheet a paper.  Draws itself in
  17.  * the left-hand side of the screen.
  18.  */
  19. public class Paper extends IdPanel {
  20.   private static final String paperStatusText = "Click for more information";
  21.  
  22.   /** This is where I draw myself. */
  23.   protected static final Rectangle placement = new Rectangle (0, 0, 734, 668);
  24.  
  25.   /** Where are my images? */
  26.   protected static final String imagePath = "personnes/";
  27.   private static final String bgFile = imagePath + "fond_text.gif";
  28.  
  29.   /* Some useful constants. */
  30.  
  31.   /** A dark shade of gray. */
  32.   protected Color darkGray = new Color (160, 160, 160);
  33.  
  34.   /** A light shade of gray. */
  35.   protected Color lightGray = new Color (192, 192, 192);
  36.  
  37.   /** Black.  You know, opposite of white? */
  38.   protected Color black = Color.black;
  39.   
  40.   /** Maximum # of lines on a page. */
  41.   protected int maxLines = 16;
  42.  
  43.   /** Height, in pixels, of a line. */
  44.   protected int lineHeight = scale (40);
  45.   
  46.   /** The border, in pixels, of the image. */
  47.   protected int border = 7;
  48.  
  49.   /** Handy spacer. */
  50.   protected int inset = scale (5);
  51.  
  52.   /** When we indent stuff, do it by this much. */
  53.   protected int indentWidth = scale (20);
  54.  
  55.   /** What we're displaying at the moment. */
  56.   protected Page currentPage;
  57.  
  58.   /** All the information at our disposal. */
  59.   protected PaintableVector theData;
  60.  
  61.   /* for double buffering */
  62.   private static Image offscreenImg;
  63.   private static Graphics offscreenG;
  64.   
  65.  
  66.   /**
  67.    * I want to live!
  68.    */
  69.   public Paper (String id, String statusText, Appletlike app) {
  70.     super (id, paperStatusText, app);
  71.     reshape (placement);
  72.     
  73.     //    setBackground (bgFile, 1);
  74.     setFont (Fonts.bigFont);
  75.     setBackground (Color.white);
  76.     setForeground (Color.black);
  77.   }
  78.  
  79.   /**
  80.    * Clear out the current page.  Don't repaint just yet, presumable
  81.    * someone's about to add some lines.
  82.    */
  83.   protected void clearPage () {
  84.     currentPage = new Page (maxLines);
  85.   }
  86.  
  87.   /**
  88.    * Returns true if this line was successfully added to the current
  89.    * page.  Can fail if there's already maxLines lines there, or if
  90.    * the page was not constructed in a way so as to be updateable.
  91.    */
  92.   protected boolean addLine (Paintable newLine) {
  93.     return currentPage.addLine (newLine);
  94.   }
  95.  
  96.   /**
  97.    * Set the data at our disposal.
  98.    */
  99.   public void setData (PaintableVector theData) {
  100.     this.theData = theData;
  101.     currentPage = new Page (theData);
  102.     repaint ();
  103.   }
  104.   
  105.   /**
  106.    * Draw my background and, if it exists, the info on the current page.
  107.    */
  108.   public void paint (Graphics g) {
  109.     super.paint (g);
  110.  
  111.     g.setColor (getBackground ());
  112.     g.fillRect (0, 0, size().width, size().height);
  113.     
  114.     g.setColor (Color.black);
  115.     g.drawRect (0, 0, size().width, size().height);
  116.     g.drawRect (1, 1, size().width - 2, size().height - 2);
  117.     g.drawRect (2, 2, size().width - 4, size().height - 4);
  118.     g.drawRect (3, 3, size().width - 6, size().height - 6);
  119.     g.drawRect (4, 4, size().width - 8, size().height - 8);
  120.     g.drawRect (5, 5, size().width - 10, size().height - 10);
  121.     g.drawRect (6, 6, size().width - 12, size().height - 12);
  122.     g.drawRect (7, 7, size().width - 14, size().height - 14);
  123.  
  124.     g.setColor (getForeground ());
  125.     g.setFont (Fonts.bigFont);
  126.     if (currentPage != null)
  127.       paintLines (g, currentPage.getLines());
  128.   }
  129.  
  130.   /** to stop the flicker */
  131.   public void update (Graphics g) {
  132.     if (offscreenG == null) {
  133.       offscreenImg = createImage (size().width, size().height);
  134.       offscreenG = offscreenImg.getGraphics ();
  135.     }
  136.  
  137.     paint (offscreenG);
  138.     g.drawImage (offscreenImg, 0, 0, this);
  139.   }
  140.  
  141.  
  142.   
  143.   /**
  144.    * For each line on the current page, draw it.  Draw a light gray line
  145.    * underneath.
  146.    */
  147.   protected void paintLines (Graphics g, PaintableVector items) {
  148.     int yPos = border + lineHeight;
  149.     
  150.     if (items != null) {
  151.       for (Enumeration e = items.elements (); e.hasMoreElements ();) {
  152.     ((Paintable)e.nextElement ()).paint (g, border + inset, yPos, this);
  153.     drawLine (g, yPos);
  154.     yPos += lineHeight;
  155.       }
  156.     }
  157.   }
  158.  
  159.   /**
  160.    * Here's how we draw that light gray line underneath each line
  161.    * of information.
  162.    */
  163.   protected void drawLine (Graphics g, int yPos) {
  164.     g.setColor (lightGray);
  165.     g.drawLine (border + inset, yPos + inset, 
  166.         scale (placement.width) - border - inset, yPos + inset);
  167.     g.setColor (black);
  168.   }
  169.  
  170.   /** 
  171.    * The user clicked on something, update our parent with whatever
  172.    * it was.
  173.    */
  174.   public boolean mouseDown (Event evt, int x, int y) {
  175.     Object item = whichItem (x, y);
  176.  
  177.     if (item != null)
  178.       app.update (item);
  179.     return true;
  180.   }
  181.   
  182.   /**
  183.    * Which object is at this location?
  184.    */
  185.   protected Object whichItem (int x, int y) {
  186.     int line = (y - border - 7) / lineHeight;
  187.     
  188.     if (currentPage != null) {
  189.       PaintableVector lines = currentPage.getLines ();
  190.       
  191.       if (lines != null & lines.size () > line) 
  192.     return lines.elementAt (line);
  193.       else
  194.     return null;
  195.     } else {
  196.       return null;
  197.     }
  198.   } 
  199.   
  200. }
  201.