Created
          November 20, 2014 12:04 
        
      - 
      
 - 
        
Save TheBeachMaster/5d47c9efed8ff2159dda to your computer and use it in GitHub Desktop.  
    Ball bouncing off screen applet
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | package bouncingball; | |
| import java.awt.*; | |
| import java.applet.*; | |
| import java.util.Timer; | |
| import java.util.TimerTask; | |
| public class BouncingBallApplet extends Applet { | |
| /** | |
| * | |
| */ | |
| private static final long serialVersionUID = 1L; | |
| Timer timer; | |
| int [] ball={10,10}; | |
| int [] vel={2,5}; | |
| int refreshrate = 150; | |
| int size = 200; | |
| int rect_left =5; | |
| int rect_right=size+rect_left; | |
| int rect_top=5; | |
| int rect_bottom=size+rect_top; | |
| public void init() { | |
| setSize(300,300); | |
| timer =new Timer(); | |
| timer.schedule(new TimerTask(){ | |
| public void run() { | |
| ballBounce(); | |
| repaint(); | |
| } | |
| } , 0,refreshrate);} | |
| public void ballBounce(){ | |
| ball[0]=ball[0]+vel[0]; | |
| ball[1]=ball[1]+vel[1]; | |
| if(ball[0]<=rect_left) | |
| {vel[0]=-vel[0];} | |
| if(ball[0]>=rect_right-20)//makes the ball fit into the screen | |
| {vel[0]=-vel[0];} | |
| if(ball[1]<=rect_top) | |
| {vel[1]=-vel[1];} | |
| if(ball[1]>=rect_bottom-20) | |
| {vel[1]=-vel[1];} | |
| } | |
| public void paint(Graphics g){ | |
| g.setColor(Color.black); | |
| g.drawRect(rect_left, rect_top, size, size); | |
| g.setColor(Color.BLUE); | |
| g.fillOval(ball[0], ball[1], 20, 20); | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment