Results 1 to 8 of 8
  1. #1
    omidelf is offline Member
    Join Date
    Jan 2016
    Posts
    8
    Rep Power
    0

    Default why this program not running properly ?

    this program : Ping Pong Game in Java

    download link in in the bottom , when i run it with eclipse or IDE , it keeps flashing! like im switching windows 20 time per sec ! is it just me ?

    how to fix it ?

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: why this program not running properly ?

    First observation: this game overrides paint(). You should override paintComponent().
    Second observation: a method called "doSomething". Really? As soon as you see methods named like that, you are allowed to question the reliability of the tutorial, if not the entire site.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: why this program not running properly ?

    Quote Originally Posted by SurfMan View Post
    First observation: this game overrides paint(). You should override paintComponent().
    Actually, I believe paint() is correct since this is AWT and there is no paintComponent(). But your skepticism on the site is well-founded. Magic numbers every where. Mixing AWT and Swing. I would stay away from this site and go buy a good book.

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

  4. #4
    omidelf is offline Member
    Join Date
    Jan 2016
    Posts
    8
    Rep Power
    0

    Default Re: why this program not running properly ?

    so it flashes for you too ?/

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: why this program not running properly ?

    Quote Originally Posted by omidelf View Post
    so it flashes for you too ?/
    No. I didn't run the code. But now I am curious so I may try it.

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

  6. #6
    omidelf is offline Member
    Join Date
    Jan 2016
    Posts
    8
    Rep Power
    0

    Default Re: why this program not running properly ?

    yes please try , because when i run it it keeps flashing , wanna know WHY ? what is causing this to happen ?

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: why this program not running properly ?

    Ok, I see the flash. Probably due to AWT and Swing interaction. So try the following:

    Replace the paint() method with a paintComponent() method. Here is the code.

    Java Code:
    public void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2d = (Graphics2D) g;
          g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
          g2d.setColor(Color.black);
          g2d.fillOval(x, y, 20, 20);
    
          g2d.setColor(Color.red);
          g2d.fillRect(a, b, 100, 10);
    
          g2d.setColor(Color.red);
          g2d.fillRect(c, d, 100, 10);
    
          g2d.drawString("Player 2 Score : " + p1 + "Points ", 100, 200);
          g2d.drawString("Player 1 Score : " + p2 + "Points ", 100, 230);
       }
    The anti aliasing code isn't necessary but it makes the graphics look smoother on the edges.

    Now, have the main class extend JPanel, not Canvas. It is javax.swing.JPanel.
    Change the dx and dy values from 5 to 1 and run the program.

    The ball will travel much slower. To fix this, change the time delay from 30 to 10 or 5.

    There are still problems with this program. Like it claims a loss when it actually hits the paddle.

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

  8. #8
    omidelf is offline Member
    Join Date
    Jan 2016
    Posts
    8
    Rep Power
    0

    Default Re: why this program not running properly ?

    Quote Originally Posted by jim829 View Post
    Ok, I see the flash. Probably due to AWT and Swing interaction. So try the following:

    Replace the paint() method with a paintComponent() method. Here is the code.

    Java Code:
    public void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2d = (Graphics2D) g;
          g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
          g2d.setColor(Color.black);
          g2d.fillOval(x, y, 20, 20);
    
          g2d.setColor(Color.red);
          g2d.fillRect(a, b, 100, 10);
    
          g2d.setColor(Color.red);
          g2d.fillRect(c, d, 100, 10);
    
          g2d.drawString("Player 2 Score : " + p1 + "Points ", 100, 200);
          g2d.drawString("Player 1 Score : " + p2 + "Points ", 100, 230);
       }
    The anti aliasing code isn't necessary but it makes the graphics look smoother on the edges.

    Now, have the main class extend JPanel, not Canvas. It is javax.swing.JPanel.
    Change the dx and dy values from 5 to 1 and run the program.

    The ball will travel much slower. To fix this, change the time delay from 30 to 10 or 5.

    There are still problems with this program. Like it claims a loss when it actually hits the paddle.

    Regards,
    Jim
    thanks it did work fine after i replaced it with this code

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2013, 03:36 PM
  2. Problem with program not outputting properly
    By ericj5555 in forum New To Java
    Replies: 3
    Last Post: 10-16-2012, 03:41 AM
  3. My Checkbox program is not working properly
    By newbie106 in forum New To Java
    Replies: 8
    Last Post: 08-17-2012, 10:44 PM
  4. Not getting why the program is not running
    By jnjh in forum New To Java
    Replies: 5
    Last Post: 04-07-2011, 11:25 AM
  5. This program compiles but doesnt run properly!
    By ErikD99 in forum New To Java
    Replies: 5
    Last Post: 12-03-2010, 09:44 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
  •