This site requires JavaScript, please enable it in your browser!
Greenfoot back
tylers
tylers wrote ...

2013/2/12

add a new line depending on the amount of character (or words)

tylers tylers

2013/2/12

#
add a new line depending on the amount of character (or words) how could i make it so if you split a certain string onto different lines after a certain amount of characters( but i dont want words split in half). EG: dont want it to look like this: -------------------------------- | kkjhlgljhgljhghgjhgjhgljhgjbh,kghkhlkhlkjhlkjhlglhlkjhkljhlkh | | | | | | | -------------------------------- But like this: --------------------------------- | kjjkhjlhkjhlhgvjhdkljhglkrgh | | kjhlkukjhkjhlkjhlkjhlkjhlkjhlkj | | jkhklhljkhljkhlkjlj | | | --------------------------------- (this looks wired after i posted it ) ;/ this is the code ive already come up with:
public help(int width,int height,String text)
    {
        img = new GreenfootImage(width+1, height+1);
        img.setColor(new Color(108,170,199,124));
        img.fillRect(0,0,width,height);
        setImage(img);
        Font font = img.getFont();
        font = font.deriveFont(FONT_SIZE);
        img.setFont(font);        
        img.drawString(text,5,5);
    }
tylers tylers

2013/2/12

#
bump ;)
danpost danpost

2013/2/12

#
You do not have to 'bump' until you original post is not longer on the 'What is happening right now' page. In fact, I was working on some code to assist you. This is what I came up with:
private void parseString(String text, int maxLineLength)
{
    while(text.length()>=maxLineLength)
    {
        int index = text.substring(0, maxLineLength).lastIndexOf(" ");
        System.out.println(text.substring(0, index));
        text = text.substring(index).trim();
    }
    System.out.println(text);
}
This code does not draw each line on an image, but does contain the code to break up a text string into limited line lengths as you want.
tylers tylers

2013/2/12

#
sorry i was soooooo impatient, anyway i get an error with your code: java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.substring(String.java:1958) at help.parseString(help.java:40) at help.<init>(help.java:24) at __SHELL0$1.<init>(__SHELL0.java:10) at __SHELL0.run(__SHELL0.java:5) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at greenfoot.localdebugger.LocalDebugger$QueuedExecution.run(LocalDebugger.java:267) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:465) at greenfoot.core.Simulation.maybePause(Simulation.java:279) at greenfoot.core.Simulation.runContent(Simulation.java:210) at greenfoot.core.Simulation.run(Simulation.java:203)
danpost danpost

2013/2/12

#
Are you using words that are larger than the maximum line length?
danpost danpost

2013/2/12

#
Sorry, I now see where you specified number of characters above.
private void parseString(String text, int maxLineLength)
{
  while(text.length() >= maxLineLength)
  {
    int index = text.substring(0, maxLineLength).lastIndexOf(" ");
    if (index < 0) index = maxLineLength; // this line added
    System.out.println(text.substring(0, index));
    text = text.substring(index).trim();
  }
  System.out.println(text);
}
tylers tylers

2013/2/12

#
yes, thanks :D how could i add that to my code ( how could i use it) dont matter
You need to login to post a reply.