Results 1 to 5 of 5
Thread: add sound to my applet
- 03-21-2008, 05:16 PM #1
Member
- Join Date
- Feb 2008
- Posts
- 10
- Rep Power
- 0
add sound to my applet
Hello every body
i have an applet that makes a ball bounce, and i want to put a rebound sound that plays whenever the ball hit the wall. I don't know what kind files java can play and how to import. Thank you very much
here is he code :
[
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.JApplet;
public class AnimeBall extends JApplet {
Motivator fullMover;
Motivator fadeMover;
public void init() {
getContentPane().add(getContent());
}
public void start() {
fullMover.start();
fadeMover.start();
}
public void stop() {
fullMover.stop();
fadeMover.stop();
}
private JTabbedPane getContent() {
FullTrail fullTrail = new FullTrail();
fullMover = new Motivator(fullTrail);
FadingTrail fader = new FadingTrail();
fadeMover = new Motivator(fader);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("full trail", fullTrail);
tabbedPane.addTab("fading trail", fader);
return tabbedPane;
}
}
class FullTrail extends JPanel implements Movable {
BufferedImage offscreen;
Point loc;
int dx = 3;
int dy = 2;
final int D = 30;
protected void paintComponent(Graphics g) {
if(offscreen == null) {
initOffscreen();
}
g.drawImage(offscreen, 0, 0, this);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.red);
g2.fillOval(loc.x-D/2, loc.y-D/2, D, D);
}
public void moveBall() {
updateOffscreen();
checkBoundries();
loc.x += dx;
loc.y += dy;
repaint();
}
private void checkBoundries() {
if(loc.x - D/2 + dx < 0 || loc.x + D/2 + dx > getWidth())
dx *= -1;
if(loc.y - D/2 + dy < 0 || loc.y + D/2 + dy > getHeight())
dy *= -1;
}
private void initOffscreen() {
int w = getWidth();
int h = getHeight();
loc = new Point(w/2, h/2);
offscreen = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = offscreen.createGraphics();
g2.setColor(UIManager.getColor("Panel.background") );
g2.fillRect(0,0,w,h);
g2.dispose();
}
private void updateOffscreen() {
Graphics g = offscreen.getGraphics();
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(new Color(240,200,240));
g2.fillOval(loc.x-2, loc.y-2, 4, 4);
g2.dispose();
}
}
class FadingTrail extends JPanel implements Movable {
Point[] trail;
Point loc;
int dx = 2;
int dy = 3;
final int D = 30;
public FadingTrail() {
int trailLength = 20;
trail = new Point[trailLength];
for(int j = 0; j < trail.length; j++) {
trail[j] = new Point();
}
loc = new Point(200,100);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
RenderingHints.VALUE_ANTIALIAS_ON);
Graphics2D copy = (Graphics2D)g.create();
copy.setPaint(new Color(240,200,240));
float alpha = 1f;
float alphaInc = 1f/20.5f;
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER , alpha);
for(int j = 0; j < trail.length; j++) {
copy.setComposite(ac);
copy.fillOval(trail[j].x-2, trail[j].y-2, 4, 4);
alpha -= alphaInc;
ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER , alpha);
}
copy.dispose();
g2.setPaint(Color.red);
g2.fillOval(loc.x-D/2, loc.y-D/2, D, D);
}
public void moveBall() {
updateTrail();
checkBoundries();
loc.x += dx;
loc.y += dy;
repaint();
}
private void checkBoundries() {
if(loc.x - D/2 + dx < 0 || loc.x + D/2 + dx > getWidth())
dx *= -1;
if(loc.y - D/2 + dy < 0 || loc.y + D/2 + dy > getHeight())
dy *= -1;
}
private void updateTrail() {
for(int j = trail.length-1; j > 0; j--) {
trail[j].setLocation(trail[j-1].getLocation());
}
double theta = Math.atan2(dy,dx) + Math.PI;
int x = loc.x + (int)((D/2)*Math.cos(theta));
int y = loc.y + (int)((D/2)*Math.sin(theta));
trail[0].setLocation(x, y);
}
}
interface Movable {
public void moveBall();
}
class Motivator implements Runnable {
Movable movable;
Thread thread;
boolean keepMoving;
public Motivator(Movable m) {
movable = m;
}
public void run() {
while(keepMoving) {
try {
Thread.sleep(50);
} catch(InterruptedException e) {
keepMoving = false;
}
movable.moveBall();
}
}
public void start() {
if(!keepMoving) {
keepMoving = true;
thread = new Thread(this);
thread.setPriority(Thread.NORM_PRIORITY);
thread.start();
}
}
public void stop() {
keepMoving = false;
thread.interrupt();
thread = null;
}
}
]
- 03-22-2008, 09:08 PM #2
Member
- Join Date
- Feb 2008
- Posts
- 10
- Rep Power
- 0
after thinking and researching a little bit ,i just want to hear a sound continuously when my applet is executed so i change my code, but i canot hear any sound can someone help me please
my applet is named AnimeBall.java
[CODE]
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.JApplet;
public class AnimeBall extends JApplet {
Motivator fullMover;
Motivator fadeMover;
public void init() {
getContentPane().add(getContent());
}
public void start() {
fullMover.start();
fadeMover.start();
}
public void stop() {
fullMover.stop();
fadeMover.stop();
}
private JTabbedPane getContent() {
FullTrail fullTrail = new FullTrail();
fullMover = new Motivator(fullTrail);
FadingTrail fader = new FadingTrail();
fadeMover = new Motivator(fader);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("full trail", fullTrail);
tabbedPane.addTab("fading trail", fader);
return tabbedPane;
}
}
class FullTrail extends JPanel implements Movable {
BufferedImage offscreen;
Point loc;
int dx = 3;
int dy = 2;
final int D = 30;
protected void paintComponent(Graphics g) {
if(offscreen == null) {
initOffscreen();
}
g.drawImage(offscreen, 0, 0, this);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.red);
g2.fillOval(loc.x-D/2, loc.y-D/2, D, D);
}
public void moveBall() {
updateOffscreen();
checkBoundries();
loc.x += dx;
loc.y += dy;
repaint();
}
private void checkBoundries() {
if(loc.x - D/2 + dx < 0 || loc.x + D/2 + dx > getWidth())
dx *= -1;
if(loc.y - D/2 + dy < 0 || loc.y + D/2 + dy > getHeight())
dy *= -1;
}
private void initOffscreen() {
int w = getWidth();
int h = getHeight();
loc = new Point(w/2, h/2);
offscreen = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = offscreen.createGraphics();
g2.setColor(UIManager.getColor("Panel.background") );
g2.fillRect(0,0,w,h);
g2.dispose();
}
private void updateOffscreen() {
Graphics g = offscreen.getGraphics();
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(new Color(240,200,240));
g2.fillOval(loc.x-2, loc.y-2, 4, 4);
g2.dispose();
}
}
class FadingTrail extends JPanel implements Movable {
Point[] trail;
Point loc;
int dx = 2;
int dy = 3;
final int D = 30;
public FadingTrail() {
int trailLength = 20;
trail = new Point[trailLength];
for(int j = 0; j < trail.length; j++) {
trail[j] = new Point();
}
loc = new Point(200,100);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
RenderingHints.VALUE_ANTIALIAS_ON);
Graphics2D copy = (Graphics2D)g.create();
copy.setPaint(new Color(240,200,240));
float alpha = 1f;
float alphaInc = 1f/20.5f;
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER , alpha);
for(int j = 0; j < trail.length; j++) {
copy.setComposite(ac);
copy.fillOval(trail[j].x-2, trail[j].y-2, 4, 4);
alpha -= alphaInc;
ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER , alpha);
}
copy.dispose();
g2.setPaint(Color.red);
g2.fillOval(loc.x-D/2, loc.y-D/2, D, D);
}
public void moveBall() {
updateTrail();
checkBoundries();
loc.x += dx;
loc.y += dy;
repaint();
}
private void checkBoundries() {
if(loc.x - D/2 + dx < 0 || loc.x + D/2 + dx > getWidth())
dx *= -1;
if(loc.y - D/2 + dy < 0 || loc.y + D/2 + dy > getHeight())
dy *= -1;
}
private void updateTrail() {
for(int j = trail.length-1; j > 0; j--) {
trail[j].setLocation(trail[j-1].getLocation());
}
double theta = Math.atan2(dy,dx) + Math.PI;
int x = loc.x + (int)((D/2)*Math.cos(theta));
int y = loc.y + (int)((D/2)*Math.sin(theta));
trail[0].setLocation(x, y);
}
}
interface Movable {
public void moveBall();
}
class Motivator implements Runnable {
Movable movable;
Thread thread;
boolean keepMoving;
public Motivator(Movable m) {
movable = m;
}
public void run() {
while(keepMoving) {
try {
Thread.sleep(50);
} catch(InterruptedException e) {
keepMoving = false;
}
movable.moveBall();
}
}
public void start() {
if(!keepMoving) {
keepMoving = true;
thread = new Thread(this);
thread.setPriority(Thread.NORM_PRIORITY);
thread.start();
}
}
public void stop() {
keepMoving = false;
thread.interrupt();
thread = null;
}
}
class SoundApplet extends Applet {
public void init() {
super.init();
AudioClip gong = getAudioClip(getDocumentBase(), "gong.au");
gong.loop();
}
}
[CODE]
- 03-22-2008, 09:12 PM #3
Member
- Join Date
- Feb 2008
- Posts
- 10
- Rep Power
- 0
and my file gong.au is with my class folder
- 03-24-2008, 11:16 PM #4
Member
- Join Date
- Mar 2008
- Posts
- 19
- Rep Power
- 0
An easy way to playback a sound file
import javax.swing.*;
import java.applet.*;
import java.net.*;
public class sound extends JApplet
{
private String file = "sound.wav";
public void init()
{
AudioClip ac = getAudioClip(getCodeBase(), file);
ac.play(); ///or you could use ac.loop(); or ac.stop(); methods
}
}
This should work for playing back a regular Wav, Aif or Au files. If they don't playback I suggest trying a few different formats of Wav until you find one that works, it can be fickle about certain file types. Keep trying till you get the right format. My favorite is interleaved, 16 bit and 44100 Hertz sampling rate. Good luck :{p
- 03-24-2008, 11:26 PM #5
Member
- Join Date
- Mar 2008
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
applet sound
By marco in forum Java AppletsReplies: 1Last Post: 09-28-2007, 10:31 PM -
Sound in applet Java
By toby in forum Java AppletsReplies: 1Last Post: 08-07-2007, 05:52 AM -
Playing sound in applet??????
By Bagesh in forum Java AppletsReplies: 2Last Post: 07-13-2007, 04:46 PM -
Applets having sound
By peiceonly in forum Java AppletsReplies: 2Last Post: 03-31-2007, 09:55 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks