// This midlet is designed to align drawn text within the application area // using anchor points import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class TextMove extends MIDlet { private Display display; // a display private TextCanvas canvas; // canvas to display text private AnchorPtList anchorPt; // list to query anchor points private int anchorPoint = Graphics.BASELINE | Graphics.HCENTER; // constructor public TextMove(){ display = Display.getDisplay(this); canvas = new TextCanvas(this); anchorPt = new AnchorPtList("Anchor Points", List.IMPLICIT, this); } // startApp protected void startApp(){ showCanvas(); } // pauseApp protected void pauseApp(){ } // destroyApp protected void destroyApp(boolean unconditional){ } // showCanvas class public void showCanvas(){ display.setCurrent(canvas); } // showList class to show the implicit list public void showList(){ display.setCurrent(anchorPt); } public int getAnchorPoint(){ return anchorPoint; } public void setAnchorPoint(int anchorPoint){ this.anchorPoint = anchorPoint; } public void exitMIDlet(){ destroyApp(true); notifyDestroyed(); } } // TextArea class -- Draw text at a specified place(anchor point) class TextCanvas extends Canvas implements CommandListener{ private Command cmExit; //exit midlet private Command cmAnchrPt; // Command to query anchor point private TextMove midlet; // contructor public TextCanvas(TextMove midlet){ this.midlet = midlet; // Create the commands and listen for events cmExit = new Command("Exit", Command.EXIT, 1); cmAnchrPt = new Command("Anchors", Command.SCREEN, 2); addCommand(cmExit); addCommand(cmAnchrPt); setCommandListener(this); } // Draw a text protected void paint(Graphics g){ int xcent = getWidth()/2; // get the x value of centre of the screen int ycent = getHeight()/2; // get the y value of centre of the screen // set a font g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_ITALIC, Font.SIZE_MEDIUM)); // draw a line at the center of display (it is dot) g.drawLine(xcent, ycent, xcent, ycent); // the dot will be static and the text will move g.drawString("Hello Mate!", xcent, ycent, midlet.getAnchorPoint()); } // Show Anchor point or alternatively exit midlet public void commandAction(Command c, Displayable d){ if( c == cmAnchrPt) midlet.showList(); else if (c == cmExit){ midlet.exitMIDlet(); } } } // class AnchorPtList --- alist to query anchor points class AnchorPtList extends List implements CommandListener{ private TextMove midlet; // contructor public AnchorPtList(String title, int ListType, TextMove midlet){ // call list constructor super(title, ListType); this.midlet = midlet; // Append list members append("TOP/LEFT", null); append("TOP/HCENTER", null); append("TOP/RIGHT", null); append("BASELINE/LEFT", null); append("BASELINE/HCENTER", null); append("BASELINE/RIGHT", null); append("BOTTOM/LEFT", null); append("BOTTOM/HCENTER", null); append("BOTTOM/RIGHT", null); setCommandListener(this); } // define commands to set anchor points public void commandAction(Command c, Displayable s){ switch(getSelectedIndex()) { case 0: midlet.setAnchorPoint(Graphics.TOP | Graphics.LEFT); break; case 1: midlet.setAnchorPoint(Graphics.TOP | Graphics.HCENTER); break; case 2: midlet.setAnchorPoint(Graphics.TOP | Graphics.RIGHT); break; case 3: midlet.setAnchorPoint(Graphics.BASELINE | Graphics.LEFT); break; case 4: midlet.setAnchorPoint(Graphics.BASELINE | Graphics.HCENTER); break; case 5: midlet.setAnchorPoint(Graphics.BASELINE | Graphics.RIGHT); break; case 6: midlet.setAnchorPoint(Graphics.BOTTOM | Graphics.LEFT); break; case 7: midlet.setAnchorPoint(Graphics.BOTTOM | Graphics.HCENTER); break; case 8: midlet.setAnchorPoint(Graphics.BOTTOM | Graphics.RIGHT); break; default: midlet.setAnchorPoint(Graphics.BASELINE | Graphics.HCENTER); } midlet.showCanvas(); } }