欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

How to use freely resizable font in in Java ME

程序员文章站 2022-03-09 21:27:56
...

Contents [hide]
1 Overview
2 Source code: FontSizingMIDlet.java
3 Source code: FontCanvas.java
4 Example application
5 See also

[edit]
Overview

In MIDlets the font and its properties are specified by using the standard LCDUI javax.microedition.lcdui.Font class. A font has three properties: size, style and face. In MIDP these properties have the following values:
Font size
SIZE_SMALL
SIZE_MEDIUM
SIZE_LARGE


Font style
STYLE_PLAIN
STYLE_BOLD
STYLE_ITALIC
STYLE_UNDERLINED
or a combination of STYLE_BOLD, STYLE_ITALIC, and STYLE_UNDERLINED


Font face
FACE_SYSTEM
FACE_MONOSPACE
FACE_PROPORTIONAL


Having only three sizes has been especially a limitation and this has been improved in the latest Java Runtimes for S60. It is now possible to use a new getFont() method in com.nokia.mid.ui.DirectUtils class:
public static Font getFont(int face, int style, int height)

face - one of FACE_SYSTEM, FACE_MONOSPACE, or FACE_PROPORTIONAL

style - STYLE_PLAIN, or a combination of STYLE_BOLD, STYLE_ITALIC, and STYLE_UNDERLINED

height - font height in pixels

This improvement is part of Nokia UI API 1.2, which is included in Java Runtime 1.3 for S60. Here is a full working sample MIDlet code below. The FontSizingMIDlet reads shows sample text on the screen and the font size and style can be changed by using the buttons on the touch screen. Because touch screen is used, this MIDlet works only in devices having one(for example, Nokia 5800 XpressMusic and N97).
[edit]
Source code: 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);
    }
}


[edit]
Source code: 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;
        }
    }
}

In the screenshots below font sizes of 12, 30 and 50 pixels have been used.

 

There are also the FontSizingMIDlet.jad and FontSizingMIDlet.jar files available here.
[edit]
Example application
FontSizingMIDlet.zip containing FontSizingMIDlet.jad and FontSizingMIDlet.jar
[edit]
See also
How to retrieve version number of Java Runtime for S60

相关标签: Java Nokia UI