Calling sounds


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 1 of 1

Thread: Calling sounds

  1. #1
    Join Date
    Oct 2005
    Posts
    11

    Help needed on button action - ball angle

    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!

    Code:
    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);				
      	}	
    		
    
    
    }
    Last edited by moxy; 12-03-2005 at 10:10 PM. Reason: Problem solved... another problem

Similar Threads

  1. VB6 vs VB.NET...Procedure Calling Syntax
    By Patrick Troughton in forum .NET
    Replies: 78
    Last Post: 08-13-2002, 12:18 PM
  2. Replies: 0
    Last Post: 02-22-2002, 08:44 AM
  3. Re: Name calling. What name calling?
    By Craig Burkett in forum .NET
    Replies: 0
    Last Post: 09-04-2001, 11:45 PM
  4. syntax for calling stored function
    By Joar Vatnaland in forum Database
    Replies: 1
    Last Post: 06-11-2001, 03:07 AM
  5. Replies: 0
    Last Post: 04-07-2001, 02:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center