This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes How can I Move Rectangle? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How can I Move Rectangle?" Watch "How can I Move Rectangle?" New topic
Author

How can I Move Rectangle?

Vas Miriyala
Ranch Hand

Joined: Sep 14, 2009
Posts: 114

Here is the code I have written and I am confused to use move method and can anybody tell me how can i move rectangle
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
As written, your rectangle locations are hard-coded with int literals, and so if you change some variables x and y, when these variables aren't being used to draw the rectangles it shouldn't be surprising that the rectangles don't "move". Solution: use your x and y variables in your Graphics#drawRect method calls and your rectangles will be movable.
Vas Miriyala
Ranch Hand

Joined: Sep 14, 2009
Posts: 114

Thanks for your help I made changes to the program then also its not moving and its keep on blinking at same place and not at all moving..
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
You have some bigger problems in your code it seems. For one, you're creating a Rectangle object within the paint method, something you shouldn't be doing, and then you don't do anything with the Rectangle object. Then I'm not sure what your if (x) does, and in fact I'm surprised that it even compiles. You are also calling repaint within a paint method which is not a good idea.

I can point you to some tutorials on how to paint in Swing, but I don't know of any personally for AWT. Must this be an AWT application?
Vas Miriyala
Ranch Hand

Joined: Sep 14, 2009
Posts: 114

thanks for your reply Friend its running problem is if(x),I have changed x value to some integer value its working fn and I am completely new to AWT/Swing Programming..okay suggest some good materials to follow,
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
srinivas miriyala wrote:thanks for your reply Friend its running problem is if(x),I have changed x value to some integer value its working fn and I am completely new to AWT/Swing Programming..okay suggest some good materials to follow,


Here are some decent tutorials:
Painting Custom Painting in Swing

Painting in AWT and Swing

To move things in Swing, I usually use a Swing Timer: How to use a Swing Timer
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I Move Rectangle?
 
Similar Threads
Method call from mouseClicked
Need help on two compile errors
Weird error not sure what Im doing wrong
ArrayIndexOutOfBoundsException
Using interface as a type