home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch07 / Advertiser.java next >
Encoding:
Java Source  |  1998-12-14  |  6.7 KB  |  317 lines

  1. import java.util.*;
  2. import java.awt.*;
  3. import java.applet.Applet;
  4.  
  5. /*
  6.  * A class that performs the banner animation
  7.  */
  8. class Banner extends Panel implements Runnable {
  9.  
  10. /*
  11.  * an instance of the applet for
  12.  * invoking methods from the Advertiser class
  13.  */
  14. Advertiser advertiser;
  15.  
  16. /*
  17.  * instance of thread used for animation
  18.  */
  19. Thread thread;
  20.  
  21. /*
  22.  * the next banner image to be displayed
  23.  */
  24. Image theImage;
  25.  
  26. /*
  27.  * width and height of the new banner image
  28.  */
  29. int img_width, img_height;
  30.  
  31. /*
  32.  * offscreen image for double-buffering
  33.  */
  34. Image offscreen;
  35.  
  36. /*
  37.  * offg1 is the graphics object associated with
  38.  * offscreen image. offg2 is the clipped version
  39.  * of offg1
  40.  */
  41. Graphics offg1, offg2; 
  42.  
  43. /*
  44.  * xstart, ystart - x and y coordinate of clipping rectangle
  45.  * width, height - width and height of clipping rectangle
  46.  * effect_type - the effect type applied to the next image
  47.  */
  48. int xstart, ystart, width, height, effect_type; 
  49.  
  50. /**
  51.  * constructor just saves instance of the applet
  52.  * @param advertiser - instance of advertiser applet
  53.  */
  54. Banner (Advertiser advertiser) {
  55.  
  56.     this.advertiser = advertiser;
  57. }
  58.  
  59. /*
  60.  * thread that calls repaint() every 25ms
  61.  * to effect animation
  62.  */
  63. public void run() {
  64.  
  65.     Dimension d = getSize();
  66.     offscreen = createImage (d.width, d.height);
  67.     offg1 = offscreen.getGraphics ();
  68.     offg1.setFont (getFont ());
  69.     offg1.setColor (Color.gray);
  70.     offg1.fillRect (0, 0, d.width, d.height);
  71.     while (true) {
  72.         repaint ();
  73.         try {
  74.             Thread.sleep (25);
  75.         } catch (InterruptedException e) {
  76.             break;
  77.         }
  78.     }
  79. }
  80.  
  81. /**
  82.  * override update() method to avoid erase flicker
  83.  * this is where the drawing is done
  84.  * @param g - destination graphics object
  85.  */
  86. public synchronized void update(Graphics g) {
  87.  
  88.     int i, x, y, w, h;
  89.     switch (effect_type) {
  90.         case 0:
  91.         offg1.drawImage (theImage, 0, 0, null);
  92.         break;
  93.  
  94.         case 1:        // barn-door open
  95.         if (xstart > 0) {
  96.             xstart -= 5;
  97.             width += 10;
  98.             offg2 = offg1.create (xstart, 0, width, height);
  99.             offg2.drawImage (theImage, -xstart, 0, null);
  100.         } else offg1.drawImage (theImage, 0, 0, null); 
  101.         break;
  102.  
  103.         case 2:        // venetian blind
  104.         if (height < 10) {
  105.             height += 1;
  106.             for (y=0; y<img_height; y+=10) {
  107.                 offg2 = offg1.create (0, y, width, height);
  108.                 offg2.drawImage (theImage, 0, -y, null);
  109.             }
  110.         } else offg1.drawImage (theImage, 0, 0, null);
  111.         break;
  112.  
  113.         case 3:        // checkerboard
  114.         if (width <= 20) {
  115.             if (width <= 10) {
  116.                 i = 0;
  117.                 for (y=0; y<img_height; y+=10) {
  118.                     for (x=(i&1)*10; x<img_width; x+=20) {
  119.                         offg2 = offg1.create (x, y, width, 10);
  120.                         offg2.drawImage (theImage, -x, -y,σ
  121.  null);
  122.                     }
  123.                     i += 1;
  124.                 }
  125.             } else {
  126.                 i = 1;
  127.                 for (y=0; y<img_height; y+=10) {
  128.                     for (x=(i&1)*10; x<img_width; x+=20) {
  129.                         offg2 = offg1.create (x, y,[cc]
  130.  width-10, 10);
  131.                         offg2.drawImage (theImage,σ
  132.  -x, -y, null);
  133.                     }
  134.                     i += 1; 
  135.                 }
  136.             }
  137.             width += 5;
  138.         } else offg1.drawImage (theImage, 0, 0, null);
  139.         break;
  140.     }
  141.     g.drawImage (offscreen, 0, 0, null);
  142. }
  143.  
  144. /**
  145.  * initialize variables for clipping rectangle
  146.  * depending on effect type
  147.  * @param which - the effect type for next image
  148.  * @param img - the next image
  149.  */
  150. public void effect (int which, Image img) {
  151.  
  152.     img_width = img.getWidth (null);
  153.     img_height = img.getHeight (null); 
  154.     theImage = img;
  155.     switch (which) {
  156.         case 0:
  157.         break;
  158.  
  159.         case 1:        // barn door
  160.         xstart = img_width >> 1;
  161.         width = 0;
  162.         height = img_height;
  163.         break;
  164.  
  165.         case 2:
  166.         width = img_width;
  167.         height = 0;
  168.         break;
  169.  
  170.         case 3:
  171.         width = 0;
  172.         break;
  173.     }
  174.     effect_type = which;
  175. }
  176.  
  177. /*
  178.  * start the repaint thread
  179.  */
  180. public void start() {
  181.  
  182.     thread = new Thread(this);
  183.     thread.start();
  184. }
  185.  
  186. /*
  187.  * stop the repaint thread
  188.  */
  189. public void stop() {
  190.  
  191.     if (thread != null)
  192.         thread = null;
  193. }
  194. }
  195.  
  196. /*
  197.  * the Advertiser class proper
  198.  */
  199. public class Advertiser extends Applet implements Runnable {
  200.  
  201. /*
  202.  * instance of Banner
  203.  */
  204. Banner panel;
  205.  
  206. /*
  207.  * instance of thread for cycling effects
  208.  * for each new image
  209.  */
  210. Thread thread;
  211.  
  212. /*
  213.  * the total number of images
  214.  */
  215. int NBanners; 
  216.  
  217. /*
  218.  * the array of images
  219.  */
  220. Image img[] = new Image[10];
  221.  
  222. /*
  223.  * the delay (dwell) time in milliseconds for each image
  224.  */
  225. int delay[] = new int[10];
  226.  
  227. /*
  228.  * the effect type for each image
  229.  */
  230. int effect[] = new int[10];
  231.  
  232. /*
  233.  * called when applet is loaded
  234.  * add the banner panel, load images, and parse applet
  235.  * parameters
  236.  */
  237. public void init() {
  238.  
  239.     int i;
  240.     setLayout(new BorderLayout());
  241.  
  242.     panel = new Banner (this);
  243.     add("Center", panel);
  244.     NBanners = 0;
  245.     MediaTracker tracker = new MediaTracker (this);
  246.  
  247.     for (i=1; i<=10; i+=1) {
  248.         String param, token;
  249.         int j, next;
  250.  
  251.         param = getParameter ("T"+i);
  252.         if (param == null) break;
  253.  
  254.         StringTokenizer st = new StringTokenizer (param, " ,");
  255.  
  256.         token = st.nextToken ();
  257.         img[NBanners] = getImage (getDocumentBase(), token);
  258.         tracker.addImage (img[NBanners], i);
  259.         showStatus ("Getting image: "+token);
  260.         try {
  261.             tracker.waitForID (i);
  262.         } catch (InterruptedException e) { }
  263.  
  264.         token = st.nextToken ();
  265.         delay[NBanners] = Integer.parseInt (token);
  266.  
  267.         token = st.nextToken ();
  268.         effect[NBanners] = Integer.parseInt (token);
  269.  
  270.         NBanners += 1;
  271.     }
  272. }
  273.  
  274. /*
  275.  * thread that starts the next image transition
  276.  */
  277. public void run () {
  278.  
  279.     int current = 0;
  280.  
  281.     while (true) {
  282.         panel.effect (effect[current], img[current]);
  283.         try {
  284.             Thread.sleep (delay[current]);
  285.         } catch (InterruptedException e) { }
  286.         current += 1;
  287.         current %= NBanners; 
  288.     }
  289. }
  290.  
  291. /*
  292.  * called when applet is started
  293.  * start both threads
  294.  */
  295. public void start() {
  296.  
  297.     panel.start();
  298.     thread = new Thread(this);
  299.     thread.start();
  300. }
  301.  
  302. /*
  303.  * called when applet is stopped
  304.  * stops all threads
  305.  */
  306. public void stop() {
  307.  
  308.     panel.stop();
  309.  
  310.     if (panel != null)
  311.         panel = null;
  312.  
  313.     if (thread != null)
  314.         thread = null;
  315. }
  316. }
  317.