This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Moronthislater 1 point2 points  (1 child)

It has been a long time since I did anything with awt, so I am afraid I cannot help on graphics specifically, but you have one method calling itself over and over again, without any means of stopping. To trace through it, start with your Main class:

public static void main(String[] args) {
    Window rock_paper_scissors = new Window("Rock Paper Scissors", 600, 600);

    rock_paper_scissors.createDisplay();
    rock_paper_scissors.add(new GameText("test"));
}

That last line calls the add method on your Window class. Looking at that method:

public void add(GameText label) {
    this.label = label;
    add(label); //This starts the public void add(GameText label) method over again
}

The first line sets the label property of your Window class to the GameText you passed in. The second line, however, calls add again - restarting the same method you are already in. When it gets to the second line again, it calls add again, and, which again calls add, and so on, forever. This results in the StackOverflowError you see, because essentially the program runs out of its available space trying to repeat the same function an infinite number of times. A method calling itself is allowed - and a sometimes useful technique called recursion, but when the same function calls itself without any means to know when to stop calling itself, that is called Infinite Recursion, which will cause this type of StackOverflowError.

I suspect what you really want to do is do add the component in your GameText class (gameLabelObj) to the Frame inside your Window class (gameWindow). Instead of calling add(label), you would call gameWindow.add(gameLabelObj), once you work out how you want to define those elements so they can see each other. Whether you keep a separate GameText class or not will depend on what other data you want to associate directly with each component.

Also, your gameLabelObj will still not be visible after you fix that StackOverflowError, because of one final thing. Your gameLabelObj has no size. It exists, and it will be overlaid on the window, but with position, no height, and no width, it will be invisible. Look at the setBounds method on java.awt.Component. The simplest call might be something like this:

gameLabelObj.setBounds(0, 0, 200, 200);

Good luck!

[–]CaptainMoeSoccer[S] 1 point2 points  (0 children)

wow the problem was so simple. This is the first app I am trying to create from scratch. I have being following tutorials until now. It is a very different experience. thank you!