What is the easiest way to draw a cube in Windows Forms?
I am making a Windows Forms app in C# for homework that accepts the length of a cube. It then displays the surface area and volu开发者_JS百科me of the cube. That's easy enough but it also needs to draw the cube.
What I'd like to know is the easiest way to draw the cube. I must do this with the Graphics
class.
My thoughts on how to do it so far:
paper = myPicBox.CreateGraphics();
myPen = new Pen(Color.Black);
myPen.Width = 3;
paper.DrawRectangle(myPen, xCoord, yCoord, width, height);
paper.DrawLine(myPen, pointOne, pointTwo); // Then repeat this line for the four lines on the Z-axis
paper.DrawRectangle(myPen, xCoord, yCoord, width, height); // Where xCoord and yCoord have been changed to be placed at the end of the lines I've drawn
This is pretty bulky, so I was wondering if there was an easier or simpler way to achieve the same thing?
As mentioned, that's probably the best you're going to get with WinForms. The best you can do is encapsulate your functionality into its own method such that you can draw more than one cube at once. So your DrawCube() method might take in an origin, length, and Graphics object and then draw it. The CreateGraphics call would come before any calls to DrawCube.
Also, after you are done with your Graphics object, you should dispose of it by calling paper.Dispose() (see thispage) or stick it in a using block. Also, check out this website that explains when to use CreateGraphics (basically when you are drawing outside of a Paint event handler, which you are doing)
In WinForms what you have done is only your best bet, if you are searching for somthing as DrawCube() method then sorry you don't have it in .Net. Any thing you do will involve using those primitive types like line and rectangle.
精彩评论