Results 1 to 7 of 7
- 04-15-2012, 05:59 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 4
- Rep Power
- 0
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
- 04-15-2012, 06:21 AM #2
Senior Member
- Join Date
- Apr 2012
- Posts
- 199
- Rep Power
- 0
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
- 04-15-2012, 06:24 AM #3
Senior Member
- Join Date
- Apr 2012
- Posts
- 199
- Rep Power
- 0
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).
- 04-15-2012, 11:44 AM #4
Senior Member
- Join Date
- Oct 2011
- Posts
- 106
- Rep Power
- 0
Re: Moving a rectangle with a button click
you need to create a swing Timer like:
Your class must implement the interface ActionListenerJava Code:Timer time = new Timer(40, this);
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(); }this scenario would redraw the rectangle 50 pixels to the right every 40 milliseconds.Java Code:public void paint(Graphics g) { g.drawRect(dx, 100, 50, 50); }
hope it helps!
- 04-15-2012, 12:01 PM #5
Senior Member
- Join Date
- Oct 2011
- Posts
- 106
- Rep Power
- 0
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();
- 04-15-2012, 12:02 PM #6
Re: Moving a rectangle with a button click
Why do they call it rush hour when nothing moves? - Robin Williams
- 04-15-2012, 12:03 PM #7
Similar Threads
-
How to make object stop moving with mouse click
By mackavelirip in forum New To JavaReplies: 3Last Post: 05-06-2011, 03:23 AM -
Button click GUI question
By ZambonieDrivor in forum New To JavaReplies: 2Last Post: 11-29-2010, 07:48 AM -
Mouse Click changes Color of Rectangle
By DocIcarus in forum New To JavaReplies: 1Last Post: 11-23-2010, 01:53 PM -
Cant figure it out (Moving a drawing in JPanel via a button).
By CAD in forum New To JavaReplies: 4Last Post: 09-25-2010, 09:03 AM -
How can I display on Button click?
By ntagrafix in forum New To JavaReplies: 3Last Post: 11-04-2009, 12:05 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks