Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Calling sounds

I've been going at this problem for over 12 hours and have reached wits end. I need to have the button change the red ball's angle abour 45 degrees (I plan on using if else statements... if xinc==0 and y >0 {xinc=x speed, yinc=yspeed}, else if xinc>0 && yinc <0 {yinc=0}, else if xinc>0 && yinc==0 {xint=x speed, yinc = -y speed and so on)

Any ideas how to implement this? I'm having issues with the A3 class passing the value to CollideBall and only having the red ball react to the change.

Thanks in advance!

import javax.swing.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

class CollideBall
{
public int width, height;
public static final int diameter=30;
//coordinates and value of increment
double x, y, xinc, yinc, coll_x, coll_y;

boolean collide, turn;
Color color;
Graphics g;
AudioClip sound1;
A3 thread;

//the constructor
public CollideBall(int w, int h, int x, int y, double xinc, double yinc, Color c, A3 thread)
{


width=w;
height=h;
this.x=x;
this.y=y;
this.xinc=xinc;
this.yinc=yinc;
color=c;
this.thread=thread;


}

public double getCenterX() {return x+diameter/2;}
public double getCenterY() {return y+diameter/2;}

public void turn(){
}


public void move()
{


if (collide)
{
double xvect=coll_x-getCenterX();
double yvect=coll_y-getCenterY();

if (xinc==0 && xvect > 0)
{yinc=-yinc;
xinc=-xvect;
}

else if (xinc==0 && xvect > 0)
{yinc=-yinc;
xinc=xvect;
}

else if((xinc>0 && xvect>0) || (xinc<0 && xvect<0)){
xinc=-xinc;
}

if (yinc==0 && yvect > 0)
{xinc=-xinc;
yinc=-yvect;
}

else if (yinc==0 && yvect > 0)
{xinc=-xinc;
yinc=yvect;
}

else if((yinc>0 && yvect>0) || (yinc<0 && yvect<0))
{yinc=-yinc;
}

collide=false;
}

x+=xinc;
y+=yinc;

// sound1=getAudioClip(getDocumentBase(),"Audio1.au");
//when the ball bumps against a boundary, it bounces off
if (x==0)
{
yinc=-yinc;
y+=yinc;
thread.playSound();
}

else if(x<6 || x>width-diameter)
{
xinc=-xinc;
x+=xinc;
thread.playSound();
}

if (y==0)
{
xinc=-xinc;
x+=xinc;
thread.playSound();
}
else if(y<6 || y>height-diameter)
{
yinc=-yinc;
y+=yinc;
thread.playSound();
}

//cast ball coordinates to integers
int x=(int)this.x;
int y=(int)this.y;

//button for red is pushed
if (turn){
xinc=-xinc;
yinc=-yinc;

turn=false;
}

}

public void hit(CollideBall b)
{
if(!collide)
{
coll_x=b.getCenterX();
coll_y=b.getCenterY();
collide=true;
}
}


public void paint(Graphics gr)
{
g=gr;
g.setColor(color);
//the coordinates in fillOval have to be int, so we cast
//explicitly from double to int
g.fillOval((int)x,(int)y,diameter,diameter);
}

}

public class A3 extends JApplet implements Runnable
{
Thread runner;
Image Buffer;
Graphics gBuffer;
CollideBall ball[];
Button redButton;
AudioClip sound1, sound2;
public boolean turn;

private Container container;
//how many balls?
static final int MAX=3;

//int xtemp,ytemp,startx,starty;
//int west, north, east, south;

public void init()
{


container = getContentPane();
container.setLayout(new BorderLayout()); // setting border layout in APPLET

Buffer=createImage(size().width,size().height);
gBuffer=Buffer.getGraphics();

redButton = new Button("Turn Red Circle");
redButton.setBackground(Color.red);

redButton.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e )
{//OptionPane.showMessageDialogue(null,"you pressed the button!");
//sound1.play();
turn=true;

} } );

container.add(redButton,BorderLayout.SOUTH);

ball=new CollideBall[MAX];

int w=size().width-5;
int h=size().height-5;

sound1=getAudioClip(getDocumentBase(),"Audio1.au");
sound2=getAudioClip(getDocumentBase(),"Audio2.au");

//our balls have different start coordinates, increment values
//(speed, direction) and colors
ball[0]=new CollideBall(w,h,50,20,0.0, 2.0,Color.BLUE, this);
ball[1]=new CollideBall(w,h,60,210,2.0,-3.0,Color.GREEN, this);
ball[2]=new CollideBall(w,h,25,50,-4.0,3.5,Color.red, this); //one that needs to be modified

}

public void start()
{
if (runner == null)
{
runner = new Thread (this);
runner.start();
}
}

public void stop()
{
if (runner != null)
{
runner.stop();
runner = null;
}
}




public void run()
{
while(true)
{
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

try {runner.sleep(15);}
catch (Exception e) { }

//move our balls around
for(int i=0;i<MAX;i++)
ball[i].move();

handleCollision();

repaint();
}
}


boolean collide(CollideBall b1, CollideBall b2)
{
double wx=b1.getCenterX()-b2.getCenterX();
double wy=b1.getCenterY()-b2.getCenterY();


//we calculate the distance between the centers two
//colliding balls (theorem of Pythagoras)
double distance=Math.sqrt(wx*wx+wy*wy);

if(distance<b1.diameter)
{sound2.play();
return true; }

return false;
}



public boolean getTurn()
{
return true;
}

public void playSound()
{
sound1.play();
}

private void handleCollision()
{
//we iterate through all the balls, checking for collision
for(int i=0;i<MAX;i++) {

for(int j=0;j<MAX;j++)
{
if(i!=j)
{
if(collide(ball[i], ball[j]))
{

ball[i].hit(ball[j]);
ball[j].hit(ball[i]);
}
}
}
}
}

public void update(Graphics g)
{
paint(g);
}

public void paint(Graphics g)
{
gBuffer.setColor(Color.WHITE);
gBuffer.fillRect(0,0,size().width,size().height);

for(int x=0;x<size().width;x+=50)
gBuffer.drawLine(x,0,x,size().height);

for(int y=0;y<size().height;y+=50)
gBuffer.drawLine(0,y,size().width,y);


//paint our balls
for(int i=0;i<MAX;i++)
ball[i].paint(gBuffer);

g.drawImage (Buffer,0,0, this);
}


}
[7676 byte] By [moxy] at [2007-11-11 7:25:32]