DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2005

    Red face Bouncing Ball Applet

    Ok, so I made a simple applet that has a blue ball that bounces around an applet. When the ball encounters the bounds of the applet it bounces appropriately. However, I have a few kinks to work out that I'm a little puzzled about.
    FYI, double buffering isn't one of those kinks- I'll fix that a little later since it's not that important in my opinion.

    Here's the problem:
    If my ball's position is incremented/decremented with an incrementation of 1, than I can easily detect when the ball collides into a wall. However, if my incrementation is, say, by a number 3 or 5, etc the ball will be detected when it encounters the bounds of the applet but will also have 'slipped' a little bit past the bounds of the applet. It 'slips' far enough to be noticeable by the user.

    If I keep my increment number @ 1 than my timer that constantly calls repaint for the applet will have to have a very short delay like 10 or something for the ball to move at what I deem an appropriate speed. To remedy this I can increase my increment number but then I will run into the problem mentioned above.

    Here's the code. Thanks for the input.

    HTML Code:
     *Ball Class
    import java.awt.*;
    public class Ball{
    	private int xPos;
    	private int yPos;
    	//speed at which balls coord's are incremented/decremented
    	private int xIncr = 3;
    	private int yIncr = 3;
    	private Color col;
    	final int size = 25;
    	//size of Applet: 300 x 400
    	static final int WIDTH = 300;
    	static final int HEIGHT = 400;
    	public Ball(int x, int y, Color color){
    		xPos = x;
    		yPos = y;
    		col = color;
    	public Ball(){
    		//starting position
    	public int getXPos(){
    		return xPos;
    	public int getYPos(){
    		return yPos;
    	public void checkBounce(){
    		if(xPos < 0 || xPos > WIDTH - size){
    			//ball hit left or right of applet
    		if(yPos < 0 || yPos > HEIGHT - size){
    			//ball hit top or bottom of applet
    	public void reverseX(){
    		//debugging print statement
    		xIncr = (-1)* xIncr;
    	public void reverseY(){
    		//debugging print statement
    		yIncr = (-1)* yIncr;
    	public void moveBall(Graphics g){
    		g.fillOval(xPos, yPos, size, size);
    		xPos += xIncr;
    		yPos += yIncr;
    		System.out.println("(" + xPos + " , " + yPos + ")");
    HTML Code:
     *Main class
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Main extends JApplet implements ActionListener{
    	Ball ball;
    	Timer timer;
    	final int DELAY = 50;
    	public void init(){
    		//set background color
    		//this.setBackground(new Color(100,0,100));
    		//use default constructor for test
    		ball = new Ball();
    		//constructor Timer(int millidelay, ActionListener l)
    		timer = new Timer(DELAY, this);
    	public void start(){
    	public void stop(){
    	public void destroy(){
    	public void paint(Graphics g){
    	public void actionPerformed(ActionEvent e){
    		if(e.getSource() == timer){
    			//call paint method

  2. #2
    Join Date
    Nov 2004
    When the coordinates tells you that a tick drives a ball through the wall then
    you should do the math for calculating its new position as if it had hit the
    wall at the proper place and bounced back a little. In other worlds; the
    actual impact will not be "on film" for some of these collisions. This is the
    only option when you are using a fixed framerate.
    eschew obfuscation

Similar Threads

  1. JavaMail applet problem
    By Tataroz T. in forum Java
    Replies: 1
    Last Post: 10-20-2005, 06:19 AM
  2. Replies: 3
    Last Post: 08-23-2001, 11:01 AM
  3. applet and thread problems
    By aneesha in forum Java
    Replies: 0
    Last Post: 05-06-2000, 10:38 PM
  4. Applet and thread problems
    By Aneesha in forum Java
    Replies: 0
    Last Post: 05-06-2000, 10:36 PM
  5. Replies: 0
    Last Post: 03-28-2000, 01:52 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
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

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

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.