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 3 of 3

Thread: Pong Overflow Error

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Pong Overflow Error

    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:
    // 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);
            }
    }
    // 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;
            }
    }
    // 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:
    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; June 5th, 2014 at 07:24 AM.


  2. #2
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Pong Overflow Error

    Does the compiler mention which line actually causes the overflow?

    Overflow can have a variety of meanings - from placing to much on a stack frame
    to over-writing memory boundaries within an array.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Pong Overflow Error

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    Please post your code correctly per the above rather than a link to it. Post as little code as possible to see the relevant parts. If necessary, post a short, runnable example that demonstrates the problem. Most here won't follow links to code. Also, post the exact error message and stack trace, the whole thing, copied from exactly as it appears at your end.

Similar Threads

  1. Dealing With Overflow?
    By linzylu1190 in forum Loops & Control Statements
    Replies: 1
    Last Post: May 23rd, 2013, 07:40 PM
  2. Recursive method - stack overflow
    By yakir_g in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 8th, 2013, 07:38 AM
  3. [SOLVED] Banker Deadlock detection algorith going haywire and likely a stack overflow error
    By javapenguin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 23rd, 2012, 05:23 PM
  4. [SOLVED] stack overflow error
    By Goldfinch in forum What's Wrong With My Code?
    Replies: 14
    Last Post: February 3rd, 2012, 01:07 AM
  5. Replies: 0
    Last Post: October 14th, 2008, 06:40 PM