Creating a Sound class for our game

To keep the audioclips of our game we create a Sound class, which we´ll have a constant with an audioclip for each of the sounds we use. These constants are public so that any object which have access to them, can play them. For example, in the Ball class we can play the sound of the bouncing of the ball using at the moment we know the ball changes its direction.

package com.edu4java.minitennis7;

import java.applet.Applet;
import java.applet.AudioClip;

public class Sound {
	public static final AudioClip BALL = Applet.newAudioClip(Sound.class.getResource("ball.wav"));
	public static final AudioClip GAMEOVER = Applet.newAudioClip(Sound.class.getResource("gameover.wav"));
	public static final AudioClip BACK = Applet.newAudioClip(Sound.class.getResource("back.wav"));

The audioclips objects will be created when the Sound class loads, the first time someone uses the Soung class. From this moment on, they will be re-used. Let´s now look at the modifications in the Game class:

package com.edu4java.minitennis7;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Game extends JPanel {

	Ball ball = new Ball(this);
	Racquet racquet = new Racquet(this);

	public Game() {
		addKeyListener(new KeyListener() {
			public void keyTyped(KeyEvent e) {

			public void keyReleased(KeyEvent e) {

			public void keyPressed(KeyEvent e) {
	private void move() {

	public void paint(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
	public void gameOver() {
		JOptionPane.showMessageDialog(this, "Game Over", "Game Over", JOptionPane.YES_NO_OPTION);

	public static void main(String[] args) throws InterruptedException {
		JFrame frame = new JFrame("Mini Tennis");
		Game game = new Game();
		frame.setSize(300, 400);
		while (true) {

In the last line of the Game class constructor, we add Sound.BACK.loop(), which will initiate the playing of our background music and will play repeatedly till it gets to the gameOver() method, where we stop the background music with Sound.BACK.stop(). After Sound.BACK.stop() and before the popup, we inform that the game is over playing "Game Over"

In the Ball class, we change the move() method so that it plays Sound.BALL when the ball bounces.

package com.edu4java.minitennis7;

import java.awt.Graphics2D;
import java.awt.Rectangle;

public class Ball {
	private static final int DIAMETER = 30;
	int x = 0;
	int y = 0;
	int xa = 1;
	int ya = 1;
	private Game game;

	public Ball(Game game) { = game;

	void move() {
		boolean changeDirection = true;
		if (x + xa < 0)
			xa = 1;
		else if (x + xa > game.getWidth() - DIAMETER)
			xa = -1;
		else if (y + ya < 0)
			ya = 1;
		else if (y + ya > game.getHeight() - DIAMETER)
		else if (collision()){
			ya = -1;
			y = game.racquet.getTopY() - DIAMETER;
		} else 
			changeDirection = false;
		if (changeDirection);
		x = x + xa;
		y = y + ya;

	private boolean collision() {
		return game.racquet.getBounds().intersects(getBounds());

	public void paint(Graphics2D g) {
		g.fillOval(x, y, DIAMETER, DIAMETER);

	public Rectangle getBounds() {
		return new Rectangle(x, y, DIAMETER, DIAMETER);

What I did in move() is add a changeDirection variable, which I initialize to true. Adding an "else" to every "if" and writing a changeDirection = false which only will runs if none of the conditions in the "if" are true, we will know if the ball has bounced. If the ball has bounced, changeDirection will be true and will be executed.

<< Adding sound to our game Adding punctuation and speed increase >>