Como utilizar fontes redimensionáveis livremente em Java ME
Dados do artigo
Exemplo de código
Artigo
Contents |
Introdução
Nos MIDlets as fontes e as propriedades são específicas, usando o padrão da classe LCDUI javax.microedition.lcdui.Font. Uma fonte possui três propriedades: tamanho, estilo e tipo. No MIDP estas propriedades tem os seguintes valores:
- Tamanho da fonte
- SIZE_SMALL
- SIZE_MEDIUM
- SIZE_LARGE
- Estilo da fonte
- STYLE_PLAIN
- STYLE_BOLD
- STYLE_ITALIC
- STYLE_UNDERLINED
- ou uma combinação de STYLE_BOLD, STYLE_ITALIC, e STYLE_UNDERLINED
- Tipo da fonte
- FACE_SYSTEM
- FACE_MONOSPACE
- FACE_PROPORTIONAL
Ter apenas três tamanhos tem sido especialmente uma limitação e esta tem sido melhorada nas últimas Java Runtimes para S60. Agora é possível usar um novo método getFont() na classe com.nokia.mid.ui.DirectUtils:
public static Font getFont(int face, int style, int height)
tipo - um dos FACE_SYSTEM, FACE_MONOSPACE, ou FACE_PROPORTIONAL
estilo - STYLE_PLAIN, ou uma combinação de STYLE_BOLD, STYLE_ITALIC, e STYLE_UNDERLINED
tamanho - tamanho da fonte em pixels
Este melhoramento é parte da Nokia UI API 1.2, que está incluida na Java Runtime 1.3 para S60. Aqui abaixo está um código de um trabalho completo. O FontSizingMIDlet mostra um simples texto na tela, a fonte e o estilo podem ser mudados usando botões em touch screen. Pelo fato do touch screen ser usado, este MIDlet funciona apenas em dispositivos que possui uma.(por exemplo, Nokia 5800 XpressMusic e N97).
Código fonte: FontSizingMIDlet.java
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
public class FontSizingMIDlet extends MIDlet {
private FontCanvas canvas;
public void startApp() {
canvas = new FontCanvas(this);
Display.getDisplay(this).setCurrent(canvas);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
protected void showError(String title, String text) {
Alert alert = new Alert(title, text, null, AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
alert.getType().playSound(Display.getDisplay(this));
Displayable current = Display.getDisplay(this).getCurrent();
if (current instanceof Alert) {}
else Display.getDisplay(this).setCurrent(alert);
}
}
Código fonte: FontCanvas.java
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import com.nokia.mid.ui.DirectUtils;
import com.nokia.mid.ui.TactileFeedback;
public class FontCanvas extends Canvas implements CommandListener {
private FontSizingMIDlet midlet;
private Command exitCommand;
private String heightString = "";
private String fontString = "";
private Font font;
private int fontHeight = 20;
private int buttonFontHeight = 20;
private int face = 0;
private int style = 0;
private int edge = 0;
private int width;
private int height;
private TactileFeedback tactileFeedback;
private boolean touchFeedback = false;
private Button plusButton;
private Button minusButton;
private Button styleButton;
private boolean init = false;
public FontCanvas(FontSizingMIDlet midlet) {
this.midlet = midlet;
exitCommand = new Command("Exit", Command.EXIT, 1);
this.addCommand(exitCommand);
this.setCommandListener(this);
tactileFeedback = new TactileFeedback();
touchFeedback = tactileFeedback.isTouchFeedbackSupported();
}
public void paint(Graphics g) {
if (!init) {
width = getWidth();
height = getHeight();
plusButton = new Button(60, 25, "HEIGHT+");
minusButton = new Button(60, 25, "HEIGHT-");
styleButton = new Button(60, 25, "STYLE");
}
heightString = "Font height = " + fontHeight;
fontString = "Style = " + style + ", face = " + face;
g.setColor(255, 255,255);
g.fillRect(0, 0, width, height);
g.setColor(0, 0, 0);
font = DirectUtils.getFont(face, style, fontHeight);
fontHeight = font.getHeight();
g.setFont(font);
g.drawString(heightString, 0, 0, Graphics.TOP|Graphics.LEFT);
g.drawString(fontString, 0, fontHeight, Graphics.TOP|Graphics.LEFT);
g.drawString("com.nokia.mid.ui.version: " + System.getProperty("com.nokia.mid.ui.version"),
0, fontHeight*2, Graphics.TOP|Graphics.LEFT); // Should return "1.2"
edge = plusButton.drawButton(g, 10, height-70, plusButton.selected);
edge = minusButton.drawButton(g, edge+10, height-70, minusButton.selected);
edge = styleButton.drawButton(g, edge+10, height-70, styleButton.selected);
if (!init) {
plusButton.registerFeedbackArea(this, 0);
minusButton.registerFeedbackArea(this, 1);
styleButton.registerFeedbackArea(this, 2);
init = true; // initialization done
}
}
protected void keyPressed(int keyCode) {
if (keyCode == -2 || keyCode == -3) {
if (fontHeight > 0) fontHeight--;
}
else if (keyCode == -1 || keyCode == -4) fontHeight++;
repaint(0, 0, width, fontHeight*3+2);
}
protected void keyReleased(int keyCode) { }
protected void keyRepeated(int keyCode) {
if (keyCode == -2 || keyCode == -3) {
if (fontHeight > 0) fontHeight--;
}
else if (keyCode == -1 || keyCode == -4) fontHeight++;
repaint(0, 0, width, fontHeight*3+2);
}
protected void pointerDragged(int x, int y) { }
protected void pointerPressed(int x, int y) {
plusButton.selected = false;
minusButton.selected = false;
styleButton.selected = false;
if (checkButton(plusButton, x, y)) {
plusButton.selected = true;
fontHeight++;
}
else if (checkButton(minusButton, x, y)) {
minusButton.selected = true;
if (fontHeight > 0) fontHeight--;
}
else if (checkButton(styleButton, x, y)) {
styleButton.selected = true;
if (style < 7) style++;
else style = 0;
}
repaint();
}
protected void pointerReleased(int x, int y) {
plusButton.selected = false;
minusButton.selected = false;
styleButton.selected = false;
repaint();
}
protected void sizeChanged(int w, int h) {
width = w;
height = h;
repaint();
}
private boolean checkButton(Button button, int x, int y) {
boolean pressed = false;
boolean horizontal = false;
boolean vertical = false;
int x_edge = button.x + button.w;
int y_edge = button.y + button.h;
if (x > button.x && x < x_edge) horizontal = true;
if (y > button.y && y < y_edge) vertical = true;
if (horizontal && vertical) pressed = true;
return pressed;
}
public void commandAction(Command c, Displayable d) {
if (c == exitCommand) {
midlet.notifyDestroyed();
}
}
class Button {
private int x=0;
private int y=0;
private int w=0;
private int h=0;
private int size=0;
private String text="";
protected boolean selected = false;
protected Button(int h, int size, String text) {
this.h = h;
this.size = size;
this.text = text;
}
public void registerFeedbackArea(Canvas canvas, int id) {
if (touchFeedback) {
try {
tactileFeedback.registerFeedbackArea(canvas, id, x, y, w, h,
TactileFeedback.FEEDBACK_STYLE_BASIC);
}
catch (IllegalArgumentException iae) {
System.out.println("IllegalArgumentException: " + iae.getMessage());
}
}
}
public int drawButton(Graphics g, int x, int y, boolean selected) {
this.x = x;
this.y = y;
font = DirectUtils.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, size);
buttonFontHeight = font.getHeight();
g.setFont(font);
this.w = font.stringWidth(text)+ 10;
g.setColor(255, 0, 0);
g.drawRect(x, y, w, h);
if (selected) g.setColor(0, 0, 255);
g.drawRect(x-1, y-1, w+2, h+2);
g.setColor(0, 0, 0);
g.drawString(text, x+w/2, y+h/2-buttonFontHeight/2, Graphics.TOP|Graphics.HCENTER);
edge = x + w;
return edge;
}
}
}
Nos screenshots abaixo as fontes de 12, 30 and 50 pixels estão sendo usadas.
Também está disponível os arquivos FontSizingMIDlet.jad FontSizingMIDlet.jar aqui.
Aplicação de exemplo
- FontSizingMIDlet.zip contendo o FontSizingMIDlet.jad e FontSizingMIDlet.jar



(no comments yet)