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

Thread: Cant get the ball to bounce around the canvass

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Location
    Gurgaon, India
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Cant get the ball to bounce around the canvass

    Hi

    I am trying to solve a programming exercise which is in a book that I am reading on Java. Bouncing a ball around a canvass.

    Attaching the code here. Please tell where I have gone wrong. Also is there any way to get the x and y numbers without using the "get" function.

    Thank you

    public class  BouncingBall extends GraphicsProgram {
     
    	public void run() {
    		double centeredx = (getWidth()/2);
    		double centeredy = (getHeight()/2);
    		GOval ball = new GOval(centeredx,centeredy,BALL_SIZE,BALL_SIZE);
    		ball.setFilled(true);
    		add(ball);
     
    		double dx = 1;
    		double dy = 1;
     
    		while (true){
    			ball.move(dx,dy);
    			pause(PAUSE_TIME);	
     
    		double x = ball.getX();
    		double y = ball.getY();
    		if (x >= getWidth() - BALL_SIZE){
    			dx = - 1;
    		if (y >= getHeight() - BALL_SIZE){
    			dy = - 1;
    		if (x == 0){
    			dx = -1;
    		if (y == 0){
    			dy = -1;
     
    				}
    			}
    		}
    	}
    }
     
     
     
    	}
     
    /* Private constant*/
     
    	private static final int BALL_SIZE = 40;
    	private static final int PAUSE_TIME = 50;
    Last edited by RajivdeCosta; June 29th, 2011 at 02:37 AM.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,504
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Cant get the ball to bounce around the canvass

    Where do you repaint?

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Location
    Gurgaon, India
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cant get the ball to bounce around the canvass

    Hi

    Thanks for the reply. Canvas meaning screen. What have I done wrong in the code?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Cant get the ball to bounce around the canvass

    You appear to be using some third party classes. For example: GraphicsProgram and GOval.
    What functionality do they provide? I'm sure they do a lot that won't be known to anyone else.

    It will be hard for anyone to help you without being able to compile and execute the code.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,243
    My Mood
    Hungover
    Thanks
    142
    Thanked 617 Times in 528 Posts

    Default Re: Cant get the ball to bounce around the canvass

    Also, your if statements look all wrong. First off, they're nested inside each other, when I'm pretty sure that's not what you want. Secondly, I don't think you want to simply set the deltaX to negative one in those cases. Take a closer look at the code in your book.

    But why are you using AWT instead of Swing?
    Last edited by KevinWorkman; June 29th, 2011 at 09:40 AM.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Junior Member
    Join Date
    Jun 2011
    Location
    Gurgaon, India
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cant get the ball to bounce around the canvass

    Hi

    Managed solve it! Many thanks KevinWorkman! I don't yet know what you mean by the the last question? But it going to be fun finding out. Thank again.

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,243
    My Mood
    Hungover
    Thanks
    142
    Thanked 617 Times in 528 Posts

    Default Re: Cant get the ball to bounce around the canvass

    Quote Originally Posted by RajivdeCosta View Post
    I don't yet know what you mean by the the last question?
    You said you're using Canvas, which is an AWT component. That's the old GUI classes, and usually people don't use them unless they have a reason to. People use Swing now.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Member OutputStream's Avatar
    Join Date
    Apr 2011
    Posts
    32
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 3 Posts

    Default Re: Cant get the ball to bounce around the canvass

    I thought Canvas was the only way to get direct rendering with Java2D?

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,243
    My Mood
    Hungover
    Thanks
    142
    Thanked 617 Times in 528 Posts

    Default Re: Cant get the ball to bounce around the canvass

    Quote Originally Posted by OutputStream View Post
    I thought Canvas was the only way to get direct rendering with Java2D?
    I guess it depends on what you mean by "direct rendering". Most people extend JPanel or JComponent, override paintComponent(), do all drawing in there, then call repaint() whenever they update the view.

    Doing "direct rendering" by calling getGraphics() or something similar is usually WAY more trouble than it's worth, unless you know what you're doing.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Member OutputStream's Avatar
    Join Date
    Apr 2011
    Posts
    32
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 3 Posts

    Default Re: Cant get the ball to bounce around the canvass

    JComponents uses passive rendering (which means you have to wait for the EDT to render the JComponent).
    With a Canvas, it's very easy to set up a BufferStrategy. You then use that BufferStrategy to render directly to the screen (it has support for multiple buffers as well). Since you don't have to wait for the EDT to render the component for you, rendering to a Canvas is much smoother

  11. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,243
    My Mood
    Hungover
    Thanks
    142
    Thanked 617 Times in 528 Posts

    Default Re: Cant get the ball to bounce around the canvass

    Quote Originally Posted by OutputStream View Post
    JComponents uses passive rendering (which means you have to wait for the EDT to render the JComponent).
    With a Canvas, it's very easy to set up a BufferStrategy. You then use that BufferStrategy to render directly to the screen (it has support for multiple buffers as well). Since you don't have to wait for the EDT to render the component for you, rendering to a Canvas is much smoother
    If you say so. I've never had any problems using JComponents, and in my humble opinion, doing "active rendering" introduces a bunch of other stuff that just confuses the basics- especially for newbies. We get so many posts saying "why does my display flicker" because people are doing "active rendering" incorrectly, when really they should just do it the way I outlined. And I'm putting quotes around "active rendering" because what you said is a bit misleading- you can call repaint() anytime you want.

    Anyway, your mileage may vary. But I stick with extending JPanel or JComponent, overriding paintComponent() and doing my painting in there, and calling repaint() when necessary. It keeps everything much cleaner, is easier for newbies (or non-newbies) to understand and not screw up, and it can render smoothly just fine at 60 fps. For 99% of the cases, that's what people should be doing.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    Member OutputStream's Avatar
    Join Date
    Apr 2011
    Posts
    32
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 3 Posts

    Default Re: Cant get the ball to bounce around the canvass

    Not saying JComponents are bad or anything, if it's working for you then stick to it.
    I had an old game project once which used JPanels and repaint() for rendering. However, I noticed it started stuttering and caused very noticeable hiccups at random times. Once I switched to a Canvas, all stuttering and hiccups went away.

  13. #13
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,243
    My Mood
    Hungover
    Thanks
    142
    Thanked 617 Times in 528 Posts

    Default Re: Cant get the ball to bounce around the canvass

    Quote Originally Posted by OutputStream View Post
    Not saying JComponents are bad or anything, if it's working for you then stick to it.
    I had an old game project once which used JPanels and repaint() for rendering. However, I noticed it started stuttering and caused very noticeable hiccups at random times. Once I switched to a Canvas, all stuttering and hiccups went away.
    Honestly, it sounds like you were doing something wrong (probably the "active rendering" you're talking about). It's a common problem, and it keeps popping up because people try to complicate things for some reason. Maybe Canvas lets you get away with things that Swing doesn't, but I wouldn't rely on it- especially because mixing Swing and AWT is a bad idea anyway. My current pet project is rendering using an extended JPanel just fine at 60 fps.

    This is weirdly mirroring a conversation I'm having on the other forums.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  14. #14
    Member OutputStream's Avatar
    Join Date
    Apr 2011
    Posts
    32
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 3 Posts

    Default Re: Cant get the ball to bounce around the canvass

    All I had was a JFrame which I added one JPanel to. When I switched to a Canvas I just had one JFrame with one Canvas on it. The only thing I changed in the rendering process was how I obtained the Graphics object to render with. With the JPanel I used the Graphics from paintComponent, with the Canvas I used the Graphics object you get from the BufferStrategy.
    When I had the JPanel, I didn't use active rendering since I called repaint() from another Thread.

    Also, this is the only forum I'm having this discussion on

  15. #15
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,243
    My Mood
    Hungover
    Thanks
    142
    Thanked 617 Times in 528 Posts

    Default Re: Cant get the ball to bounce around the canvass

    Quote Originally Posted by OutputStream View Post
    All I had was a JFrame which I added one JPanel to. When I switched to a Canvas I just had one JFrame with one Canvas on it. The only thing I changed in the rendering process was how I obtained the Graphics object to render with. With the JPanel I used the Graphics from paintComponent, with the Canvas I used the Graphics object you get from the BufferStrategy.
    When I had the JPanel, I didn't use active rendering since I called repaint() from another Thread.

    Also, this is the only forum I'm having this discussion on
    Without seeing the actual code, I can't really comment, but I promise that extending a JPanel and overriding paintComponent() works just fine. If I had to hazard a guess, I'd bet that you're updating the view in the thread at the same time as the stuff you're updating is rendering, so you get frames that are half wrong, which could lead to flickering or stuttering. A quick fix for this would be to do all your updating on the EDT- use a Swing Timer instead of a Thread, or call SwingUtililities.invokeLater() from your Thread. That way only a single Thread is reading or writing the values used for display.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  16. #16
    Member OutputStream's Avatar
    Join Date
    Apr 2011
    Posts
    32
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 3 Posts

    Default Re: Cant get the ball to bounce around the canvass

    I did all updating on the EDT. The JPanel looked fine when rendering, no frames were wrong.

  17. #17
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,243
    My Mood
    Hungover
    Thanks
    142
    Thanked 617 Times in 528 Posts

    Default Re: Cant get the ball to bounce around the canvass

    Quote Originally Posted by OutputStream View Post
    I did all updating on the EDT. The JPanel looked fine when rendering, no frames were wrong.
    Then I guess I'm not sure what you mean by stuttering. But again, without seeing any code, I'm just guessing anyway. I'd take a look at it if you posted an SSCCE in a new thread, but it seems like you've got a way that works for you as is.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Bouncing Ball Program Random Color Change
    By coderEvolution in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 3rd, 2011, 03:01 PM
  2. Need help with a third ball in game.
    By vlan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 12th, 2010, 03:35 PM