`
wuhua
  • 浏览: 2095140 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

How to use freely resizable font in in Java ME

    博客分类:
  • J2ME
阅读更多

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

分享到:
评论

相关推荐

    How to use SFTP

    You should ensure that the server's public keys are loaded by the client as described in How to use SFTP (with server validation - known hosts), or you may want to switch off server validation to get ...

    Coding Games in Python

    The book teaches how to use freely available resources, such as PyGame Zero and Blender, to add animations, music, scrolling backgrounds, 3-D scenery, and other pieces of professional wizardry to ...

    How to Cheat at Configuring Open Source Security Tools

    See how to reporting on bandwidth usage and other metrics and to use data collection methods like sniffing, NetFlow, and SNMP. * Learn Defensive Monitoring Considerations See how to define your ...

    freely programmed search help in our WebDynpro component

    freely programmed search help in our WebDynpro components

    Beginning JavaScript with DOM Scripting and Ajax: Second Editon

    As well as focusing on client-side JavaScript, you will also learn how to work with the Browser Object Model, the Document Object Model (DOM), how to use XML and JSON as well as communicate with ...

    Pro WPF in C# 2010 Mar 2010

    Chapter 21: Data Views explores how you use the view in a data-bound window to navigate through a list of data items, and to apply filtering, sorting, and grouping. Chapter 22: Lists, Grids, and ...

    Guice教程(Google)

    The Google Web Toolkit (GWT) is a nifty framework that Java programmers can use to create Ajax applications. The GWT allows you to create an Ajax application in your favorite IDE, such as IntelliJ ...

    JAVA反编译软件

    3. How to use JD-GUI For example, to decompile "Object.class", you can : - execute the following command line : "jd-gui.exe Object.class". - select "Open File ..." in "File" menu and browse to "Object...

    Beginning JavaScript with DOM Scripting and Ajax (pdf + ePub)

    As well as focusing on client-side JavaScript, you will also learn how to work with the Browser Object Model, the Document Object Model (DOM), how to use XML and JSON as well as communicate with ...

    Sams.Ubuntu.Unleashed.2010.Edition.Dec.2009.pdf

    This book provides all the information that you need to get up and running with Ubuntu.It even tells you how to keep Ubuntu running in top shape and how to adapt Ubuntu to changes in your own needs....

    OReilly Java Cookbook 3rd Edition

    Each recipe includes self-contained code solutions that you can freely use, along with a discussion of how and why they work. If you are familiar with Java basics, this cookbook will bolster your ...

    IMGMIPSFPGA资料.pdf

    paving the way for your students to explore how a commercial pipelined processor core works inside and to use this core in their projects, in effect creating their own SoC designs. With its long ...

    java反编译工具

    3. How to use JD-GUI For example, to decompile "Object.class", you can : - execute the following command line : "jd-gui.exe Object.class". - select "Open File ..." in "File" menu and browse to "Object...

    Analyzing.Financial.Data.and.Implementing.Financial.Models.Using.R.3319

    This book is a comprehensive introduction to financial modeling that teaches advanced undergraduate and graduate students in finance and economics how to use R to analyze financial data and implement ...

    一款好用的Java反编译器

    3. How to use JD-GUI For example, to decompile "Object.class", you can : - execute the following command line : "jd-gui.exe Object.class". - select "Open File ..." in "File" menu and browse to "Object...

    sed-awk-2nd-edition.chm

    how to use awk's built-in functions; how to write user-defined functions; debugging techniques for awk programs; how to develop an application that processes an index, demonstrating much of the power...

    wxPython in Action (2006).pdf

    I found myself fully back in the C++ world again, although I was able to use Python for some of the build and test scripts for the project. Harri wasn’t able to spend any time on it either, so ...

    Python GUI Programming Cookbook

    If you are a Python programmer with intermediate level knowledge of GUI programming and want to learn how to create beautiful, effective, and responsive GUIs using the freely available Python GUI ...

    MATLAB远程仿真终端

    To use MATLAB freely, You just need to bring client exe file in you USB disk, whose size is only 2 megabytes. After clicking client exe file and inputing the IP address of server, you can input any ...

    The Definitive Guide to Jython-Python for the Java Platform

    Jython is freely available for both commercial and noncommercial use and is distributed with source code. Jython is complementary to Java. The Definitive Guide to Jython, written by the official ...

Global site tag (gtag.js) - Google Analytics