如何在J2ME中将文本写入矩形

时间:2012-02-17 10:13:12

标签: java-me

我有一个图像,我在上面绘制一个矩形。有没有办法写入那个矩形?

2 个答案:

答案 0 :(得分:0)

您可以测量所有位置的图像和文字。请参阅 Link. 你会对此有更好的认识。

答案 1 :(得分:-1)

package animationMaker;
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;

import java.lang.*;
import java.io.*;
import java.util.*;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.*;
import javax.microedition.media.*;
import javax.microedition.m2g.ScalableGraphics;
import javax.microedition.m2g.ScalableImage;

//A first MIDlet with simple text and a few commands.
public class Connect extends Canvas implements CommandListener {

    //The exit commands
    private Command exitCommand;
    private Command connectCommand;
    private Command startCommand;
    private mainMenuMidlet midlet;
    //The display for this MIDlet
    private Display display;
    Form displayForm;
    private ScalableImage background;
    private mainCanvas mainCanvas;
    private TextBox textBox;

    public Connect(mainMenuMidlet m, Display d, ScalableImage background) {
        //display = Display.getDisplay(this);
        this.background= background;
        midlet=m;
        display= d;
        exitCommand = new Command("Exit", Command.SCREEN, 0);
        connectCommand = new Command("OK", Command.OK, 1);
        startCommand = new Command("Confirm", Command.OK, 1);
    }
    public Connect(mainMenuMidlet m, Display d, ScalableImage background, mainCanvas mainCanvas) {
        mainCanvas = this.mainCanvas;
        //display = Display.getDisplay(this);
        this.background= background;
        midlet=m;
        display= d;
        exitCommand = new Command("Exit", Command.SCREEN, 0);
        connectCommand = new Command("OK", Command.OK, 1);
        startCommand = new Command("Confirm", Command.OK, 1);
    }

    public void startApp() {
    }

    public void paint(Graphics g) {
        clearScreen(g);
        textBox = new TextBox("Please enter your message", "Text: ", 256, 0);
        textBox.addCommand(connectCommand);
        textBox.setCommandListener(this);
        display.setCurrent(textBox);

        displayForm = new Form("Welcome to Text Displayer");
        displayForm.addCommand(exitCommand);
        displayForm.setCommandListener(this);
        displayForm.append(showResults(textBox.getString().substring(textBox.getString().indexOf("Name:") +5).trim()));
    }


    public void pauseApp() {
    }

    // Destroy must cleanup everything not handled
    // by the garbage collector.
    // In this case there is nothing to cleanup.
    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable s) {
        if (c == exitCommand) {
            destroyApp(true);
            //notifyDestroyed();
        } else if(c == connectCommand) {
            try{

                Image       originalImage = Image.createImage("/picture/background2.png");
                //originalImage = Image.createImage(80,20);
                System.out.println(originalImage.toString()+" originalImage123");
                String text= textBox.getString().substring(textBox.getString().indexOf("Text:") +5).trim();
                //   MutableImage mi = new MutableImage(midlet,display,background,text);
                mainCanvas= new mainCanvas(midlet,display,background,text,originalImage);
                //  mainCanvas.append("Welcome, " + textBox.getString().substring(textBox.getString().indexOf("Text:") +5).trim());

                //  mainCanvas.setTitle(textBox.getString().substring(textBox.getString().indexOf("Text:") +5).trim());
                // mainCanvas.displayForm.append(textBox.getString().substring(textBox.getString().indexOf("Text:") +5).trim());
                display.setCurrent(mainCanvas);
            }catch(IOException e){
                e.printStackTrace();
            }

        } else if(c == startCommand) {
            displayForm.append(showResults(textBox.getString().substring(textBox.getString().indexOf("Text:") +5).trim()));
            display.setCurrent(displayForm);
        }
    }


    String showResults(String string) {
        String result = "";
        try {
        } catch (Exception exc) {
            exc.printStackTrace();
        }
        return result;
    }
    private void clearScreen(Graphics g) {
        g.setColor(0xFFFFFF);
        g.fillRect(0,0,getWidth(),getHeight());
    }
}

此连接代码只是执行文本输入操作。  用户将被带到此页面并输入他的文本