# Cant get the ball to bounce around the canvass

• June 28th, 2011, 10:50 PM
RajivdeCosta
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

Code Java:

```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;```
• June 28th, 2011, 11:13 PM
Junky
Re: Cant get the ball to bounce around the canvass
Where do you repaint?
• June 29th, 2011, 02:07 AM
RajivdeCosta
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?
• June 29th, 2011, 07:27 AM
Norm
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.
• June 29th, 2011, 09:36 AM
KevinWorkman
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?
• June 29th, 2011, 11:59 PM
RajivdeCosta
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.
• June 30th, 2011, 07:52 AM
KevinWorkman
Re: Cant get the ball to bounce around the canvass
Quote:

Originally Posted by RajivdeCosta
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.
• June 30th, 2011, 08:07 AM
OutputStream
Re: Cant get the ball to bounce around the canvass
I thought Canvas was the only way to get direct rendering with Java2D?
• June 30th, 2011, 08:24 AM
KevinWorkman
Re: Cant get the ball to bounce around the canvass
Quote:

Originally Posted by OutputStream
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.
• June 30th, 2011, 08:55 AM
OutputStream
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
• June 30th, 2011, 09:01 AM
KevinWorkman
Re: Cant get the ball to bounce around the canvass
Quote:

Originally Posted by OutputStream
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.
• June 30th, 2011, 09:22 AM
OutputStream
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.
• June 30th, 2011, 09:35 AM
KevinWorkman
Re: Cant get the ball to bounce around the canvass
Quote:

Originally Posted by OutputStream
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.
• June 30th, 2011, 09:52 AM
OutputStream
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 ;)
• June 30th, 2011, 10:04 AM
KevinWorkman
Re: Cant get the ball to bounce around the canvass
Quote:

Originally Posted by OutputStream
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.
• June 30th, 2011, 10:11 AM
OutputStream
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.
• June 30th, 2011, 10:18 AM
KevinWorkman
Re: Cant get the ball to bounce around the canvass
Quote:

Originally Posted by OutputStream
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.