home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Java / Java.zip / rdb084r2.zip / TGShow.nrx < prev    next >
Text File  |  1997-03-15  |  3KB  |  100 lines

  1. /* TGShow: an applet which shows an image, some text, or both */
  2. /* ---------------------------------------------------------- */
  3. /* Based on GifShow.nrx by Mike Cowlishaw,                    */
  4. /* as an example of Plugin for MaxBase                        */
  5. /* ---------------------------------------------------------- */
  6.  
  7. options binary      -- optional; runs a bit faster if set
  8.  
  9. class TGShow extends Applet
  10.   gif      =String  -- name of the image
  11.   recipe   =String
  12.   goodimage=Image   -- the Image to draw (null if no good)
  13.   rRecTxt = Rexx[1000]
  14.   rMaxLine = Rexx ""
  15.   rxRecipe = RXFile()
  16.  
  17.  /* init gets the requested image, then uses a MediaTracker to track it
  18.     and wait until it has all arrived.  If it looks good, the goodimage
  19.     property is set so paint(g) can use it. */
  20.  
  21.  method init
  22.   rx = RXFile()
  23.  
  24.   gif = (rx.linein()).strip()    -- Get the name of the image
  25.   recipe = (rx.linein()).strip() -- Get the name of the text file
  26.  
  27.  
  28.   -- If there is a text file, read it.
  29.  
  30.   rRecTxt[0]=0
  31.   if rxRecipe.stream(recipe, "c", "query exists") \= "" then
  32.   do
  33.    rxRecipe.stream(recipe, "c", "open read")
  34.    loop while(rxRecipe.lines() \= 0)
  35.     rRecTxt[0] = rRecTxt[0] + 1
  36.     rRecTxt[rRecTxt[0]] = rxRecipe.linein()
  37.     if rRecTxt[rRecTxt[0]].length() > rMaxLine.length() then
  38.      rMaxLine = rRecTxt[rRecTxt[0]]
  39.    end
  40.    rxRecipe.stream("c", "close")
  41.   end
  42.   else
  43.    if rxRecipe.stream(gif, "c", "query exists") = "" then
  44.     exit  -- If we don't have a valid image OR text file, then exit.
  45.  
  46.   newimage=getImage(getDocumentBase(), gif)  -- get the image
  47.   tracker=MediaTracker(this)                 -- 'this' is the "observer"
  48.   tracker.addImage(newimage, 0)              -- track image arrival, ID=0
  49.   do
  50.     tracker.waitForID(0)                     -- wait for image 0 to complete
  51.   catch InterruptedException                 -- something stopped us
  52.     return                                   -- can do no more
  53.   end
  54.   -- Good images have useful dimensions, bad images have -1 or 0 in X or Y
  55.   if newimage.getWidth(this)>0
  56.    then if newimage.getHeight(this)>0
  57.    then goodimage=newimage
  58.  
  59.   if goodimage = null & rRecTxt[0] = 0 then -- again, no text and no image
  60.    exit
  61.  
  62.  
  63.  method paint(g=Graphics)
  64.   w = int
  65.   h = int
  66.   f = Font("Courier", Font.PLAIN, 12)
  67.   iCount = int
  68.  
  69.   if goodimage\=null then
  70.   do
  71.    w = goodimage.getWidth(this)
  72.    h = goodimage.getHeight(this);
  73.  
  74.    if rRecTxt[0] > 0 then -- The file has to have some text in it to be valid.
  75.    do
  76.  
  77.     -- Resize the window
  78.     if h > f.getSize()*rRecTxt[0] then
  79.      this.resize(w + (f.getSize() % 7)*5*rMaxLine.length() + 150, h + 5)
  80.     else
  81.      this.resize(w + (f.getSize() % 7)*5*rMaxLine.length() + 150, f.getSize()*rRecTxt[0] + 10)
  82.  
  83.     loop iCount = 1 to rRecTxt[0] -- Draw the text file
  84.      g.drawString(rRecTxt[iCount], w + 5, f.getSize() * iCount)
  85.     end
  86.    end 
  87.    else
  88.     this.resize(w + 5, h + 5)
  89.    g.drawImage(goodimage, 1, 1, w, h, this) -- Draw the image
  90.   end
  91.   else -- No image.
  92.   do
  93.    g.drawString('Errore!', 5, f.getSize())
  94.    this.resize((f.getSize() % 7)*4*rMaxLine.length() + 150, f.getSize()*rRecTxt[0] + 10)
  95.    loop iCount = 1 to rRecTxt[0]
  96.     g.drawString(rRecTxt[iCount], 5, f.getSize() * iCount)
  97.    end 
  98.   end
  99.  
  100.