Hi,

I have a task to write a class for J2ME applications to include ads (banners and links) from
our ads server. It will get the banners and URL links from our ads server once the app's started.

Basically, our ads server will return the banners' URL and URL links in text forms to the calling
application and my class needs to rotate the banners and URL Links periodically which the interval
can be set by the publishers.

Now, I'm stuck in changing the banner and its URL periodically. I'm using a canvas to display the
banner and a click of "*" will launch the browser. Currently, i'm hard-coding the banner's URL and
its link's URL. My code is shown as below:

1) a) Can anyone give me ideas on how i can refresh the banner and its URL link based on the stored banner and URL
links in text forms returned from the ads server?
b) Referring to the class SimpleSlidingCanvas, i can display the ads based on
bannerURL1 and linkURL1, how can i rotate the ads to bannerURL2 & linkURL1 and etc every 10 minutes or any configured time?


2) Currently, i'm writing the class (class SimpleSlidingCanvas) by extending Canvas and this class will be used by the publisher to display
our ads (banners and links).
a) Is this the right method?
b) Or what's your suggestion/advise to improve the design for the ease of use by publisher to use my class?

Any suggestions/advises are welcomed as i'm new in J2ME development.

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import java.io.*;
import java.util.*;

public class SlideImageInCanvas extends MIDlet{
private Display display;
private SimpleSlidingCanvas canvas;
private Random rand;
private final int min = 18364;//Either min or max will be used as the partnerid
private final int max = 18365;
private int partnerId;

public SlideImageInCanvas() {
display = Display.getDisplay(this);
rand = new Random();
partnerId = rand.nextInt(max - min + 1) + min;
canvas = new SimpleSlidingCanvas(this, partnerId);
}

public void startApp() throws MIDletStateChangeException{
display.setCurrent(canvas);
}
public void pauseApp(){}
public void destroyApp(boolean unconditional){
notifyDestroyed();
}
}

//The class to display the ads
class SimpleSlidingCanvas extends Canvas implements Runnable{
public GifDecoder d; //GifDecoder is a class used to display gif

Image image = null;
private MIDlet midlet;
private Date date;
String timeStamp;
private long tDate;
String BannerURL;
String LinkURL;

String BannerURL1 = "http://www.MyAdsServer.com/bannerurl.php?ad=1&partnerid=";
String LinkURL1 = "http://www.MyAdsServer.com/linkurl.php?ad=1&partnerid=";
String BannerURL2 = "http://www.MyAdsServer.com/bannerurl.php?ad=2&partnerid=";
String LinkURL2 = "http://www.MyAdsServer.com/linkurl.php?ad=2&partnerid=";


String showText = "Click * to visit";

public SimpleSlidingCanvas(MIDlet midlet_, int partnerId){
date = new Date();
tDate = date.getTime();
timeStamp = Long.toString(tDate);

//I'm only able to display ads based on BannerURL1 and LinkURL1 but to rotate the ads.
BannerURL = BannerURL1 + partnerId + "&ts=" + timeStamp.substring((timeStamp.length() - 5));
LinkURL = LinkURL1 + partnerId + "&ts=" + timeStamp.substring((timeStamp.length() - 5));

midlet = midlet_;
try{
this.image = getImage(BannerURL);
new Thread(this).start();
}catch(Exception e){
e.printStackTrace();
}
}


public void run(){
int t;
if (image != null) {
while(true) {
int n = d.getFrameCount();
for (int i = 0; i < n; i++) {
image = d.getFrame(i);
t = d.getDelay(i);
repaint();
serviceRepaints();
try {
Thread.sleep(t);
} catch (Exception ex) {}
}
}
}
}

Image getImage(String url) throws IOException {
HttpConnection hC = null;
DataInputStream dS = null;
Image mapImage = null;

try {
hC = (HttpConnection) Connector.open(url);
hC.setRequestMethod(HttpConnection.GET);
hC.setRequestProperty("IF-Modified-Since", "10 Nov 2000 17:29:12 GMT");
hC.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.1");
hC.setRequestProperty("Content-Language", "en-CA");
} catch (IOException e) { }

int iByteCount;
iByteCount = (int) hC.getLength();
dS = hC.openDataInputStream();

//Assume J2ME doesn't support native GIF format
d = new GifDecoder();
if (d != null) {
int err = d.read(dS);
if (err == 0) {
mapImage = d.getImage();
}
}

hC.close();
dS.close();
return mapImage;

}


protected void paint(Graphics g){
g.setColor(0xffffff);
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());

if(image != null){
g.setColor(0x000000);
g.drawString(showText, getWidth()/2, getHeight()/2 + 35, Graphics.HCENTER|Graphics.BASELINE);

g.drawImage(image, getWidth() / 2, getHeight() / 2 + 50, Graphics.HCENTER
| Graphics.VCENTER);
} else {
g.setColor(0x000000);
g.drawString("No image available", getWidth() / 2, getHeight() / 2,
Graphics.HCENTER | Graphics.BASELINE);
}
}

public void keyPressed(int keyCode) {
if(keyCode == KEY_STAR) {
try {
if (midlet.platformRequest(this.LinkURL) == true) {
midlet.notifyDestroyed();
}
}
catch (ConnectionNotFoundException ex) {
ex.printStackTrace();}

}
repaint();
}



}