smartgoogle的源码重新写了一遍,这回来算有点满意. 移植的问题好像蛮难解决,关键是字体的大小和系统相关.源码贴出来大家看.准备继续完善一下,将来加上焦点事件.(自己感觉要比刚才的好)
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.io.*;
public class SmartGoogle extends MIDlet
{
private Display display;
private GoogleCanvas googlecanvas;
public SmartGoogle()
{
display=Display.getDisplay(this);
googlecanvas=new GoogleCanvas();
}
public void startApp()
{
display.setCurrent(googlecanvas);
}
public void pauseApp(){}
public void destroyApp(boolean unconditional){}
}
class GoogleCanvas extends Canvas
{
private StringElement[] s;
private ImageElement i;
private TextBoxElement t;
private CommandElement[] c;
private int focus;
public GoogleCanvas()
{
setTitle("Google - Microsoft Internet Explorer");
s=new StringElement[13];
s[0]=new StringElement("Personalized Home",true);
s[1]=new StringElement("|",false);
s[2]=new StringElement("Sign In",true);
i=new ImageElement("/logo.png");
s[3]=new StringElement("Web",false);
s[4]=new StringElement("Images",true);
s[5]=new StringElement("Group",true);
s[6]=new StringElement("News",true);
s[7]=new StringElement("More >>",true);
t=new TextBoxElement();
c=new CommandElement[2];
c[0]=new CommandElement("Google Search");
c[1]=new CommandElement("I'm Feeling Lucky");
s[8]=new StringElement("-",false);
s[9]=new StringElement("Business Solutions",true);
s[10]=new StringElement("About Google",true);
s[11]=new StringElement("Make Google Your Homepage!",true);
s[12]=new StringElement("About Google",true);
}
public void paint(Graphics g)
{
clearScreen(g);
int x;
int y;
Font f=Font.getDefaultFont();
//fisr line
x=getWidth();
y=0;
x=x-s[2].getWidth();
s[2].draw(g,x,y);
x=x-f.charWidth(' ')*2;
s[1].draw(g,x,y);
x=x-f.charWidth(' ')-s[0].getWidth();
s[0].draw(g,x,y);
//the logo
x=(getWidth()-i.getWidth())/2;
y=f.getHeight()+5;
i.draw(g,x,y);
//the third line
x=(getWidth()-s[3].getWidth()-s[4].getWidth()-s[5].getWidth()-s[6].getWidth()-s[7].getWidth()-f.charWidth(' ')*2)/2;
y=y+i.getHeight();
s[3].draw(g,x,y);
x=x+s[3].getWidth()+f.charWidth(' ');
s[4].draw(g,x,y);
x=x+s[4].getWidth()+f.charWidth(' ');
s[5].draw(g,x,y);
x=x+s[5].getWidth()+f.charWidth(' ');
s[6].draw(g,x,y);
x=x+s[6].getWidth()+f.charWidth(' ');
s[7].draw(g,x,y);
//the textbox
x=getWidth()-x-s[7].getWidth()-5;
y=y+s[7].getHeight()+5;
t.setWidth(getWidth()-x*2);
t.setHeight(20);
t.draw(g,x,y);
//commands
x=(getWidth()-c[0].getWidth()-c[1].getWidth())/2;
y=y+23;
c[0].draw(g,x,y);
x=x+c[0].getWidth()+1;
c[1].draw(g,x,y);
//the sixth line
x=(getWidth()-s[8].getWidth()*2-s[9].getWidth()-s[10].getWidth()-f.charWidth(' ')*3)/2;
y=y+c[0].getHeight()+25;
s[8].draw(g,x,y);
x=x+s[8].getWidth()+f.charWidth(' ');
s[9].draw(g,x,y);
x=x+s[9].getWidth()+f.charWidth(' ');
s[8].draw(g,x,y);
x=x+s[8].getWidth()+f.charWidth(' ');
s[10].draw(g,x,y);
//the seventh line
x=(getWidth()-s[11].getWidth())/2;
y=y+s[10].getHeight()+10;
s[11].draw(g,x,y);
//the lase line
x=(getWidth()-s[12].getWidth())/2;
y=y+s[11].getHeight()+10;
s[12].draw(g,x,y);
}
private void clearScreen(Graphics g)
{
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
}
}
//Element is the super class of all elements which can be used in GoogleCanvas;
abstract class Element
{
private boolean selectable; //not used yet
private int xpsn;
private int ypsn;
private int width;
private int height;
public Element(boolean s)
{
selectable=s;
xpsn=0;
ypsn=0;
width=0;
height=0;
}
public boolean isSelectAble(){return selectable;} //also no use
public int getX(){return xpsn;}
public int getY(){return ypsn;}
public int getWidth(){return width;}
public int getHeight(){return height;}
public void setX(int x){xpsn=x;}
public void setY(int y){ypsn=y;}
public void setWidth(int w){width=w;}
public void setHeight(int h){height=h;}
public abstract void draw(Graphics g,int x,int y);
}
class StringElement extends Element
{
private String string;
private int style;
public static final int PLAN=0;
public static final int HYPERLINK=1;
public StringElement(String s,boolean slectable)
{
super(slectable);
string=s;
if(slectable) style=HYPERLINK;
else style=PLAN;
setWidth(Font.getDefaultFont().stringWidth(s));
setHeight(Font.getDefaultFont().getHeight());
}
public void draw(Graphics g,int x,int y)
{
setX(x);
setY(y);
switch(style)
{
case PLAN:
style=PLAN;
g.setColor(0,0,0);
g.drawString(string,x,y,Graphics.LEFT|Graphics.TOP);
break;
case HYPERLINK:
style=HYPERLINK;
g.setColor(0,0,255);
g.drawString(string,x,y,Graphics.LEFT|Graphics.TOP);
g.drawLine(x,y+Font.getDefaultFont().getHeight()-1,x+Font.getDefaultFont().stringWidth(string)-1,y+Font.getDefaultFont().getHeight()-1);
break;
}
}
}
class ImageElement extends Element
{
private Image image;
public ImageElement(String s)
{
super(false);
try
{
image=Image.createImage(s);
}
catch(IOException e){}
setWidth(image.getWidth());
setHeight(image.getHeight());
}
public void draw(Graphics g,int x,int y)
{
setX(x);
setY(y);
g.drawImage(image,x,y,Graphics.LEFT|Graphics.TOP);
}
}
class CommandElement extends Element
{
private String label;
public CommandElement(String s)
{
super(true);
label=s;
setWidth(Font.getDefaultFont().stringWidth(s)+10);
setHeight(Font.getDefaultFont().getHeight()+4);
}
public void draw(Graphics g,int x,int y)
{
setX(x);
setY(y);
int w=getWidth();
int h=getHeight();
g.setColor(0,0,0);
g.setGrayScale(190);
g.fillRect(x,y,w,h);
g.setGrayScale(0);
g.drawLine(x,y+h,x+w,y+h);
g.drawLine(x+w,y,x+w,y+h);
g.drawString(label,x+5,y+2,Graphics.LEFT|Graphics.TOP);
}
}
class TextBoxElement extends Element
{
public TextBoxElement()
{
super(true);
}
public void draw(Graphics g,int x,int y)
{
setX(x);
setY(y);
int w=getWidth();
int h=getHeight();
g.setColor(0,0,0);
g.drawLine(x,y,x,y+h);
g.drawLine(x,y,x+w,y);
g.setGrayScale(200);
g.drawLine(x,y+h,x+w,y+h);
g.drawLine(x+w,y,x+w,y+h);
}
}
下面贴张画看看