Results 1 to 7 of 7
  1. #1
    vasiqshair is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Moving a rectangle with a button click

    I have been delaying asking for help for a while now but now it just seems undoable. The problem that I am stuck on is to move a rectangle graphic at the click of a button. Upon clicking the "Send" button, the ActionListener attached to the "Send" button should cause a rectangle shape to move from the top of the frame to the bottom. I would really appreciate if you guys steer me in the right direction. Thanks

  2. #2
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Moving a rectangle with a button click

    I'm been trying to do something like this just recently. I'm trying to do it by scheduling a panel.repaint(). My program sets the coordinates of the image with the mouses current position.

    I haven't been able to get it to work yet.

    You may want to take a look at this:
    ScheduledExecutorService – Java program example pradeepwiki

  3. #3
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Moving a rectangle with a button click

    I just read your post again. It seems I misunderstood. But you'll still want to use a timer or scheduling to move the object. At each period you will update the position of the object and then call panel.repaint (or whatever your painting to).

  4. #4
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Moving a rectangle with a button click

    you need to create a swing Timer like:
    Java Code:
    Timer time =  new Timer(40, this);
    Your class must implement the interface ActionListener

    call your rapaint() method in your actionPerformed method.
    you need a variable for your change lets say in x.
    Java Code:
    int dx = 400;
    Java Code:
             public void actionPerformed(ActionEvent arg0)
    	{
                 dx += 50;
                 repaint();
            }
    Java Code:
    public void paint(Graphics g)
    {
        g.drawRect(dx, 100, 50, 50);
    }
    this scenario would redraw the rectangle 50 pixels to the right every 40 milliseconds.
    hope it helps!

  5. #5
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Moving a rectangle with a button click

    I miss read. To do this on button click you would do your dx += 50; in your button click event. I also forgot you need to start the timer:
    Java Code:
    time.start();

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default Re: Moving a rectangle with a button click

    Quote Originally Posted by mwr1976 View Post
    Your class must implement the interface ActionListener
    Wrong. And a gentle nudge in the right direction would have been better than posting code, especially when the person asking the question hasn't posted his/her attempts at a solution.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default Re: Moving a rectangle with a button click

    Quote Originally Posted by mwr1976 View Post
    To do this on button click you would do your dx += 50; in your button click event.
    What's a button click event? Are you sure you're posting advice that pertains to Java?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. How to make object stop moving with mouse click
    By mackavelirip in forum New To Java
    Replies: 3
    Last Post: 05-06-2011, 04:23 AM
  2. Button click GUI question
    By ZambonieDrivor in forum New To Java
    Replies: 2
    Last Post: 11-29-2010, 08:48 AM
  3. Mouse Click changes Color of Rectangle
    By DocIcarus in forum New To Java
    Replies: 1
    Last Post: 11-23-2010, 02:53 PM
  4. Replies: 4
    Last Post: 09-25-2010, 10:03 AM
  5. How can I display on Button click?
    By ntagrafix in forum New To Java
    Replies: 3
    Last Post: 11-04-2009, 01:05 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
  •