Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Problems with my Portholes... (String splitting and adding to lines)

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Problems with my Portholes... (String splitting and adding to lines)

    Hello there!

    I seem to have encountered into a problem which I cannot solve, or understand for that matter.
    What I'm trying to do is that I want to paint a rectangle on the screen, and then paint strings as lines onto it.
    Something like this:

    250yearsbeforevs0.jpg

    How it would work:
    In the constructor I would have one String parameter, which then would be split into words. These words would then be added to a line if it fits. When the line is full, it would continue to the next line and repeat until all words have been used.

    My code:

    Basic Window

    package Graph;
     
    import java.awt.Color;
    import java.awt.Graphics;
     
    public class Porthole {
     
        protected static Color frame = new Color(59, 60, 54), back = new Color(27, 77, 62, 150);
        protected static int SPACE = 4;
        protected int x, y, width, height;
        protected int ix, iy, iw, ih;
        protected int cx, cy, cw, ch;
        protected boolean visible;
     
        public Porthole(int x, int y, int width, int height) {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
     
            ix = x + SPACE;
            iy = y + SPACE;
            iw = width - 2 * SPACE;
            ih = height - 2 * SPACE;
     
            cx = ix + SPACE;
            cy = iy + SPACE;
            cw = iw - 2 * SPACE;
            ch = ih - 2 * SPACE;
     
            visible = false;
        }
     
        public void drawBasic(Graphics g) {
            g.setColor(back);
            g.fillRect(x, y, width, height);
     
            g.setColor(frame);
            g.drawRect(x, y, width, height);
            g.drawRect(ix, iy, iw, ih);
        }
     
        public void drawContentBox(Graphics g){
            g.setColor(new Color(120,120,120,100));
            g.fillRect(cx, cy, cw, ch);
        }
     
        public void draw(Graphics g) {
            if (visible) {
                drawBasic(g);
                drawContentBox(g);
            }
        }
     
        public void setVisible(boolean a) {
            visible = a;
        }
     
        public void move(int x, int y) {
            this.x = x;
            this.y = y;
            ix = x - 2 * SPACE;
            iy = y - 2 * SPACE;
        }
    }

    Text Window

    package Graph;
     
    import java.awt.Graphics;
     
    public class Porthole_Text extends Porthole {
     
        protected static int tSize = 12;
        protected String text;
        protected String[] words;
        protected String[] lines;
     
        public Porthole_Text(int x, int y, int width, int height, String text) {
            super(x, y, width, height);
            lines = new String[text.length()];
            this.text = text;
            words = text.split(" ");
            String t = "";
            int id = 0;
     
     
            for (int n = 0; n < lines.length; n++) {
                while (t.isEmpty() || (t.length() + words[id].length() + 1) * tSize <= cw) {
                    t += words[id];
                    t += " ";
                    System.out.println(words[id]);
                    id++;
                }
                lines[n] += t;
                System.out.println(t);
                t = "";
            }
     
        }
     
        @Override
        public void draw(Graphics g) {
            if (visible) {
                drawBasic(g);
            }
        }
    }

    Applet

    package Graph;
     
    import java.applet.Applet;
    import java.awt.Graphics;
    import java.awt.Image;
     
    public class Appletir extends Applet implements Runnable {
     
        Image i;
        Porthole a;
        Porthole_Text b;
     
        @Override
        public void init(){
            setSize(800,600);
            a = new Porthole(10, 10, 200, 100);
            a.setVisible(true);
            b = new Porthole_Text(0,0,200,200,"Sticks and stones may break my bones, but words can never hurt me.");
        }
     
        @Override
        public void paint(Graphics g){
            a.draw(g);
        }
     
        @Override
        public void start(){
            new Thread(this).start();
        }
     
        @Override
        public void run() {
            while (true){
                repaint();
                try {
                    Thread.sleep(10);
                } catch (Exception e) {
                }
            }
        }
     
        @Override
        public void update(Graphics g) {
            if (i == null || i.getWidth(this) != getWidth() || i.getHeight(this) != getHeight()) {
                i = createImage(getWidth(), getHeight());
            }
            i.getGraphics().clearRect(0, 0, getWidth(), getHeight());
            paint(i.getGraphics());
     
            g.drawImage(i, 0, 0, this);
        }
    }


    When I run, I get the following output:

    Sticks
    and
    Sticks and
    stones
    may
    stones may
    break
    my
    break my
    bones,
    but
    bones, but
    words
    can
    words can
    never
    hurt
    me.
    java.lang.ArrayIndexOutOfBoundsException: 13
    at Graph.Porthole_Text.<init>(Porthole_Text.java:22)
    at Graph.Appletir.init(Appletir.java:18)
    at sun.applet.AppletPanel.run(AppletPanel.java:424)
    at java.lang.Thread.run(Thread.java:662)

    And the screen does not paint out my test window a.
    Could someone please help me with this? And sorry about the unclarities if there are some...Can't seem to really type what I'm thinking...

    Thanks in advance!
    /Adzox
    Last edited by Adzox; December 25th, 2012 at 03:04 PM. Reason: Solved! Finally!


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Problems with my Portholes... (String splitting and adding to lines)

    1. Which line is Porthole_Text line 22, the line that causes the exception?
    2. Have you added debug statements to your code?
    3. Just curious why you're creating an AWT Applet rather than a Swing JApplet?

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Problems with my Portholes... (String splitting and adding to lines)

    Thanks for your reply and here are the following answers to your questions:
    1. It's the while statement:
    while (t.isEmpty() || (t.length() + words[id].length() + 1) * tSize <= cw)

    2. If I understand your question correctly, then it is line 25 and line 29,

    Line 25:
    System.out.println(words[id]);
    Line 29:
    System.out.println(t);

    3. Well...I haven't learned Swing yet...I wanted to complete AWT first. Was that a mistake?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problems with my Portholes... (String splitting and adding to lines)

    You need to move the debug calls to println to before the line where the error happens so you can see what the values of the variables are before the error happens.

    ArrayIndexOutOfBoundsException: 13
    You need to see why the index to an array goes past the end of the array.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Adzox (December 25th, 2012)

  6. #5
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Problems with my Portholes... (String splitting and adding to lines)

    Quote Originally Posted by Adzox View Post
    ...
    3. Well...I haven't learned Swing yet...I wanted to complete AWT first. Was that a mistake?
    Yeah, don't bother learning AWT except where there is overlap with Swing such as the layout managers. Just go straight to Swing.

  7. The Following User Says Thank You to curmudgeon For This Useful Post:

    Adzox (December 25th, 2012)

  8. #6
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Problems with my Portholes... (String splitting and adding to lines)

    I can't seem to solve it...Could you show me or something? Sory for the rudeness...And I didn't understand the thing with the debug call, if it's inside a while loop? Confused...

    --- Update ---

    Quote Originally Posted by curmudgeon View Post
    Yeah, don't bother learning AWT except where there is overlap with Swing such as the layout managers. Just go straight to Swing.
    Okay, I will then, just after this project ^^. Otherwise poor AWT will feel sad and left behind...

  9. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problems with my Portholes... (String splitting and adding to lines)

    java.lang.ArrayIndexOutOfBoundsException: 13
    at Graph.Porthole_Text.<init>(Porthole_Text.java:22)
    There is a problem on line 22. To see what is causing the exception, you need to insert a print statement just before line 22 that prints out the values of ALL the variables used on line 22 so you can see what variable has the value causing the error. Something like this:
    System.out.println("var1="+ var1 +", var2="+var2);
    replace var1 etc with the actual variables in your code.
    If you don't understand my answer, don't ignore it, ask a question.

  10. The Following User Says Thank You to Norm For This Useful Post:

    Adzox (December 25th, 2012)

  11. #8
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Problems with my Portholes... (String splitting and adding to lines)

    Okay, I think I solved the problem now. Thanks for all the help! It's really appriceated .

    Adzox out!

Similar Threads

  1. Help with splitting string in PDF Generator Function
    By efluvio in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 21st, 2012, 05:43 PM
  2. [SOLVED] I somehow can't find problems with a few lines
    By SOG in forum What's Wrong With My Code?
    Replies: 8
    Last Post: May 10th, 2012, 09:12 PM
  3. Splitting String
    By JuLiAnc in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 17th, 2011, 10:23 AM
  4. [SOLVED] Splitting String by Multiple Delimiters
    By relixus in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 4th, 2011, 08:54 PM
  5. Reading lines of a text file into a string array
    By fortune2k in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 11th, 2010, 11:56 AM

Tags for this Thread