⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drawstring.java

📁 在eclipse环境下
💻 JAVA
字号:
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class DrawString extends MIDlet {
    private Display display;
    
    private StringCanvas sc;
    
    //被绘制的字符串
    private final static String STR = "你好, 刘斌";
    
    public DrawString() {
        super();
        sc = new StringCanvas();
    }

    protected void startApp() throws MIDletStateChangeException {
        //获得当前MIDlet的Display对象
        display = Display.getDisplay(this); 
        //设置StringCanvas对象为当前显示对象
        display.setCurrent(sc);
    }

    protected void pauseApp() {
        // TODO Auto-generated method stub

    }

    protected void destroyApp(boolean arg0) 
        throws MIDletStateChangeException {
        // TODO Auto-generated method stub

    }
    
    class StringCanvas extends Canvas {
        /**
         * 在这个方法中绘制屏幕
         */
        protected void paint(Graphics g) {
            //清除屏幕 - 背景白色
            g.setColor(0xFFFFFF);
            g.fillRect(0, 0, this.getWidth(), this.getHeight());
            //设置绘图颜色为黑色
            g.setColor(0x000000);
            
            Font font = Font.getFont(Font.FACE_MONOSPACE,
                    Font.STYLE_PLAIN, Font.SIZE_LARGE);
            g.setFont(font);
            int y=0;
            
            //定位点位于字符串绘制的影响
            g.drawString(STR, 0, 0, 
                    Graphics.LEFT | Graphics.TOP);
            
            y+=font.getHeight();
            g.drawString(STR, (getWidth()-font.stringWidth(STR))/2, 
                    y, Graphics.TOP | Graphics.LEFT);
            
            y+=font.getHeight();
            g.drawString(STR, (getWidth()-font.stringWidth(STR))/2, 
                    y, Graphics.TOP | Graphics.HCENTER);

            y+=font.getHeight();
            g.drawString(STR, (getWidth()-font.stringWidth(STR))/2, 
                    y, Graphics.TOP | Graphics.RIGHT);   
            
            //下面3个等式相等
            font = Font.getFont(Font.FACE_PROPORTIONAL,
                    Font.STYLE_BOLD, Font.SIZE_LARGE);
            g.setFont(font);
            
            y+=font.getHeight();
            int x = 20;
            g.drawString(STR, x, y, Graphics.TOP | Graphics.LEFT);
            
            y+=font.getHeight();
            g.drawString(STR, x+font.stringWidth(STR)/2, y, 
                    Graphics.TOP | Graphics.HCENTER);
            
            y+=font.getHeight();
            g.drawString(STR, x+font.stringWidth(STR), y, 
                    Graphics.TOP | Graphics.RIGHT);
 
            //下面3个等式相等
            font = Font.getFont(Font.FACE_SYSTEM,
                    Font.STYLE_UNDERLINED, 
                    Font.SIZE_LARGE);
            g.setFont(font);
            
            y+=font.getHeight();
            g.drawString(STR, x,
                    y + font.getBaselinePosition(), 
                    Graphics.BASELINE | Graphics.LEFT);
            y+=font.getHeight();
            g.drawString(STR, x+font.stringWidth(STR)/2,
                    y + font.getBaselinePosition(), 
                    Graphics.BASELINE | Graphics.HCENTER);
            y+=font.getHeight();
            g.drawString(STR, x+font.stringWidth(STR),
                    y + font.getBaselinePosition(), 
                    Graphics.BASELINE | Graphics.RIGHT);

            //下面3个等式相等
            font = Font.getFont(Font.FACE_SYSTEM,
                    Font.STYLE_ITALIC, 
                    Font.SIZE_LARGE);
            g.setFont(font);
            
            y+=font.getHeight();
            g.drawString(STR, x,
                        y + font.getHeight(), 
                        Graphics.BOTTOM | Graphics.LEFT);
            y+=font.getHeight();
            g.drawString(STR, x+font.stringWidth(STR)/2,
                        y + font.getHeight(), 
                        Graphics.BOTTOM | Graphics.HCENTER);
            y+=font.getHeight();
            g.drawString(STR, x+font.stringWidth(STR),
                        y + font.getHeight(), 
                        Graphics.BOTTOM | Graphics.RIGHT); 
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -