Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (489)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (555)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [solved] Breaking lines on drawString()  (Read 3178 times)
0 Members and 1 Guest are viewing this topic.
Offline ReBirth
« Posted 2011-08-31 12:50:21 »

I'm going to make a briefing state that telling story with some words running (like typewritter) in screen. Some kind like this

Quote
September 28,

The monster has overtaken the city. Somehow, I'm still alive.

NOTE: you'll notice I quote an example from RE3  Roll Eyes

However, it seems that break line \n doesnt work on g.drawString() method. The full length text went out the screen. My current approach is create N number of string in array for every number of line.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
public void update(long delta){
      d += delta;
      if (d > 50 && !finish){
         if (!halfFinish){
            sb1.append(TEXT[level][0].charAt(textCount));
            textCount++;
            if (textCount == TEXT[level][0].length()){
               textCount = 0;
               halfFinish = true;
            }
            d = 0;
         }else{
            sb2.append(TEXT[level][1].charAt(textCount));
            textCount++;
            if (textCount == TEXT[level][1].length())
               finish = true;
            d = 0;
         }
      }
   }

   public void draw(Graphics2D g){
      g.setColor(Color.black);
      g.drawString(sb1.toString(),50,100);
      g.drawString(sb2.toString(),50,200);
   }

This is not good and I want to know your better approach for this problem. Thanks Cool

Offline loom_weaver

JGO Coder


Medals: 17



« Reply #1 - Posted 2011-08-31 13:33:32 »

At some point you'll need to call drawString for each line.

If I had to do this task, I would write some kind of code that calculates where the line breaks should be (see TextArea.java or JTextArea.java).  If the string already has them then your task is easy.

For each line, call drawString but do it in a loop instead and use FontMetrics.getHeight to calculate how far apart vertically each line should be.
Offline Cero
« Reply #2 - Posted 2011-08-31 15:34:26 »

Quote
September 28,

The monster has overtaken the city. Somehow, I'm still alive.

NOTE: you'll notice I quote an example from RE3  Roll Eyes

I approve !

Well:
1  
2  
3  
4  
5  
6  
7  
8  
public static void drawStrings(String ln[], int x, int y, Graphics g)
    {
        int h = g.getFont().getLineHeight();
        for (int i=0; i<ln.length; i++)
        {
            g.drawString(ln[i], x, y+(h*i) + h);
        }
    }


this is my usual routine. Now... you can make it, so that null Strings will be ignored. Or you could even make it so that the method can also get one string which has \n's and then splits this and passes it to this method, and so on.
obviously you can also add a padding parameter and stuff.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ReBirth
« Reply #3 - Posted 2011-09-02 03:49:07 »

@Cero
Hehe so RE fans too huh? Smiley
Your method give me idea how to solve this. I'll let the method to split the string inside and update Y value. Ofc it only work in same distance between lines. Thanks.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (10 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (27 views)
2014-08-22 19:31:30

atombrot (40 views)
2014-08-19 09:29:53

Tekkerue (38 views)
2014-08-16 06:45:27

Tekkerue (34 views)
2014-08-16 06:22:17

Tekkerue (24 views)
2014-08-16 06:20:21

Tekkerue (34 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (47 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!