Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Swing timer, falling object

  1. #1
    Junior Member
    Join Date
    May 2012
    Thanked 0 Times in 0 Posts

    Default Swing timer, falling object

    I'm trying to simulate a falling object with an initial horizontal speed. I understand how to make it move horizontally (no acceleration) but I have some trouble making it move vertically because of the equation y = gt^2/2 + vt + y0. I have problems because of the quadratic equation.

    What I tried to do is to do make a time variable which would increase by one every time the action is performed by the SwingTimer. So that I would actually have a timevariable. But I don't think that is the best way to do it?

    Can somebody push me in the right direction?

    Below you can find the code I have already written:

    public class Simulation extends JPanel implements ActionListener
        Timer timer = new Timer(5,this);;
        private int Xpos=0, Ypos=0, velX, velY;
        private int Px,Py;
        JButton dropknop;
        private boolean drop = false;
        public Simulation()
            velX = 2;
            velY = 2;
            dropknop = new JButton("DROP");
        public int getXpos() {
            return Xpos;
        public void paintComponent(Graphics g)
            g.drawRect(Xpos, 0, 20, 20);
                g.fillRect(Px, Py, 5, 5);
        public void actionPerformed(ActionEvent ae) 
            if(ae.getSource() == dropknop)
                Px = getXpos();
                this.drop = true;
            if(Xpos<0 || Xpos>986)
                velX = -velX;
            if(Ypos<0 || Ypos>708)
                velY = - velY;
                Px += velY;
                Py += velX;
            Ypos += velY;
            Xpos += velX;

    Thank you in advance!

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,455 Times in 2,406 Posts

    Default Re: Swing timer, falling object

    have you asked this question on a maths forum?
    What are the y values at unit time intervals?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. testing is player is jumping or not jumping. falling or not falling.
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 11th, 2012, 06:07 PM
  2. Swing timer not stopping/starting...
    By hypnotoad in forum AWT / Java Swing
    Replies: 3
    Last Post: October 19th, 2012, 09:01 PM
  3. Timer implementation in swing
    By portem1 in forum Algorithms & Recursion
    Replies: 1
    Last Post: January 19th, 2011, 08:14 AM
  4. Anyone familiar with the swing timer?
    By BigJoe in forum AWT / Java Swing
    Replies: 5
    Last Post: December 28th, 2010, 12:15 PM
  5. Help - Swing Timer, 2 KeyEvents
    By Gheta in forum AWT / Java Swing
    Replies: 2
    Last Post: July 29th, 2009, 02:46 PM

Tags for this Thread