Monday, May 24, 2010

C# drawing question (asked in an easier to understand way). A best answer will be awarded.?

I have a class that draws itself.





The class draws a picture of what a node looks like in a B tree. But this takes a lot of method calls.





1. draw top line


2. draw bottom line


3. draw left arc


4. draw right arc


5. draw diving lines inside node


6. draw left rectangle


7. draw left circle


8. draw right rectangle


9. draw right circle


10. draw rectangle between left and right (in a for statement, so if there are 50 data cells this code runs 48 times)


11. draw in numbers on each data cell





etc etc etc





I dont want the computer to have to execute all of that code every time it has to repaint the window (due to it being minimized or another window dragged over top of it, etc).





So I just want to know how programmers usually deal with something like this. Is it okay to have it go through all of that drawing every single time it needs to repaint? Mind you, this will be the drawing of only one node.... there could be hundreds that need to be drawn!





thanks for your help!

C# drawing question (asked in an easier to understand way). A best answer will be awarded.?
You can make a path and save it. I assume that since you are drawinf nodes, that each looks the same. I program in VB.Net, but the code is virtually the same for C#...





Dim path As New GraphicsPath


Dim rectRoundBounds As New Rectangle





path.StartFigure()


path.AddLine(rect.X + Radius, rect.Y, rect.X + rect.Width - 2 * Radius, rect.Y)


path.AddArc(rect.X + rect.Width - 2 * Radius, rect.Y, 2 * Radius, 2 * Radius, 270, 90)


path.AddLine(rect.X + rect.Width, rect.Y + Radius, rect.X + rect.Width, rect.Y + rect.Height - 2 * Radius)


path.AddArc(rect.X + rect.Width - 2 * Radius, rect.Y + rect.Height - 2 * Radius, 2 * Radius, 2 * Radius, 0, 90)


path.AddLine(rect.X + rect.Width - 2 * Radius, rect.Y + rect.Height, rect.X + Radius, rect.Y + rect.Height)


path.AddArc(rect.X, rect.Y + rect.Height - 2 * Radius, 2 * Radius, 2 * Radius, 90, 90)


path.AddLine(rect.X, rect.Y + rect.Height - 2 * Radius, rect.X, rect.Y + Radius)


path.AddArc(rect.X, rect.Y, 2 * Radius, 2 * Radius, 180, 90)


path.CloseFigure()








Then call





g.FillRegion(brushBackground, New Region(path))





or


g.DrawPath(BorderPen, path)
Reply:Hm, I already answered this question elsewhere ;-)


My advice is to use double buffering, or just ignore it. It might not even be a problem unless youve tonnes of them





Remember modern graphics cards can thousands and thousands of polygons per second without even sweating
Reply:a simple and fast solution would be to following: I am assuming that you are writing to a picture box.





Im not going to provide the code since you seem to know what you're doing.





after you draw the image that you want save it as a .bmp to memory. when the screen needs to be refreshed simply draw the .bmp image to the device context (aka the picture box).





good luck

survey

No comments:

Post a Comment