Hi everyone, i'm trying to make a sound each time a ball touches the lift and right of a frame but when i use clip and try to use the sound class it sound from right then left the in middle and then stop. i Can really use some help. The code is down below..

package basics;
import java.awt.*;
import java.util.Formatter;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.*;


import java.awt.Toolkit;



// Extends JPanel, so as to override the paintComponent() for custom rendering codes.
public class MovingLeftRight extends JPanel {

// Container box's width and height
private static final int BOX_WIDTH = 640;
private static final int BOX_HEIGHT = 480;
private Clip clip;
// Ball's properties
private float ballRadius = 50; // Ball's radius
private float ballX =250- ballRadius ; // Ball's center (x, y)
private float ballY = 250-ballRadius;
private float ballSpeedX = 7; // Ball's speed for x and y
//private float ballSpeedY = 2;

private static final int UPDATE_RATE = 30; // Number of refresh per second

/** Constructor to create the UI components and init game objects. */
public MovingLeftRight() {
super();



this.setPreferredSize(new Dimension(BOX_WIDTH, BOX_HEIGHT));


// Start the ball bouncing (in its own thread)
Thread gameThread = new Thread() {
public void run() {
	try {
	    // Open an audio input stream.
		File soundFile = new File("resources/snd16.wav");

	    // Get a sound clip resource.
	    Clip clip = AudioSystem.getClip();
	    // Open audio clip and load samples from the audio input stream.
	    AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
	    clip.open(audioIn);
	    while (true) { // Execute one update step
	    	// Calculate the ball's new position
	    	ballX += ballSpeedX;
	    	// Check if the ball moves over the bounds
	    	// If so, adjust the position and speed.
	    	if (ballX - ballRadius < 0) {
	    	ballSpeedX = -ballSpeedX; // Reflect along normal
	    	ballX = ballRadius; // Re-position the ball at the edge
	    	clip.start();
	    	//Toolkit.getDefaultToolkit().beep();
	    	clip.loop(4);


	    	// Sounds.reproduceSound(Sounds.PING);

	    	} else if (ballX + ballRadius > BOX_WIDTH) {
	    	ballSpeedX = -ballSpeedX;
	    	ballX = BOX_WIDTH - ballRadius;
	    	//clip.setLoopPoints(0, 1);
	    	clip.start();
	    	//Toolkit.getDefaultToolkit().beep();
	    	}

	    	// Refresh the display
	    	repaint(); // Callback paintComponent()
	    	// Delay for timing control and give other threads a chance
	    	try {
	    	Thread.sleep(1000 / UPDATE_RATE); // milliseconds
	    	} catch (InterruptedException ex) { }
	    	}

	 } catch (UnsupportedAudioFileException e) {
	    e.printStackTrace();
	 } catch (IOException e) {
	    e.printStackTrace();
	 } catch (LineUnavailableException e) {
	    e.printStackTrace();
	 }
	//boolean isEnableSound;

}
};
gameThread.start(); // Callback run()
}

/** Custom rendering codes for drawing the JPanel */
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g); // Paint background

// Draw the box
g.setColor(Color.BLACK);
g.fillRect(0, 0, BOX_WIDTH, BOX_HEIGHT);

// Draw the ball
g.setColor(Color.GREEN);
g.fillOval((int) (ballX - ballRadius), (int) (ballY - ballRadius),
(int)(2 * ballRadius), (int)(2 * ballRadius));

// Display the ball's information
g.setColor(Color.WHITE);
g.setFont(new Font("Courier New", Font.PLAIN, 12));
StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb);
formatter.format("Ball @(%3.0f) Speed=(%2.0f)", ballX,ballSpeedX);
g.drawString(sb.toString(), 20, 30);
}

/** main program (entry point) */
public static void main(String[] args) {
// Run GUI in the Event Dispatcher Thread (EDT) instead of main thread.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
// Set up main window (using Swing's Jframe)
JFrame frame = new JFrame("A Moving Ball");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new MovingLeftRight());
frame.pack();
frame.setVisible(true);
}
});
}
}

Recommended Answers

All 4 Replies

can you give a bit more information before we start going through that code? what is the sound format you're trying to use?

The sound format is .wav

Dont worry about it anymore i got it figured out and Thanks for your interest.

Hi there.
It's great that you figured it out. In the share and cooperate spirit of DaniWeb do you think you could take a few minutes to explain how you solved it? I'm sure there are lots of us out here who would like to add some sound effects to our games etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.