Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# repainting in shapes other than square

Chris Dancy
Ranch Hand
Posts: 136
What I want to do is to repaint a square component but only a portion of it say in a circular shape. Something like.... repaint(10,10,new Circle()) ... I think that gets the point across. I do not want to repaint the entire component just some portion of it in a shape other than repaint(0,0,getWidth(),getHeight()) type of deal.

Christopher Dancy

Rob Camick
Ranch Hand
Posts: 2542
9
The repaint area must be a Rectangle.

Why does this cause a problem? If we know the problem maybe we can provide a solution.

The Graphics2D class support a draw(Shape) and fill(Shape) methods which may help.

Fred Hamilton
Ranch Hand
Posts: 684
Chris Dancy wrote:What I want to do is to repaint a square component but only a portion of it say in a circular shape. Something like.... repaint(10,10,new Circle()) ... I think that gets the point across. I do not want to repaint the entire component just some portion of it in a shape other than repaint(0,0,getWidth(),getHeight()) type of deal.

Christopher Dancy

I'm not sure this is the answer you are looking for, but think about the definition of drawOval and fillOval, and I think you will see that if you replace fillRect by fillOval, and leave the parameters the same then you will get exactly the result you are looking for. Because drawOval and FillOval are expressed relative to the rectangular shape they occupy.

The drawArc and fillArc methods are also relative to the rectangle it would occupy if the arc was drawn a full 360 degrees

http://richardbowles.tripod.com/java/guide/graphics.htm

Does this help?

Chris Dancy
Ranch Hand
Posts: 136
The component is rectangular shaped. What we are playing around with is different effects to transition to the next stage/component within that same area. So my brilliant idea was to, from the center of the component repaint outwardly, with ever increasing in size circles, until the new component is made visible, if that makes sense. As I'm writing this now I'm thinking that maybe by setting a composite of some sort I can get the desired effect.

Fred Hamilton
Ranch Hand
Posts: 684
Chris Dancy wrote:The component is rectangular shaped. What we are playing around with is different effects to transition to the next stage/component within that same area. So my brilliant idea was to, from the center of the component repaint outwardly, with ever increasing in size circles, until the new component is made visible, if that makes sense. As I'm writing this now I'm thinking that maybe by setting a composite of some sort I can get the desired effect.

OK, well if you are just using draw and fill commands to display the transition to the next component, then I'd say you can do what you describe by expressing the parameters of the drawOval command as functions of the co-rdinates of the first rectangular component, and incrementing the draw parameters in a for loop, until you reach the co-ordinates of the rectangle.

But anyways, that's just one way to look at it, I'm sure there are others.

Rob Camick
Ranch Hand
Posts: 2542
9
So my brilliant idea was to, from the center of the component repaint outwardly, with ever increasing in size circles,

Well, its still not a problem using a Rectangle.

There are many different ways to solve the problem. Presumably you have a Timer to schedule the transition. So every time the paintComponent() method is invoked you simply draw a different sized oval. So you would need to keep track of the size of the oval. This could be done by keep a class variable for the current size. Or you could get the size of the Rectangle to be repainted from the Graphics clip bounds.

Chris Dancy
Ranch Hand
Posts: 136
Rob: That's what my initial idea was, however I was not sure if I simply was not aware of some way to hack repaint, maybe a custom paint manager, to get it to repaint in different shapes other than rectangles...like repaint(x,y,width,height) but do something like repaint(0,0,Circle). I know there is no documented way of doing this, I just thought maybe some of you guys might have done this kind of effect before and know an easy way to accomplish it, not that the way Rob has put is hard by any measure. So I'll go with that than. Thanks

Craig Wood
Ranch Hand
Posts: 1535
from the center of the component repaint outwardly, with ever increasing in size circles, until the new component is made visible
If you want all circles/ovals with a common center you could start from the outside with fillOval and move toward the center with different colors and decreasing radius or width/height values.
Other shapes, eg, polygons could be handled the same way.

no documented way of doing this
We do have the Area class that allows complex geometry primitives to be created.
See Constructing Complex Shapes from Geometry Primitives.

Another possibility is to set a clipping shape for filling. The Graphics setClip method takes a Shape argument, eg, Ellipse2D, Path2D, Polygon. You can do this at any location. Make sure you restore the clip to its original value before leaving the painting method.
See Clipping the Drawing Region.

Edit: fix url.

Fred Hamilton
Ranch Hand
Posts: 684
Yep I'd say there's a good half dozen ways to get this job done.

Chris Dancy
Ranch Hand
Posts: 136
By no documented way I meant no way to say repaint( new Triangle()) or something to that effect. I did in fact set the clip as a Circle and simply drew bigger circles until the component was completely redrawn. Thanks for the tips however.