Results 1 to 2 of 2
  1. #1
    Basit781 is offline Member
    Join Date
    May 2010
    Posts
    47
    Rep Power
    0

    Unhappy Getting StringIndexOutOfBoundsException while using textWrap code

    Hi to all,
    Hope you all will be fine. Actually i want to create custom look and feel for my app. i don't want to use LWUIT because it increases your app size. I am reading a book in which i found word wrapping code but when i run this code i am getting above mentioned error on second time when while loop runs. Here is the code. Please tell me why i am getting this error .

    Java Code:
    public class WordWrapCanvas extends Canvas {
        
        private final Display mDisplay;
        private Font mFont;
        private String mString = 
          "The family of Dashwood had long been " +
          "settled in Sussex. Their estate was large, " +
          "and their residence was at Norland Park, in " + 
          "the centre of their property, where, for " +
          "many generations, they had lived in so " +
          "respectable a manner as to engage the general " +
          "good opinion of their surrounding acquaintance.";
        
        
        // constructor
        public WordWrapCanvas(Display mDisplay) {
            
            this.mDisplay = mDisplay;
            
        } //end of constructor
        
        public void paint(Graphics g) {
            
            int displayWidth = getWidth();  //240
            int displayHeight = getHeight(); // 291
            
           // System.out.println("displayWidth: " + displayWidth);
           // System.out.println("displayHeight: " + displayHeight);
            
            
            g.setColor(0xffffff);
            g.fillRect(0, 0, displayWidth, displayHeight);
            
            int x = displayWidth / 8;   // 240 / 8 = 30
            int y = displayHeight / 8;  // 291 / 8 = 36
            
            //System.out.println("x = displayWidth / 8: " + x);
            //System.out.println("y = displayHeight / 8: " + y);
    
            int wrapWidth = displayWidth * 3 / 4;   // 240 * 3 / 4 = 180;
            //System.out.println("wrapWidth = displayWidth * 3 / 4: " + wrapWidth);
    
            int anchor = Graphics.TOP | Graphics.LEFT;
    
            if (mFont == null) {
    
                int[] size = {Font.SIZE_LARGE, Font.SIZE_MEDIUM, Font.SIZE_SMALL};
    
                for (int i=0; i<size.length; i++) {
    
                    mFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, size[i]);
    
                    g.setFont(mFont);
    
                    int wrapHeight = getWrapHeight(g, mString, x, y, wrapWidth, anchor);
    
                    if (wrapHeight < displayHeight - y) {
    
                        break;
    
                    }
    
                } //end of for (int i=0
    
            } //end of if (mFont == null)
    
            g.setFont(mFont);
            g.setColor(0x770077);
    
            int wrapHeight = wrap(g, mString, x, y, wrapWidth, anchor);
    
            g.setColor(0xff0000);
            g.drawRect(x - 1, y, wrapWidth, wrapHeight);
    
        }  //end of paint()
    
        public int getWrapHeight(Graphics g, String s, int x, int y, int width, int anchor) {
    
            return wrapImplementation(g, s, x, y, width, anchor, false);
    
        } //end of getWrapHeight()
    
        private int wrap(Graphics g, String s, int x, int y, int width, int anchor) {
    
            return wrapImplementation(g, s, x, y, width, anchor, true) ;
    
        } //end of wrap()
        
        private int wrapImplementation(Graphics g, String s, int x, int y, int width, int anchor, boolean draw) {
            
            
            // Gets the current form
            Font f = g.getFont();
    
            int oldY = y; // 36
    
            boolean trucking = true;
            int i = 0;
            int length = 0;
            int space = -1;
    
            while (trucking) {
    
                boolean write = false;
    
                if (i >= s.length()) {
    
                    trucking = false;
    
                } else if (s.charAt(i) == ' ') {   // Here i am getting error when while runs second time
    
                    i++;
    
                }else {
    
                    int pw = f.substringWidth(s, i, length);
                    System.out.println("f.substringWidth(s, i, length): " + pw);
    
                    if (pw < width) {
    
                        if (space > 0) {
    
                            length = space;
    
                        } else {
    
                            length--;
    
                        }
    
                        write = true;
    
                    } else if (i + length >= s.length()) {
    
                        write = true;
                        trucking = false;
    
                    } else {
    
                        if (s.charAt(i + length) == ' ') { 
    
                            length++;
    
                        } // end of if (s.charAt(i + length) == ' ')
    
                    }
    
                }
    
                if (write) {
    
                    if (draw) {
    
                        g.drawSubstring(s, i, length, x, y, anchor);
    
                    } //end of if (draw)
    
                    i += length;
                    y += f.getHeight();
                    length = 0;
                    space = -1;
    
                } //end of if (write)
    
            } //end of while
    
            
            return y - oldY;
            
        } //end of  wrapImplementation()
    
    } //end of class WordWrapCanvas
    Thanks
    Last edited by Basit781; 06-27-2011 at 10:08 AM.

  2. #2
    Basit781 is offline Member
    Join Date
    May 2010
    Posts
    47
    Rep Power
    0

Similar Threads

  1. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  2. StringIndexOutOfBoundsException- Crossword Solver App.
    By numbskull20 in forum Advanced Java
    Replies: 3
    Last Post: 12-01-2010, 01:33 PM
  3. StringIndexOutOfBoundsException Error
    By Mayur in forum New To Java
    Replies: 4
    Last Post: 02-01-2009, 05:10 PM
  4. StringIndexOutOfBoundsException
    By ravian in forum New To Java
    Replies: 2
    Last Post: 01-29-2008, 11:25 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •