• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can I Move Rectangle?

 
Vas Miriyala
Ranch Hand
Posts: 114
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic