Results 1 to 3 of 3
  1. #1
    Samkough is offline Member
    Join Date
    May 2013
    Posts
    7
    Rep Power
    0

    Default Pong Overflow Error

    I figured this was Intermediate Java, so I just put it here instead of the Advanced Java section.

    Anyways, I can't figure out this problem that I'm having in my Pong game. It's compiling fine, except when I run it, it gives me an overflow error.

    Here's my code: PONG IN JAVA (CLASS/HOME) - Pastebin.com

    I learned a bit in my school (about Graphics) that's helped me and over the internet but other then that, I'm doing this by myself.

    Here's the code:

    Java Code:
    // Pong
    import javax.swing.JFrame;
    import java.awt.Color;
    import java.awt.Graphics;
     
    public class Pong extends JFrame
    {
            private static final int WIDTH = 400;
            private static final int HEIGHT = 400;
           
            Ball b = new Ball();
            Paddle p1 = new Paddle();
            Paddle p2 = new Paddle();
           
            protected int x;
            protected int y;
           
           
            public Pong()
            {
                    b.setSize(30, 30);
                    b.setColor(Color.BLUE);
                    b.setX(20);
                    b.setY(20);
                   
                    p1.setSize(40, 40);
                    p1.setColor(Color.RED);
                    p1.setX(50);
                    p1.setY(50);
                   
                    p2.setSize(40, 40);
                    p2.setColor(Color.RED);
                    p2.setX(100);
                    p2.setY(100);
           
                    add(b);
                    add(p1);
                    add(p2);
                    getContentPane().repaint();
            }
           
           
            public void paint(Graphics g)
            {
                   
            }
           
            public void setColor(Color c)
            {
                   
            }
           
            public boolean collides(boolean a)
            {
                    return a;
            }
           
            public void setSize(int width, int height)
            {
                   
            }
           
            /*public void setX(int i)
            {
                    x = i;
            }
     
            public void setY(int i)
            {
                    y = i;
            }
           
            public int getX()
            {
                    return x;
            }
           
            public int getY()
            {
                    return y;
            }*/
           
            public static void main(String args[])
            {
                    Pong frame = new Pong();
                   
                    frame.setSize(WIDTH, HEIGHT);
                    frame.setTitle("Pong");
                    frame.setResizable(false);
                    frame.setVisible(true);
                    frame.setLocationRelativeTo(null);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
    }
    Java Code:
    // Ball
    import java.awt.Color;
    import java.awt.Graphics;
     
    public class Ball extends Pong
    {
            public Ball()
            {
                    /*
                    b.setSize(30, 30);
                    b.setColor(Color.BLUE);
                    b.setX(20);
                    b.setY(20);
                    */
            }
           
            public void paint(Graphics g)
            {
                   
            }
           
            public void setColor(Color c)
            {
                   
            }
           
            public void setSize(int width, int height)
            {
                   
            }
           
            public void setX(int i)
            {
                    x = i;
            }
     
            public void setY(int i)
            {
                    y = i;
            }
           
            public int getX()
            {
                    return x;
            }
           
            public int getY()
            {
                    return y;
            }
    }
    Java Code:
    // Paddle
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.*;
     
    public class Paddle extends Pong implements KeyListener
    {
            public Paddle()
            {
                    /*
                    p1.setColor(Color.RED);
                    p1.setX(50);
                    p1.setY(50);
                   
                    p2.setSize(40, 40);
                    p2.setColor(Color.RED);
                    p2.setX(100);
                    p2.setY(100);
                    */
            }
           
            public void paint(Graphics g)
            {
                   
            }
           
            public void setColor(Color c)
            {
                   
            }
           
            public void setSize(int width, int height)
            {
                   
            }
           
            public void setX(int i)
            {
                    x = i;
            }
     
            public void setY(int i)
            {
                    y = i;
            }
           
            public int getX()
            {
                    return x;
            }
           
            public int getY()
            {
                    return y;
            }
     
            public void keyPressed(KeyEvent e)
            {
                   
            }
     
            public void keyReleased(KeyEvent e)
            {
                   
            }
     
            public void keyTyped(KeyEvent e)
            {
                   
            }
           
    }
    Here's the error message:
    java.lang.StackOverflowError
    at sun.awt.AppContext.get(AppContext.java:604)
    at com.sun.java.swing.SwingUtilities3.getDelegateRepa intManager(SwingUtilities3.java:120)
    at javax.swing.RepaintManager.getDelegate(RepaintMana ger.java:1625)
    at javax.swing.RepaintManager.addDirtyRegion(RepaintM anager.java:445)
    at javax.swing.JComponent.repaint(JComponent.java:479 5)
    at java.awt.Component.repaint(Component.java:3286)
    at javax.swing.JComponent.setBackground(JComponent.ja va:2733)
    at javax.swing.LookAndFeel.installColors(LookAndFeel. java:175)
    at javax.swing.LookAndFeel.installColorsAndFont(LookA ndFeel.java:211)
    at javax.swing.plaf.basic.BasicPanelUI.installDefault s(BasicPanelUI.java:66)
    at javax.swing.plaf.basic.BasicPanelUI.installUI(Basi cPanelUI.java:56)
    at javax.swing.JComponent.setUI(JComponent.java:664)
    at javax.swing.JPanel.setUI(JPanel.java:153)
    at javax.swing.JPanel.updateUI(JPanel.java:126)
    at javax.swing.JPanel.<init>(JPanel.java:86)
    at javax.swing.JPanel.<init>(JPanel.java:109)
    at javax.swing.JPanel.<init>(JPanel.java:117)
    at javax.swing.JRootPane.createGlassPane(JRootPane.ja va:545)
    at javax.swing.JRootPane.<init>(JRootPane.java:365)
    at javax.swing.JFrame.createRootPane(JFrame.java:277)
    at javax.swing.JFrame.frameInit(JFrame.java:258)
    at javax.swing.JFrame.<init>(JFrame.java:181)
    at Pong.<init>(Pong.java:19)
    at Ball.<init>(Ball.java:7)
    at Pong.<init>(Pong.java:10)
    at Ball.<init>(Ball.java:7)
    at Pong.<init>(Pong.java:10)
    at Ball.<init>(Ball.java:7)
    at Pong.<init>(Pong.java:10)
    at Ball.<init>(Ball.java:7)
    at Pong.<init>(Pong.java:10)
    at Ball.<init>(Ball.java:7)
    at Pong.<init>(Pong.java:10)
    at Ball.<init>(Ball.java:7)
    at Pong.<init>(Pong.java:10)
    at Ball.<init>(Ball.java:7)
    at Pong.<init>(Pong.java:10)
    at Ball.<init>(Ball.java:7)
    at Pong.<init>(Pong.java:10)
    ------------------------------------------------------------------------------------------------------------
    The error keeps going for a lot of more lines.

    The line that gets highlighted is the first bracket of the Pong constructor.
    Last edited by Samkough; 06-05-2014 at 03:19 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Pong Overflow Error

    Please paste the relevant code and error message in your post. Also, try keeping line numbers of the code and those mentioned in the error messages in sync.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Pong Overflow Error

    Your Ball extends Pong. And inside teh constructor of Pong you create a Ball. So that ends in a loop, because each Ball creates a Ball on its own.

    So just think about your object oriented design. Pong is a frame for your game. Is your Ball a frame of your game, too?
    Same with Paddle!

    With kind regards,

    Konrad

Similar Threads

  1. Help with stack overflow error.
    By stubz151 in forum New To Java
    Replies: 2
    Last Post: 11-09-2013, 06:00 PM
  2. GSON causing stack overflow error?
    By cele1989 in forum New To Java
    Replies: 2
    Last Post: 10-29-2012, 12:35 PM
  3. Gui problem and stack overflow error
    By zaricpp in forum Advanced Java
    Replies: 5
    Last Post: 06-03-2011, 10:54 AM
  4. Replies: 1
    Last Post: 03-17-2010, 06:25 AM
  5. Graphics2D: stack overflow error
    By rosh72851 in forum New To Java
    Replies: 11
    Last Post: 10-15-2008, 10:01 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
  •