0

I am making a custom Field representing an image and a label near it. I am drawing the image by graphics.drawBitmap(...)

Nw for the text, I am using graphics.drawText(String, x, y); but if the text it bigger than the display, it doesn't return to the line. I tried to add \n but that didn't solve the problem.

How can I draw a text that can expand on several lines?

Thanks,

Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
Farid Farhat
  • 2,300
  • 1
  • 16
  • 29
  • possible duplicate of [Problems with newline in Graphics2D.drawString](http://stackoverflow.com/questions/4413132/problems-with-newline-in-graphics2d-drawstring) – aioobe Oct 27 '11 at 07:37
  • @aioobe I found the solution how can I close the post? – Farid Farhat Oct 27 '11 at 11:08
  • If it was the solution I proposed in the question I linked to, (and since you haven't got any good answers) you can probably delete or close it without upsetting anyone. If the solution you found wasn't like any other existing answer here on SO I suggest you add an answer to this question yourself, and accept that. – aioobe Oct 27 '11 at 11:32
  • @Farid, you shouldn't use the "java" tag for BlackBerry questions, as it brings in the full java community which isn't aware of BlackBerry Java-ME constraints. – Michael Donohue Oct 28 '11 at 16:01

3 Answers3

3

I found a solution to the problem. You can find it below for those who are interested in this question:

public void drawMultipleLines(Graphics graphics, String text, int X, int Y, int XavailableSpace) {
    String[] texts = UiSpliter.split(text, " "); //UiSpliter is a class that will split the string into string array based on the ' ' character
    int x = X;
    int y = Y;
    for (int i = 0 ; i < texts.length ; i ++) {
        if (x + getFont().getAdvance(texts[i]) - X > XavailableSpace) {
            if (!(x == X)) {
                x = X;
                y = y + getFont().getHeight();
            }
        }
        graphics.drawText(texts[i] + " ", x, y);
        x += getFont().getAdvance(texts[i] + " ");
    }
}
Farid Farhat
  • 2,300
  • 1
  • 16
  • 29
1

As this answer says :

The drawString method does not handle new-lines, so is drawText

You'll have to split the string on new-line characters yourself and draw the lines one by one with a proper vertical offset.See that answer for details.

Community
  • 1
  • 1
COD3BOY
  • 11,964
  • 1
  • 38
  • 56
  • Check my post again I wrote the solution – Farid Farhat Oct 27 '11 at 14:47
  • 1
    @Sanjay the answer you refer to is for Java SE not for Java ME (`import java.awt.*;` clearly indicates that). Adding my -1 – gnat Oct 27 '11 at 15:02
  • @gnat I said, As this answer says : The drawString method does not handle new-lines, **SO IS** `drawText` – COD3BOY Oct 27 '11 at 15:08
  • @gnat I never said this is your answer! I juz made use of an equivalent condition to show him! – COD3BOY Oct 27 '11 at 15:13
  • @Sanjay understood - now that makes better sense. If you edit your answer I'll revoke downvote (can't do this right now - SO says my "vote is locked until answer is edited"). BTW code in answer you refer to, although is not J2ME API but pretty much illustrates the idea how to deal with that indeed – gnat Oct 27 '11 at 17:10
  • @gnat see I have another dowvote, i hate it when people downvotes and just go away without telling why they did that! – COD3BOY Oct 27 '11 at 17:20
  • @Sanjay nope I removed my down vote but its obvious that you gave me a -1 lol – Farid Farhat Oct 27 '11 at 19:46
  • lol I gave it when you mixed the question and answer :D and now removed :D – COD3BOY Oct 28 '11 at 02:17
-1

You can use AttributedCharacterIterator,TextLayout and LineBreakMeasurer classes as explained in this Java Tutorial.

deAngel
  • 108
  • 3
  • above tutorial is for Java SE, it refers to API unavailable in java ME / Blackberry API – gnat Oct 27 '11 at 08:16
  • This is unavailable for Java ME – Farid Farhat Oct 27 '11 at 11:07
  • 1
    I apologize for the wrong answer ! Looks like I've missed the blackberry and java-me tags. Maybe this example - [TextWrapUtil to draw Multiple line text in Java ME](http://www.developer.nokia.com/Community/Wiki/TextWrapUtil_to_draw_Multiple_line_text_in_Java_ME) can help you... – deAngel Oct 27 '11 at 11:20