How to use freely resizable font in in Java ME

本文介绍如何使用Java Runtime 1.3 for S60的Nokia UI API 1.2来调整MIDlet应用中的字体大小。通过DirectUtils.getFont()方法可以设置字体的高度、样式和类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值