Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (511)
Games in Android Showcase (119)
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  
  String formatting problem  (Read 906 times)
0 Members and 1 Guest are viewing this topic.
Offline Coinerson

Junior Duke




Introducing the world's cutest zombie, Timmy


« Posted 2006-11-01 00:15:13 »

static final String newLine = System.getProperty("line.separator");

public static void setText(String t)   {
        System.out.println(t);
        text = t.replaceAll("\n", newLine);
        System.out.println(text);
 }
Both outputs are: *Turtle sound*\n\nI can ferry you across the ocean. Also lava because my programmer\nhas an odd opinion about turtles!

Loaded from a text file if that means anything.

Offline kappa
« League of Dukes »

JGO Kernel


Medals: 78
Projects: 15


★★★★★


« Reply #1 - Posted 2006-11-01 01:19:26 »

hmm could you not just use

1  
2  
3  
4  
5  
public static void setText(String t)   {
        System.out.println(t);
        text = t.replaceAll("\n", " ");
        System.out.println(text);
 }
Offline Coinerson

Junior Duke




Introducing the world's cutest zombie, Timmy


« Reply #2 - Posted 2006-11-01 01:55:56 »

That also does nothing.

However this
public static void setText(String t)   {
        System.out.println(t);
        text = t.replaceAll("+", newLine);
        System.out.println(text);
    }

Gives me neat errors...

java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0
+
^
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ryanm

Senior Duke


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #3 - Posted 2006-11-01 11:55:19 »


java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0
+
^

That's because the "+" character is important in regex ( it means "The preceding character group, one or more times" ). If you want to replace all instances of the "+" character, you must escape it with a "\" so that it loses it's magical regex properties. So try
1  
text = t.replaceAll( "\\+", newLine );


edit: temporarily forgot that you have to escape the escape.
Offline Coinerson

Junior Duke




Introducing the world's cutest zombie, Timmy


« Reply #4 - Posted 2006-11-02 08:14:54 »

Turns out im going to use fontmetrics and lots of math, since the current system makes whoever wrote the map in charge of getting the text to fit. Bad plan.



So here is the codes

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  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
import java.awt.*;
import java.util.Iterator;
import java.awt.image.BufferedImage;

public class AlertWindow {
    public static final int PORTRAIT_X_OFFSET = 30;
    public static final int PORTRAIT_Y_OFFSET = 22;
   
    public static final int PORTRAIT_STRING_X_OFFSET = 74;
    public static final int NORMAL_STRING_X_OFFSET = 30;
   
    public static final int START_STRING_Y_OFFSET = 30;
   
    public static final int STRING_OFFSET_BETWEEN_LINES = 8;
   
    public static final int FONT_SIZE = 12;
   
    private static String text;
    private static BufferedImage portrait;
    private static BufferedImage image;
    public static boolean drawing;
   
    static int x, y;
   
   
    public static void setImage(BufferedImage i)   {
        image = i;
        x = (RunnerApp.WIDTH/2) - (getWidth()/2);
        y = (RunnerApp.HEIGHT/2) - (getHeight()/2);
    }
   
    public static BufferedImage getImage()  {
        return image;
    }
   
    public static void setPortrait(BufferedImage np)   {
        portrait = np;
    }
   
    public static BufferedImage getPortrait()  {
        return portrait;
    }
   
    public static String getText()    {
        return text;
    }
   
    public static void setText(String t)   {
        text = t;
    }
   
   
    public static int getHeight()  {
        if (image == null)   {
            return 0;
        }   else    {
            return image.getHeight();
        }
    }
   
    public static int getWidth()  {
        if (image == null)   {
            return 0;
        }   else    {
            return image.getWidth();
        }
    }
   
    public static void draw(Graphics g)  {
        if (!drawing)   return;
       
        //g.drawImage(image, x, y, null);
        g.drawImage(portrait, x + PORTRAIT_X_OFFSET, y + PORTRAIT_Y_OFFSET, null);
       
        Font font = new Font("Helvetica Neue", Font.PLAIN, FONT_SIZE);
        g.setFont(font);
        g.setColor(Color.WHITE);
        FontMetrics metrics = g.getFontMetrics(font);
       
        drawTheText(g, 1, metrics);
    }
   
    public static void drawTheText(Graphics g, int lineNum, FontMetrics metrics)   {
        int boxWidth = (RunnerApp.WIDTH/2) + (getWidth()/2);
       
        int width = metrics.stringWidth(getText());
        int height = metrics.getHeight();
       
       
       
        if (NORMAL_STRING_X_OFFSET + width >= boxWidth)   {
            g.drawString(frontHalfString(getText()), x + NORMAL_STRING_X_OFFSET, y + START_STRING_Y_OFFSET + (STRING_OFFSET_BETWEEN_LINES * lineNum) + (height * lineNum));
            setText(secondHalfString(getText()));
            drawTheText(g, ++lineNum, metrics);
           
        }   else    {
            g.drawString(getText(), x + NORMAL_STRING_X_OFFSET, y + START_STRING_Y_OFFSET + (STRING_OFFSET_BETWEEN_LINES * lineNum) + (height * lineNum));
        }
       
        return;
       
    }
   
    public static String frontHalfString(String string)    {
        return (string.substring(0, string.length()/2));
    }
   
    public static String secondHalfString(String string)    {
        return string.substring(string.length()/2, string.length());
    }
}


Maybe im just really tired since its 2 in the morning. But this just draws the last segment of the string. It draws the first two which are quickly covered up or something. I think the game world is being drawn over it repeatedly. I noted out that other thing to see if the text ws under it for some reason. Im going ot bed. Anything obvious?
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.

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

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

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

lcass (36 views)
2014-10-15 16:18:58

TehJavaDev (66 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (55 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
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!