home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-12-14 | 7.8 KB | 360 lines |
-
- import java.util.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.applet.Applet;
- import java.net.*;
-
- /*
- * A class which performs the banner animation
- */
- class Banner extends Panel implements Runnable{
-
- /*
- * an instance of the applet for
- * invoking methods from Advertiser class
- */
- Advertiser advertiser;
-
- /*
- * instance of thread used for animation
- */
- Thread thread;
-
- /*
- * the next banner image to be displayed
- */
- Image theImage;
-
- /*
- * width and height of the new banner image
- */
- int img_width, img_height;
-
- /*
- * offscreen image for double-buffering
- */
- Image offscreen;
-
- /*
- * offg1 is the graphics object associated with
- * offscreen image. offg2 is the clipped version
- * of offg1
- */
- Graphics offg1, offg2;
-
- /*
- * xstart, ystart - x and y coordinate of clipping rectangle
- * width, height - width and height of clipping rectangle
- * effect_type - the effect type applied the next image
- */
- int xstart, ystart, width, height, effect_type;
-
- /**
- * constructor just saves instance of the applet
- * @param advertiser - instance of Advertiser applet
- */
- Banner (Advertiser advertiser) {
-
- this.advertiser = advertiser;
-
- }
-
- /*
- * thread that calls repaint() every 25 ms
- * to effect animation
- */
- public void run() {
-
- Dimension d = getSize();
- offscreen = createImage (d.width, d.height);
- offg1 = offscreen.getGraphics ();
- offg1.setFont (getFont ());
- offg1.setColor (Color.gray);
- offg1.fillRect (0, 0, d.width, d.height);
- while (true) {
- repaint ();
- try {
- Thread.sleep (25);
- } catch (InterruptedException e) {
- break;
- }
- }
- }
-
- /**
- * override update() method to avoid erase flicker
- * this is where the drawing is done
- * @param g - destination graphics object
- */
- public synchronized void update(Graphics g) {
-
- int i, x, y, w, h;
- switch (effect_type) {
- case 0:
- offg1.drawImage (theImage, 0, 0, null);
- break;
-
- case 1: // barn-door open
- if (xstart > 0) {
- xstart -= 5;
- width += 10;
- offg2 = offg1.create (xstart, 0, width, height);
- offg2.drawImage (theImage, -xstart, 0, null);
- } else offg1.drawImage (theImage, 0, 0, null);
- break;
-
- case 2: // venetian blind
- if (height < 10) {
- height += 1;
- for (y=0; y<img_height; y+=10) {
- offg2 = offg1.create (0, y, width, height);
- offg2.drawImage (theImage, 0, -y, null);
- }
- } else offg1.drawImage (theImage, 0, 0, null);
- break;
-
- case 3: // checker board
- if (width <= 20) {
- if (width <= 10) {
- i = 0;
- for (y=0; y<img_height; y+=10) {
- for (x=(i&1)*10; x<img_width; x+=20) {
- offg2 = offg1.create (x, y, width, 10);
- offg2.drawImage (theImage, -x, -y, null);
- }
- i += 1;
- }
- } else {
- i = 1;
- for (y=0; y<img_height; y+=10) {
- for (x=(i&1)*10; x<img_width; x+=20) {
- offg2 = offg1.create (x, y, width-10, 10);
- offg2.drawImage (theImage, -x, -y, null);
- }
- i += 1;
- }
- }
- width += 5;
- } else offg1.drawImage (theImage, 0, 0, null);
- break;
- }
- g.drawImage (offscreen, 0, 0, null);
- }
-
- /**
- * Initialize variables for clipping rectangle
- * depending on effect type
- * @param which - the effect type for next image
- * @param img - the next image
- */
- public void effect (int which, Image img) {
-
- img_width = img.getWidth (null);
- img_height = img.getHeight (null);
- theImage = img;
- switch (which) {
- case 0:
- break;
-
- case 1: // barn door
- xstart = img_width >> 1;
- width = 0;
- height = img_height;
- break;
-
- case 2:
- width = img_width;
- height = 0;
- break;
-
- case 3:
- width = 0;
- break;
- }
- effect_type = which;
- }
-
- /*
- * Start the repaint thread
- */
- public void start() {
-
- thread = new Thread(this);
- thread.start();
- }
-
- /*
- * Stop the repaint thread
- */
- public void stop() {
-
- if (thread != null)
- thread = null;
- }
-
-
- }
-
- /*
- * the Advertiser class proper
- */
- public class Advertiser extends Applet implements Runnable, MouseListener {
-
- /*
- * instance of Banner
- */
- Banner panel;
-
- /*
- * instance of thread for cycling effects
- * for each new image
- */
- Thread thread;
-
- /*
- * the total number of images
- */
- int NBanners;
-
- /*
- * the array of images
- */
- Image img[] = new Image[10];
-
- /*
- * the delay (dwell) time in milliseconds for each image
- */
- int delay[] = new int[10];
-
- /*
- * the effect type for each image
- */
- int effect[] = new int[10];
-
- /*
- * the URL to go to for each image
- */
- URL url[] = new URL[10];
-
- /*
- * the index variable pointing to the
- * current image, delay time, and associated URL
- */
- int current = 0;
-
- /*
- * called when applet is loaded
- * add the banner panel, load images, and parse applet
- * parameters
- */
- public void init() {
-
- int i;
- setLayout(new BorderLayout());
-
- panel = new Banner (this);
- panel.addMouseListener(this);
-
- add("Center", panel);
- NBanners = 0;
- MediaTracker tracker = new MediaTracker (this);
-
- for (i=1; i<=10; i+=1) {
- String param, token;
- int j, next;
-
- param = getParameter ("T"+i);
- if (param == null) break;
-
- StringTokenizer st = new StringTokenizer (param, " ,");
-
- token = st.nextToken ();
- img[NBanners] = getImage (getDocumentBase(), token);
- tracker.addImage (img[NBanners], i);
- showStatus ("Getting image: "+token);
- try {
- tracker.waitForID (i);
- } catch (InterruptedException e) { }
-
- token = st.nextToken ();
- delay[NBanners] = Integer.parseInt (token);
-
- token = st.nextToken ();
- effect[NBanners] = Integer.parseInt (token);
-
- token = st.nextToken ();
- try {
- url[NBanners] = new URL (token);
- } catch (MalformedURLException e) {
- url[NBanners] = null;
- }
-
- NBanners += 1;
- }
- }
-
- /*
- * thread that starts the next image transition
- */
- public void run () {
-
- while (true) {
- panel.effect (effect[current], img[current]);
- try {
- Thread.sleep (delay[current]);
- } catch (InterruptedException e) { }
- current += 1;
- current %= NBanners;
- }
- }
-
- /*
- * called when applet is started
- * start both threads
- */
- public void start() {
-
- panel.start();
- thread = new Thread(this);
- thread.start();
- }
-
- /*
- * called when applet is stopped
- * stops all threads
- */
- public void stop() {
-
- panel.stop();
- if (thread != null)
- thread = null;
- }
- /**
- * Called when user clicks in the applet
- * Force the browser to show the associated URL
- * MouseListener required methods*/
- /////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////
- public void mouseClicked(MouseEvent e){}
- public void mouseEntered(MouseEvent e){}
- public void mouseExited(MouseEvent e){}
-
- public void mouseReleased(MouseEvent e){}
-
-
- public void mousePressed(MouseEvent e)
- {
-
- if (url[current] != null)
- {
- System.out.println("current " + url[current]);
- getAppletContext().showDocument (url[current]);
- }
- }
-
- /////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////
-
- }
-