O'Reilly logo

Java™ After Hours: 10 Projects You'll Never Do at Work by Steven Holzner

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating the Pucks

The pucks are represented by the sprite class Puck. The constructor of this class is responsible for storing all the information passed to it—the image for the puck, the type of puck (0 for a standard puck, 1 for the computer's blocker, and 2 for the user's blocker), the rectangle named edges corresponding to the dimensions of the rink, and so on. The constructor also sets up the puck's (random) initial position and velocity. Here's what the Puck constructor looks like:

 public Puck(Image image1, int type, int maxVelocity, Rectangle edges, Component rink) { this.rink = rink; this.image1 = image1; if (type > 0){ doNotMove = true; } this.maxVelocity = maxVelocity; this.edges = edges; random = new Random(System.currentTimeMillis()); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required