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

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