Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#Actualj-locke

Posted 11 August 2012 - 03:48 PM

Haha, no problem. What you described is right.

First, a little insight into what's happening.
As you've seen, the underlying framework will call the Applet's paint method for you whenever it needs to be repainted. You've extended the Applet class with your game class (public class game extends Applet). So now the underlying framework is calling the paint method in your game class.
Right now you're drawing the balls directly in that method.

What you're gonna try now is instead of drawing directly in the game class' paint method is to call the ball class' paint method and let the ball take care of drawing itself.

So, things you need to actually do are:
1. Inside the Ball class, add a paint method that takes a Graphics object as a parameter. Then use that Graphics object to draw itself. So, from your existing code you can see that drawing a ball means using a line of code to set the color and a line of code to draw a filled oval.

So after step 1, your code should be able to compile. Nothing will have changed yet though. If you run it, it will be exactly like before. Because even though you've added that method with the code inside of it, nobody is calling that method so it doesn't affect how your program actually runs.

2. For this part, you're going to put the new method from step 1 to use. Inside game's paint method, you have a loop that loops through each ball in the array of balls. What you want to do is call the method from step 1 on each of the balls in that array.

#1j-locke

Posted 11 August 2012 - 03:46 PM

Haha, no problem. What you described is right.

First, a little insight into what's happening.
As you've seen, the underlying framework will call the Applet's paint method for you whenever it needs to be repainted. You've extended the Applet class with your game class. So now the underlying framework is calling the paint method in your game class.
Right now you're drawing the balls directly in that method.

What you're gonna try now is instead of drawing directly in the game class' paint method is to call the ball class' paint method and let the ball take care of drawing itself.

So, things you need to actually do are:
1. Inside the Ball class, add a paint method that takes a Graphics object as a parameter. Then use that Graphics object to draw itself. So, from your existing code you can see that drawing a ball means using a line of code to set the color and a line of code to draw a filled oval.

So after step 1, your code should be able to compile. Nothing will have changed yet though. If you run it, it will be exactly like before. Because even though you've added that method with the code inside of it, nobody is calling that method so it doesn't affect how your program actually runs.

2. For this part, you're going to put the new method from step 1 to use. Inside game's paint method, you have a loop that loops through each ball in the array of balls. What you want to do is call the method from step 1 on each of the balls in that array.

PARTNERS