001 package com.croftsoft.apps.ads; 002 003 import java.applet.Applet; 004 import java.awt.*; 005 import java.io.*; 006 import java.net.*; 007 import java.util.*; 008 009 /********************************************************************* 010 * Rotating ad banner canvas. 011 * 012 * Allows rotating ad banners to be integrated into both Java applets 013 * and applications. 014 * 015 * @version 016 * 1999-03-14 017 * @author 018 * <A HREF="http://www.alumni.caltech.edu/~croft/">David W. Croft</A> 019 *********************************************************************/ 020 021 public final class AdCanvas extends Canvas implements Runnable { 022 ////////////////////////////////////////////////////////////////////// 023 ////////////////////////////////////////////////////////////////////// 024 025 /** 026 * 027 * Modify to redirect control of what ads are displayed. 028 * 029 * Example: "/mydata/ads.data" 030 * 031 * The link is relative to the CodeBase although this may be modified 032 * as well. 033 * 034 */ 035 private static final String ADS_DATA 036 = "http://www.orbs.com/advertising/data/Ads.data"; 037 038 /** 039 * 040 * How long each image is displayed, in milliseconds. 041 * 042 */ 043 private static final long AIR_TIME = 15000; 044 045 private static Random random = new Random ( ); 046 private static boolean data_loaded = false; 047 private static Vector imageURL_StringVector = new Vector ( ); 048 private static Vector site_URL_StringVector = new Vector ( ); 049 private Graphics g; 050 private Dimension size; 051 private Image offscreenImage; 052 private Graphics offscreenGraphics; 053 private Thread thread; 054 private int index; 055 private Applet applet; 056 057 ////////////////////////////////////////////////////////////////////// 058 ////////////////////////////////////////////////////////////////////// 059 060 public AdCanvas ( Applet applet ) { 061 ////////////////////////////////////////////////////////////////////// 062 this.applet = applet; 063 } 064 065 public void start ( ) { 066 ////////////////////////////////////////////////////////////////////// 067 g = getGraphics ( ); 068 size = size ( ); 069 offscreenImage = createImage ( size.width, size.height ); 070 offscreenGraphics = offscreenImage.getGraphics ( ); 071 if ( data_load ( applet ) ) ( thread = new Thread ( this ) ).start ( ); 072 } 073 074 private synchronized static boolean data_load ( Applet applet ) { 075 ////////////////////////////////////////////////////////////////////// 076 if ( data_loaded ) return true; 077 try { 078 URL dataURL = new URL ( applet.getCodeBase ( ), ADS_DATA ); 079 InputStream conn = dataURL.openStream ( ); 080 DataInputStream data = new DataInputStream ( conn ); 081 while ( true ) { 082 String line = data.readLine ( ); 083 if ( line == null ) break; 084 imageURL_StringVector.addElement ( line ); 085 site_URL_StringVector.addElement ( data.readLine ( ) ); 086 if ( data.readLine ( ) == null ) break; 087 } 088 } 089 catch ( Exception e ) { 090 e.printStackTrace ( ); 091 applet.showStatus ( e.getMessage ( ) ); 092 return false; 093 } 094 data_loaded = true; 095 return true; 096 } 097 098 public void run ( ) { 099 ////////////////////////////////////////////////////////////////////// 100 int count = imageURL_StringVector.size ( ); 101 while ( true ) { 102 int [ ] random_order = random_order_list ( count ); 103 for ( int i = 0; i < count; i++ ) { 104 index = random_order [ i ]; 105 try { 106 Image image = applet.getImage ( new URL ( 107 ( String ) imageURL_StringVector.elementAt ( index ) ) ); 108 if ( prepareImage ( image, size.width, size.height, this ) ) { 109 offscreenGraphics.drawImage ( 110 image, 0, 0, size.width, size.height, this ); 111 paint ( g ); 112 } 113 } catch ( Exception e ) { 114 e.printStackTrace ( ); 115 applet.showStatus ( e.getMessage ( ) ); 116 } 117 try { thread.sleep ( AIR_TIME ); } 118 catch ( InterruptedException e ) { } 119 } 120 } 121 } 122 123 public boolean imageUpdate ( 124 Image image, int flags, int x, int y, int w, int h ) { 125 ////////////////////////////////////////////////////////////////////// 126 if ( flags == 32 ) { 127 offscreenGraphics.drawImage ( 128 image, 0, 0, size.width, size.height, this ); 129 paint ( g ); 130 } 131 return super.imageUpdate ( image, flags, x, y, w, h ); 132 } 133 134 public boolean mouseMove ( Event evt, int x, int y ) { 135 ////////////////////////////////////////////////////////////////////// 136 if ( site_URL_StringVector == null ) return true; 137 if ( site_URL_StringVector.size ( ) <= index ) return true; 138 applet.showStatus ( 139 ( String ) site_URL_StringVector.elementAt ( index ) ); 140 return true; 141 } 142 143 public boolean mouseUp ( Event evt, int x, int y ) { 144 ////////////////////////////////////////////////////////////////////// 145 if ( site_URL_StringVector == null ) return true; 146 if ( site_URL_StringVector.size ( ) <= index ) return true; 147 try { 148 applet.getAppletContext ( ).showDocument ( new URL ( 149 ( String ) site_URL_StringVector.elementAt ( index ) ) ); 150 } catch ( MalformedURLException e ) { 151 e.printStackTrace ( ); 152 applet.showStatus ( e.getMessage ( ) ); 153 } 154 return true; 155 } 156 157 public void paint ( Graphics graphics ) { 158 ////////////////////////////////////////////////////////////////////// 159 if ( offscreenImage == null ) return; 160 graphics.drawImage ( offscreenImage, 0, 0, this ); 161 } 162 163 public void stop ( ) { 164 ////////////////////////////////////////////////////////////////////// 165 if ( thread != null ) { 166 thread.stop ( ); 167 thread = null; 168 } 169 } 170 171 public void update ( Graphics graphics ) { 172 ////////////////////////////////////////////////////////////////////// 173 paint ( graphics ); 174 } 175 176 public void repaint ( ) { 177 ////////////////////////////////////////////////////////////////////// 178 paint ( g ); 179 } 180 181 public static int [ ] random_order_list ( int count ) { 182 ////////////////////////////////////////////////////////////////////// 183 Vector vector = new Vector ( count ); 184 for ( int i = 0; i < count; i++ ) { 185 vector.addElement ( new Integer ( i ) ); 186 } 187 int [ ] random_order = new int [ count ]; 188 for ( int i = 0; i < count; i++ ) { 189 Integer lucky = ( Integer ) vector.elementAt ( 190 ( int ) roll ( 1, count - i, -1 ) ); 191 vector.removeElement ( lucky ); 192 random_order [ i ] = lucky.intValue ( ); 193 } 194 return random_order; 195 } 196 197 public static long roll ( 198 long multiplier, 199 long base, 200 long offset ) { 201 ////////////////////////////////////////////////////////////////////// 202 long temp = 0; 203 ////////////////////////////////////////////////////////////////////// 204 for ( long index_roll = 0; index_roll < multiplier; index_roll++ ) { 205 temp += 1 + Math.round ( 206 ( double ) ( base - 1 ) * random.nextDouble ( ) ); 207 } 208 return temp + offset; 209 } 210 211 ////////////////////////////////////////////////////////////////////// 212 ////////////////////////////////////////////////////////////////////// 213 }