Results 1 to 2 of 2
  1. #1
    ICEpower is offline Member
    Join Date
    Apr 2012
    Rep Power

    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. I also want to plot the graph of the falling object.

    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:
    Java Code:
    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
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default Re: Swing timer, falling object

    You actually want to see your object falling? If so for each time t you have to draw your object at location (xt, yt) where yt is that quadratic equation. You may have to scale 'g' to see a nice parabola.

    kind regards,

    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Having few problems with a swing.timer
    By JayRee in forum New To Java
    Replies: 3
    Last Post: 02-11-2013, 04:39 PM
  2. Thread v swing timer
    By musico in forum New To Java
    Replies: 1
    Last Post: 06-23-2011, 09:51 PM
  3. help with Swing Timer animation
    By tomas1991 in forum New To Java
    Replies: 7
    Last Post: 03-19-2010, 09:06 AM
  4. Swing Timer Questions
    By morfasto in forum New To Java
    Replies: 9
    Last Post: 11-14-2009, 03:48 PM
  5. Timer on swing
    By finzaiko in forum AWT / Swing
    Replies: 3
    Last Post: 04-02-2009, 07:45 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts