mport javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
public class TestMIDlet extends MIDlet
{
private Display display;
private menuCanvas myCanvas = new menuCanvas(this);
public TestMIDlet()
{}
public void startApp()
{
Display.getDisplay(this).setCurrent(myCanvas);
}
public void pauseApp()
{}
public void destroyApp(boolean uncond)
{
notifyDestroyed();
}
}
//menuCanvas.java
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class menuCanvas extends Canvas
{
String [] myStrings = {"Favorite","Flag","Music","Star","Tool"};
private int selectedIndex = 0;
private String selectedLink;
public TestMIDlet parent;
public static int iKEY_UP = -1;
public static int iKEY_DOWN = -2;
public static int iKEY_LEFT = -3;
public static int iKEY_RIGHT = -4;
public static int iKEY_FIRE = -5;
public static int iKEY_SOFT1 = -6;
public static int iKEY_SOFT2 = -7;
public menuCanvas(TestMIDlet midlet)
{
parent = midlet;
}
protected void paint(Graphics g) {
//This methods paints on screen (mandatory)
drawBackground(g);
drawImageButtons(g);
}
void drawBackground(Graphics g){
// To draw the black background
g.setColor(255,255,255);
g.fillRect(0, 0, getWidth(), getHeight());
}
void drawImageButtons(Graphics g)
{
// draws Image and Text on the screen
g.setColor(185,185,255);
if(selectedIndex == 0)
g.fillRect(18,13,100,25);
else if(selectedIndex == 1)
g.fillRect(18,38,100,25);
else if(selectedIndex == 2)
g.fillRect(18,78,100,25);
else if(selectedIndex == 3)
g.fillRect(18,118,100,25);
else if(selectedIndex == 4)
g.fillRect(18,158,100,25);
g.setColor(0,0,0);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL));
int space = 40;
for (int i =0 ; i < 5 ; i++)
{
if(i == 0)
g.drawString(myStrings[i], 25, 15, Graphics.LEFT | Graphics.TOP);
else
g.drawString(myStrings[i], 25, space*i, Graphics.LEFT | Graphics.TOP);
}
g.setColor(0,0,0);
g.drawString("Select",10,getHeight()-5,Graphics.BOTTOM|Graphics.LEFT);
g.drawString("Exit",getWidth()-10,getHeight()-5,Graphics.BOTTOM|Graphics.RIGHT);
}
public void keyPressed(int key)
{
if(key == iKEY_DOWN)
{
if(selectedIndex < 4)
selectedIndex++;
else if(selectedIndex == 4)
selectedIndex = 0;
}
else if(key == iKEY_UP)
{
if(selectedIndex > 0)
selectedIndex--;
else if(selectedIndex == 0)
selectedIndex = 4;
}
else if(key == iKEY_SOFT1 || key == iKEY_FIRE)
{
keypressFIRE();
new connection();
}
else if(key == iKEY_SOFT2)
parent.destroyApp(true);
repaint();
}
public void keypressFIRE()
{
if(selectedIndex == 0)
selectedLink="http://www.forestinteractive.my/gamma/";
else if(selectedIndex == 1)
selectedLink="http://www.forestinteractive.my/gamma/";
else if(selectedIndex == 2)
selectedLink="http://www.forestinteractive.my/gamma/";
else if(selectedIndex == 3)
selectedLink="http://www.forestinteractive.my/gamma/";
else if(selectedIndex == 4)
selectedLink="http://www.forestinteractive.my/gamma/";
}
class connection implements Runnable
{
public connection()
{
Thread t = new Thread(this);
t.start();
}
public void run()
{
HttpConnection connection = null;
InputStream in = null;
try
{
connection = (HttpConnection)Connector.open(selectedLink);
connection.setRequestMethod(HttpConnection.GET);
if(connection.getResponseCode() == HttpConnection.HTTP_OK)
{
in = connection.openInputStream();
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
int ch;
int cc = 0;
while ((ch = in.read()) != -1)
{
if(ch==(int)'\n')
cc++;
bytestream.write(ch);
}
String strData = new String(bytestream.toByteArray());
bytestream.close();
in.close();
connection.close();
}
}catch(Exception e)
{
System.out.println("Exception in run()******"+e);
}
}
}
}
No comments:
Post a Comment