This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Rectangle Not coming+swing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Rectangle Not coming+swing" Watch "Rectangle Not coming+swing" New topic
Author

Rectangle Not coming+swing

Vas Miriyala
Ranch Hand

Joined: Sep 14, 2009
Posts: 114

I got this program from herewebpage ,Window is coming but Rectangle is not showing in a winodow
here is the code

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
It works fine for me, but I would suggest that when drawing on JComponents and anything that is derived from this, you override paintComponent, not paint. For example:
Vas Miriyala
Ranch Hand

Joined: Sep 14, 2009
Posts: 114

Thanks a lot ,now its working fine
Vas Miriyala
Ranch Hand

Joined: Sep 14, 2009
Posts: 114

I have one doubt? that is if i want to move a small rectangle along with x-axis,what am i suppose to do? any idea?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
srinivas miriyala wrote:I have one doubt? that is if i want to move a small rectangle along with x-axis,what am i suppose to do? any idea?


Right now, because you draw the rectangle using int literals (10, 10, 200, 200), it is hard-coded to have its left upper corner located at [10, 10], relative to the JComponent it sits in. If you want to move the rectangle, you're along the x axis, you're going to have to use a variable for the first parameter of drawRect, change that variable in a Swing Timer, and call repaint on the MyCanvas object after changing the variable.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Rectangle Not coming+swing