Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (516)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (577)
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  
  special character problem  (Read 1118 times)
0 Members and 1 Guest are viewing this topic.
Offline TitusBH85

Senior Newbie




Java games rock!


« Posted 2003-03-25 13:11:30 »

I found out that the drawString method cannot paint special characters like \n, \t and so on.

What can i do to overcome this? Huh
Offline Herkules

Senior Duke




Friendly fire isn't friendly!


« Reply #1 - Posted 2003-03-25 13:16:35 »

tokenize the string, calc. character height, do multiple drawString()?

HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Offline snak

Senior Newbie




Eu não falo o português


« Reply #2 - Posted 2003-03-25 13:50:18 »

Have you found a nice way to compute the size of a string?  I tried to once, it was nasty.  You have to walk a long road - font, font metrics, etc.  Has anyone found a nice way to do that?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Backmask

Junior Duke




586: The average IQ needed to understand a PC


« Reply #3 - Posted 2003-03-26 05:37:33 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public static Dimension getStringLength(String str, Font font){
   FontMetrics metrics = /*your game frame*/.getFontMetrics(font);            
   Dimension textSize = new Dimension();
   textSize.width = metrics.stringWidth(str) + 2 * metrics.charWidth(' ');
    textSize.height += metrics.getAscent() + metrics.getDescent() + 1;
    return textSize;
}
......
Dim d = getStringLength("Hello world", new Font("Times new roman", FONT.BOLD, 24);
System.out.println("string width in pixels: " + d.width);
System.out.println("string height in pixels: " + d.height);


And below a small help class to wrap a string to a specified width.

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  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
package org.backmask.util.font;

import java.awt.Font;
import java.awt.FontMetrics;
import java.util.StringTokenizer;
import java.util.Vector;

public class FontWrapManager {

      private String[] data = null;
      private StringTokenizer tok = null;
      private int lineNum = 0;

      public FontWrapManager(String[] text) {
            this.data = text;
            this.lineNum = 0;
            this.tok = new StringTokenizer(this.data[lineNum], " \t\n\r", true);
      }

      public FontWrapManager(String text) {
            this.data = new String[1];
            this.data[0] = text;
            this.lineNum = 0;
            this.tok = new StringTokenizer(this.data[lineNum], " \t\n\r", true);
      }

      public String[] wrapForWidth(int width, FontMetrics metrics) {
            Vector lines = new Vector();

            String word = "";
            String line = "";
            while (true){
                  word = nextToken();
                 
                  //end of data array
                  if (word == null){
                        System.out.println("word = null: adding line: " + line);
                        lines.add(line);
                        break;
                  }
                       
                  if (word.equals("\n")) {
                        lines.add(line);
                        line = "";
                  }
     
                  if (metrics.stringWidth(line + " " + word) > (width - 2)){
                        lines.add(line);
                        line = word;
                  } else {
                        line = line + " " + word;
                  }
            }

          String[] newData = new String[lines.size()];
          for (int i=0; i<lines.size(); i++)
                  newData[i] = (String) lines.elementAt(i);

          return newData;
      }

      /**
       * This method is used to tokenize the text in the array.  It lets
       * us treat the data in the array as a Stream.
       *
       * @return The next token of data.  This includes whitespace, so the
       *         caller can look for repeated newlines.
       */

      private String nextToken(){

            while (!this.tok.hasMoreTokens()) {
                  if (lineNum == (this.data.length - 1))
                        return null;
                  this.tok =      new StringTokenizer(this.data[++lineNum], " \t\n\r", true);
            }

            return tok.nextToken();

      }

}
.....................................
//wrap the story text
FontWrapManager fw = new FontWrapManager("This is a very long text line that need to be wrap due to the rectangle where the text should be painted on has only a width of 40px");
String[] wrap = fw.wrapForWidth(40, frame.getFontMetrics(font));

for (int i=0; i<wrap.length; i++)
   g.drawString(wrap[i], x, y + font_height + (font_height + 1)*i);


Hope it helps.

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.

TehJavaDev (33 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (41 views)
2014-10-26 19:37:11

Luminem (22 views)
2014-10-26 10:17:50

Luminem (27 views)
2014-10-26 10:14:04

theagentd (33 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (58 views)
2014-10-16 15:22:06

Norakomi (47 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!