-1
import java.awt.Graphics;
import javax.swing.JApplet;
import javax.swing.JPanel;

public class Circle extends JPanel {

    int x = 75;
    int y = 100;
    int diameter = 50;

    public void setAnimationY(int y) {
        this.y = y;
    }

    public int getAnimationY() {
        return y;
    }

    public int getDiameter() {
        return diameter;
    }

    public void setDiameter(int startDiameter) {
        diameter = startDiameter;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawOval(x, y, diameter, diameter);
    }
}


import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.Timer;

public class BouncingBall extends JApplet {

    private int speed = 5;
    private Timer timer;
    private Circle draw;

    @Override
    public void init() {
        super.init();
        setLayout(new BorderLayout());
        draw = new Circle();
        add(draw);
        timer = new Timer(30, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int y = draw.getAnimationY();
                int diameter = draw.getDiameter();
                int roof = getHeight();
                y += speed;
                if (y < 0) {
                    y = 0;
                    speed *= -1;
                } else if (y + diameter > roof) {
                    y = roof - diameter;
                    speed *= -1;
                }
                draw.setAnimationY(y);
                repaint();
            }
        });
    }

    @Override
    public void start() {
        super.start();
        timer.start();
    }

    @Override
    public void stop() {
        timer.stop();
        super.stop();
    }
}

I am trying to create a JApplet that contains a ball that is bouncing up and down. So far I have been able to get the ball to go up and down but now I am trying to make the ball more "life-like" so I want the height of the ball to decrease each time the ball bounces until eventually it stops.

I have attempted to do a while loop using the roof variable that I created for the getHeight() method but for some reason when I tried to use it either the ball didn't move at all or the loop had no affect on the ball.

I have also tried a for loop but I ran into the same problem that I got into with the while loop. I believe the problem is that I am not placing this for loop in the correct spot for it to work correctly.

thanks in advance.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Please cite your original question on this topic, [Creating an animation using JApplet](http://stackoverflow.com/questions/19648353/creating-an-animation-using-japplet); also, consider a [hybrid](http://stackoverflow.com/a/12449949/230513) applet/applicaiton. – trashgod Oct 29 '13 at 18:36
  • Your roof is not your roof it's your floor. (0,0) coordinates are in top left corner. – DSquare Oct 29 '13 at 19:09

2 Answers2

0

Little modifications to your code that can give you some trails:

            @Override
            public void actionPerformed(ActionEvent e) {
                int y = draw.getAnimationY();
                int diameter = draw.getDiameter();
                int roof = getHeight();
                y += speed;
                //
                // Reduce the ball size at the bottom of the screen
                //
                if(y + diameter > roof) {
                    if(diameter > minDiameter) {
                        diameter -= (roof - y);                 
                    } else {
                        diameter = minDiameter;
                    }                   
                } else if (diameter < maxDiameter) {
                    diameter++;
                }
                draw.setDiameter(diameter);
                if (y < 0) {
                    y = 0;
                    speed *= -1;    
                } else if (y + diameter > roof) {
                    y = roof - diameter;
                    speed *= -1;                    
                }                   
                //  Simulates a little gravity
                speed += 0.5;
                draw.setAnimationY(y);
                repaint();
            }

For more realism, the best way would to find an equation that is function of the ball position and a coefficient of hardness for the ball and would give you the ball size.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
dooxe
  • 1,481
  • 12
  • 17
  • Hmm I didn't even think about change the size of the ball that's pretty cool! That's a good idea! Well for some reason now the ball isn't going up and down. This is the same problem I ran into when I was trying to use the loops for the "lowering roof" – user2926525 Oct 29 '13 at 19:10
0

Well let use continue with @MadProgrammer's solution from your other related question:

In your class of DrawPane we can easily define the height, getAnimationHeight() and setAnimationHeight(int) to control the height decrease as soon as it touches the ground. Please remember that in java left-top co-ordinate is (0, 0) and right-bottom co-ordinate is (getWidth(), getHeight()). Suppose that it starts from height = 0(top). Then it will start from y = height(top) and eventually move to the getHeight()(bottom) of your container. We will increase the height(top y) using setAnimationHeight() by adding an amount(say 30) to current height(which getAnimationHeight() will return) .

So, this little tweak made to @MadeProgrammer's solution in your other question will be the following demo.

enter image description here

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

public class Circle extends JApplet {

    private int delta = 8;

    private Timer timer;
    private DrawPane drawPane;

    @Override
    public void init() {
        super.init();
        setLayout(new BorderLayout());
        drawPane = new DrawPane();
        add(drawPane);
        timer = new Timer(100, new ActionListener() {

            int frameCount = 0;

            @Override
            public void actionPerformed(ActionEvent e) {
                int y = drawPane.getAnimationY();
                int diameter = drawPane.getDiameter();
                y += delta;
                if (y < drawPane.getAnimationHeight()) {
                    y = drawPane.getAnimationHeight();

                    delta *= -1;
                } else if (y + diameter > getHeight()) {
                    y = getHeight()- diameter;
                    delta *= -1;

                    int animationHeight =  drawPane.getAnimationHeight();
                    animationHeight = animationHeight + (getHeight() - diameter - animationHeight)/2;

                    drawPane.setAnimationHeight(animationHeight);

                    if(animationHeight + diameter + 2 >= getHeight())
                    {
                        System.out.println("true");
                        drawPane.setAnimationY(getHeight() - diameter);
                       repaint();
                        timer.stop();
                        return;
                    }


                }
                drawPane.setAnimationY(y);
               repaint();



            }
        });
    }

    @Override
    public void start() {
        super.start();
        timer.start();
    }

    @Override
    public void stop() {
        timer.stop();
        super.stop();
    }

    public class DrawPane extends JPanel {

        int x = 100;
        int y = 0;
        int diameter = 50;
        int height = 0;


        public void setAnimationX(int x) {
            this.x = x;
        }

        public void setAnimationY(int y) {
            this.y = y;
        }



        public void setAnimationHeight(int h)
        {
            height = h;
        }

        public int getAnimationHeight()
        {
           return  height;
        }

        public int getAnimationX() {
            return x;
        }

        public int getAnimationY() {
            return y;
        }

        public int getDiameter() {
            return diameter;
        }

        public void setDiameter(int startDiameter) {
            diameter = startDiameter;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawOval(x, y, diameter, diameter);
        }
    }
}

NOTE: As soon as it touches the bottom finally, you should stop the Timer to get rid of the flickering of the ball. This task is left as an exercise for you.

Community
  • 1
  • 1
Sage
  • 15,290
  • 3
  • 33
  • 38