Reputation: 59
I want to draw graphics (shapes) onto the panel to the top left. The shape will be drawn depending on the shape chosen and the value given by the track bar. The track bar values aren't specific i.e aren't pixels or millimeters, so basically when the track bar increases in number the shape should get larger.
This is the my main code. Other classes such as Circle, Square and Triangle also exist.
public partial class drawShape : Form
{
Graphics drawArea;
public decimal area;
double myBoundary = 0;
double myArea = 0;
public double length = 100;
public drawShape()
{
InitializeComponent();
drawArea = pnlDrawArea.CreateGraphics();
}
public void updateShape()
{
if(rbCircle.Checked)
{
drawCircle();
}
if(rbSquare.Checked)
{
drawSquare();
}
if(rbTriangle.Checked)
{
drawTriangle();
}
if(rb2DecimalPlaces.Checked)
{
lblBoundaryLength.Text = myBoundary.ToString("#,0.00");
lblAreaResult.Text = myArea.ToString("#,0.00");
}
if(rb3DecimalPlaces.Checked)
{
lblBoundaryLength.Text = myBoundary.ToString("#,0.000");
lblAreaResult.Text = myArea.ToString("#,0.000");
}
if(rb4DecimalPlaces.Checked)
{
lblBoundaryLength.Text = myBoundary.ToString("#,0.0000");
lblAreaResult.Text = myArea.ToString("#,0.0000");
}
}
public void drawCircle()
{
Circle myCircle = new Circle(length);
myArea = myCircle.GetArea(length);
myBoundary = myCircle.GetCircumference();
lblAreaResult.Text = myArea.ToString();
lblBoundaryLength.Text = myBoundary.ToString();
}
public void drawSquare()
{
Square mySquare = new Square(length);
myArea = mySquare.GetArea();
myBoundary = mySquare.GetBoundLength(length);
lblAreaResult.Text = myArea.ToString();
lblBoundaryLength.Text = myBoundary.ToString();
}
public void drawTriangle()
{
Triangle myTriangle = new Triangle(length);
myArea = myTriangle.GetArea();
myBoundary = myTriangle.GetBoundLength();
lblAreaResult.Text = myArea.ToString();
lblBoundaryLength.Text = myBoundary.ToString();
}
Upvotes: 0
Views: 2044
Reputation: 54433
You should use the Panel
's Paint
event like this:
private void pnlDrawArea_Paint(object sender, PaintEventArgs e)
{
int offset = 20;
Rectangle bounding = new Rectangle(offset, offset,
(int)myBoundary.Value, (int)myBoundary.Value);
if (rbSquare.Checked)
{
e.Graphics.DrawRectangle(Pens.Red, bounding);
}
else if (rbCircle.Checked)
{
e.Graphics.DrawEllipse(Pens.Red, bounding);
}
// else if...
}
and in your updateShape
simply call the Paint
event by coding: pnlDrawArea.Invalidate();
For the triangle you will
DrawLines
methos and Points
for it Don't forget to hook up the Paint
event!!
Upvotes: 3