Shape fill color

mattscotney

Member
Joined
Jul 9, 2003
Messages
13
Hi all,

How do I change the fill color of a rectangle which is overlapped by a second different colored rectangle without having to redraw the second rectangle, please see example below:

First I create a graphics surface to draw on:
Code:
Dim gfxSUR As Graphics
gfxSUR = bgPICTBOX.CreateGraphics

Create a new solid brush
Dim mybrush = New SolidBrush(bgPICTBOX.BackColor)

Create a permanent bitmap surface to draw on
Dim bmp = New Bitmap(bgPICTBOX.Width, bgPICTBOX.Height)
bgPICTBOX.Image = bmp
gfxSUR.Clear(bgPICTBOX.BackColor)
Dim gfx = Graphics.FromImage(bmp)
gfx.fillrectangle(mybrush, 0, 0, 300, 300)

Next I add two overlapping rectangles filled with different colors to the graphics surface:
Code:
Fill first rectangle
Dim mybrush = New SolidBrush(Color.BlueViolet)
gfx.fillrectangle(mybrush, 50, 50, 80, 80)

Fill second rectangle
Dim mybrush2 = New SolidBrush(Color.LemonChiffon)
gfx.fillrectangle(mybrush4, 70, 70, 80, 80)

How can I change the fill color of the first rectangle without refilling over the second rectangle or having to redraw the second rectangle?

[Broken External Image]:http://www.freewebs.com/mattscotney/combined.gif
Not sure why this image wont show up, it worked on the preview! to see what I mean graphically(Its only 3kb) visit http://www.freewebs.com/mattscotney/combined.gif

Is it possible to access or create a backColor property for the rectangle?

I have been banging my head against the wall for days with this one, so any help is greatly appreciated.
 
Last edited by a moderator:
Since theyre overlapping youll have to draw them both again. You might be able to use a FloodFill type of function, but it would be much slower than just drawing both rectangles, plus youd have to do some work to find a point inside the rectangle that didnt overlap the 2nd one. Also, if you have other shapes on the screen the work gets more complicated. Similarly, you could code the logic to break apart the original rectangle into 2 or 3 smaller rectangles and draw them. But it would be greatly complicated if you were talking about more than just 2 rectangles.

-Nerseus
 
You could draw two different rectangles around the outside of the green one (long, narrow ones) to cover the orange parts.

Is there a reason you dont want to redraw the second rectangle? Itd certainly be easiest.
 
I would prefer not to have to redraw the second rectangle because in the final program there will be 10 or so overlapping shapes. The user will draw the shapes. I would prefer the user to be able to change the color of particular shapes without me having to redraw all their shapes.

Does the hidden portion of the first rectangle still exsist when it is overdrawn by the second?

Surely there is some way that I can create a shape with a backColor property (like in VB6)?

If it was only rectangles I would use the label component, but there will be irregular shapes and rotated rectangles.

Or maybe use some sort of layers, like in photoshop or fireworks?

Also is it possible to move shapes around a drawing surface without having to redraw?

I suppose what I am really after is to be able to manipulate vector graphics the way that the graphics programs like Photoshop and Fireworks do. There must be a better, more logical way to do this?
 
I would think using a layered approach is the best youll get. It sounds like you want to maintain the original shape (say a rectangle) even if another shape is on top. You may want something more advanced but a simple Stack or ArrayList will work for storing your shapes. As one changes (shape or color), just redraw all of the objects in order.

-Nerseus
 
Back
Top