001: /*
002: *
003: * Copyright (c) 2007, Sun Microsystems, Inc.
004: *
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * * Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * * Neither the name of Sun Microsystems nor the names of its contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032: package example.fonts;
033:
034: import javax.microedition.lcdui.Canvas;
035: import javax.microedition.lcdui.Command;
036: import javax.microedition.lcdui.CommandListener;
037: import javax.microedition.lcdui.Display;
038: import javax.microedition.lcdui.Displayable;
039: import javax.microedition.lcdui.Font;
040: import javax.microedition.lcdui.Graphics;
041:
042: //import java.io.*;
043: //import javax.microedition.midlet.*;
044: //import javax.microedition.lcdui.*;
045: import javax.microedition.midlet.MIDlet;
046: import javax.microedition.midlet.MIDletStateChangeException;
047:
048: /**
049: * FontTestlet is simple MIDlet which attempts to display
050: * text in all of the MIDP's different fonts.
051: */
052: public class FontTestlet extends MIDlet implements CommandListener {
053: private Display myDisplay;
054: private FontCanvas myCanvas;
055: private int currentFace = Font.FACE_SYSTEM;
056: private Command monospaceCommand = new Command("monospace",
057: Command.ITEM, 1);
058: private Command proportionalCommand = new Command("proportional",
059: Command.ITEM, 1);
060: private Command systemCommand = new Command("system", Command.ITEM,
061: 1);
062: private Command exit = new Command("Exit", Command.EXIT, 3);
063:
064: /**
065: * FontTestlet - default constructor
066: */
067: public FontTestlet() {
068: super ();
069:
070: // Set up the user interface
071: myDisplay = Display.getDisplay(this );
072: myCanvas = new FontCanvas(this ); // pointer to myself
073: myCanvas.setCommandListener(this );
074: myCanvas.addCommand(monospaceCommand);
075: myCanvas.addCommand(proportionalCommand);
076: myCanvas.addCommand(exit);
077: }
078:
079: /**
080: * initApp()
081: */
082: public void init() throws MIDletStateChangeException {
083: }
084:
085: /**
086: * startApp()
087: */
088: public void startApp() throws MIDletStateChangeException {
089: myDisplay.setCurrent(myCanvas);
090: }
091:
092: /**
093: * pauseApp()
094: */
095: public void pauseApp() {
096: // System.out.println("pauseApp()");
097: }
098:
099: /**
100: * destryApp()
101: *
102: * This is important. It closes the app's RecordStore
103: * @param cond true if this is an unconditional destroy
104: * false if it is not
105: * currently ignored and treated as true
106: */
107: public void destroyApp(boolean cond) {
108: myDisplay.setCurrent((Displayable) null);
109: myCanvas.destroy();
110: notifyDestroyed();
111: }
112:
113: /**
114: * draw some stuff to the graphics context
115: */
116: public void paint(Graphics g) {
117: String title;
118: int height = 0;
119:
120: g.setColor(0x00000000);
121: g.fillRect(0, 0, myCanvas.getWidth(), myCanvas.getHeight());
122:
123: g.setColor(0x00ffffff);
124:
125: switch (currentFace) {
126: case Font.FACE_SYSTEM:
127: title = "System";
128:
129: break;
130:
131: case Font.FACE_PROPORTIONAL:
132: title = "Proportional";
133:
134: break;
135:
136: case Font.FACE_MONOSPACE:
137: title = "Monospaced";
138:
139: break;
140:
141: default:
142: title = "unknown";
143:
144: break;
145: }
146:
147: g.drawString(title, 0, 0, Graphics.TOP | Graphics.LEFT);
148: height += g.getFont().getHeight();
149:
150: g.setFont(Font.getFont(currentFace, Font.STYLE_PLAIN,
151: Font.SIZE_LARGE));
152: g.drawString("Regular plain", 0, height, Graphics.TOP
153: | Graphics.LEFT);
154: height += g.getFont().getHeight();
155:
156: g.setFont(Font.getFont(currentFace, Font.STYLE_ITALIC,
157: Font.SIZE_LARGE));
158: g.drawString("Regular ital", 0, height, Graphics.TOP
159: | Graphics.LEFT);
160: height += g.getFont().getHeight();
161:
162: g.setFont(Font.getFont(currentFace, Font.STYLE_BOLD,
163: Font.SIZE_LARGE));
164: g.drawString("Bold plain", 0, height, Graphics.TOP
165: | Graphics.LEFT);
166: height += g.getFont().getHeight();
167:
168: g.setFont(Font.getFont(currentFace, Font.STYLE_BOLD
169: | Font.STYLE_ITALIC, Font.SIZE_LARGE));
170: g.drawString("Bold ital", 0, height, Graphics.TOP
171: | Graphics.LEFT);
172: }
173:
174: Command getCurrentCommand() {
175: switch (currentFace) {
176: case Font.FACE_MONOSPACE:
177: return monospaceCommand;
178:
179: case Font.FACE_PROPORTIONAL:
180: return proportionalCommand;
181:
182: case Font.FACE_SYSTEM:
183: default:
184: return systemCommand;
185: }
186: }
187:
188: public void commandAction(Command cmd, Displayable disp) {
189: myCanvas.addCommand(getCurrentCommand());
190:
191: if (cmd == monospaceCommand) {
192: myCanvas.removeCommand(monospaceCommand);
193: currentFace = Font.FACE_MONOSPACE;
194: } else if (cmd == proportionalCommand) {
195: myCanvas.removeCommand(proportionalCommand);
196: currentFace = Font.FACE_PROPORTIONAL;
197: } else if (cmd == systemCommand) {
198: myCanvas.removeCommand(systemCommand);
199: currentFace = Font.FACE_SYSTEM;
200: } else if (cmd == exit) {
201: destroyApp(true);
202: }
203:
204: myCanvas.repaint();
205: }
206: }
|