mattscotney Posted July 9, 2003 Posted July 9, 2003 (edited) 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: 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: '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? 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. Edited July 9, 2003 by mattscotney Quote
*Experts* Nerseus Posted July 9, 2003 *Experts* Posted July 9, 2003 Since they're overlapping you'll 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 you'd have to do some work to find a point inside the rectangle that didn't 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 Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
*Experts* Volte Posted July 9, 2003 *Experts* Posted July 9, 2003 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 don't want to redraw the second rectangle? It'd certainly be easiest. Quote
mattscotney Posted July 9, 2003 Author Posted July 9, 2003 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? Quote
*Experts* Nerseus Posted July 9, 2003 *Experts* Posted July 9, 2003 I would think using a layered approach is the best you'll 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 Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.