计算并绘制形状 [英] Calculate and Draw the Shapes

查看:50
本文介绍了计算并绘制形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有一个类可以在窗口窗体应用程序中执行形状程序,但是我不知道如何在Form1.cs中编写代码以显示形状区域并在选择组合框后绘制-圆,三角形, 正方形.单击区域按钮"时单击绘制"时绘制形状,以及如何启用和禁用使用时键入数字的文本框,即圆仅需要填充半径,然后将组合框更改为三角形,需要填充底部和高度.

形状类别如下:

I already have a class to do a shape program in window form application , but I don''t know how to write a code in Form1.cs to show the area of shapes and draw after choosing the combobox - circle , triangle, square. when click the ''Area button'' and draw the shape when click ''draw'' , and how to enable and disable textbox of typing number when in use , ie. Circle needs to fill radius only , then change the combobox to triangle needs to fill base and height.

The Shape Class is below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Class_Shape
{
class Shape
{
public string[] GetShape()
{

string []s = {"circle","triangle","square"};
return s;

}
public double CircleArea(double radius)
{
return Math.PI * radius * radius;
}
public double RectangleArea(double width, double height)
{
return width * height;
}
public double TangleArea(double width,double height)
{
return 0.5 * width * height;
}
public void DrawCircle(double r,Form1 f)
{
Pen big = new Pen(Brushes.Red ,5);
f.Refresh ();
f.CreateGraphics().DrawEllipse(big, (float) 110,(float) 160,(float) r,(float) r);
f.CreateGraphics().FillEllipse(Brushes.YellowGreen , (float)110, (float)160, (float)r, (float)r);
}
public void DrawRect(double w,double h, Form1 f)
{
Pen big = new Pen(Brushes.Red, 5);
f.Refresh();
f.CreateGraphics().DrawRectangle (big, (float)110, (float)160, (float)w, (float)h);
f.CreateGraphics().FillRectangle (Brushes.YellowGreen, (float)110, (float)160, (float)w, (float)h);
}
public void DrawTang(double w, double h, Form1 f)
{
Pen big = new Pen(Brushes.Red, 5);
Point [] p = {new Point(120, 160),new Point(120,160+(int)h), new Point(120+(int)w,160+(int)h) } ;
f.Refresh();
f.CreateGraphics().DrawPolygon(big, p);
f.CreateGraphics().FillPolygon(Brushes.YellowGreen , p);
}
}
}

推荐答案

好,我可以看到您的主要问题:您正试图完全滥用OOP.这是无法修复的.将形状表示为字符串以进行分类仅是对您自己的犯罪.将所有人归为一类是对OOP的滥用,并且将一事无成.您将需要创建一个抽象形状类,并从中派生许多具体的形状类.您需要使用继承和多态性.

您根本不准备解决此类问题.不要浪费时间,不要浪费时间.从头开始学习一些OOP,.NET和C#.这将花费太多文字才能为您提供所有参考.好吧,读一些书.是的,我100%确定.你在做什么是没用的.在熟悉所有技术和概念之前,请先回到该问题,首先像在控制台应用程序上一样,在可能不使用UI的情况下,通过最简单的练习来训练自己.

当您遇到一些特定的问题并能够准确地提出问题时,请回来;我们很乐意为您提供帮助.

—SA
OK, I can see your main problem: you are trying to totally abuse OOP. This is beyond repair. Representing a shape as a string for classification purpose is simply a crime, against yourself. Putting all in one class is an abuse of OOP and will lead you nowhere. You would need to make an abstract shape class and derive a number of concrete shape classes from it. You need to use inheritance and polymorphism.

You are simply not ready to solve such problem. Don''t waste your time, stop it. Learn some OOP, .NET and C# — from scratch. It would take too much text to give you all the reference. Well, read some books. Yes, I''m 100% sure. What are you doing is useless. Don''t get back to this problem until you are familiar with all the techniques and concepts, first train yourself on simplest exercises, probably without UI, just as console applications.

When you face some specific problem and able to pose your problem accurately — come back; we will gladly help you.

—SA


尝试以下链接:

简单的矢量形状

旋翼仪形状:数学公式中的WPF贝塞尔曲线形状
Try this links :

Simple Vector Shapes

Spirograph Shapes: WPF Bezier Shapes from Math Formulae


感谢您的建议,我可以解决程序!

使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;

命名空间Class_Shape
{
公共局部类Form1:Form
{
Shape s = new Shape();


公共Form1()
{
InitializeComponent();
}

私有void Form1_Load(对象发送者,EventArgs e)
{

cbbShape.DataSource = s.GetShape();
}

私有无效btnCal_Click(对象发送者,EventArgs e)
{
//圆

如果(cbbShape.SelectedIndex == 0)
{

txtArea.Text = s.CircleArea(Convert.ToDouble(txt1.Text)).ToString();



}
//三角形

否则(cbbShape.SelectedIndex == 1)
{
txtArea.Text = s.TangleArea(Convert.ToDouble(txt1.Text),Convert.ToDouble(txt2.Text)).ToString();

}
//矩形
否则if(cbbShape.SelectedIndex == 2)
{
txtArea.Text = s.RectangleArea(Convert.ToDouble(txt1.Text),Convert.ToDouble(txt2.Text)).ToString();
}
}

私有void cbbShape_SelectedIndexChanged(object sender,EventArgs e)
{
如果(cbbShape.SelectedIndex == 0)
{
label4.Text ="Radius";
txt2.Visible = false;
lblHeight.Visible = false;
}
否则if(cbbShape.SelectedIndex == 1)
{
label4.Text ="hight";
lblHeight.Visible = true;
txt2.Visible = true;

}
否则(cbbShape.SelectedIndex == 2)
{
label4.Text ="width";
lblHeight.Visible = true;
txt2.Visible = true;

}
}

私有无效btnDraw_Click(对象发送者,EventArgs e)
{
//圆
如果(cbbShape.SelectedIndex == 0)
{
s.DrawCircle(Convert.ToDouble(txt1.Text),this);


}
//三角形
如果(cbbShape.SelectedIndex == 1)
{
s.DrawTang(Convert.ToDouble(txt1.Text),Convert.ToDouble(txt2.Text),此);


}
//矩形

否则(cbbShape.SelectedIndex == 2)
{

s.DrawRect(Convert.ToDouble(txt1.Text),Convert.ToDouble(txt2.Text),此);
}

}


}
}
Thanks for your recommendations,I can solve the program!

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Class_Shape
{
public partial class Form1 : Form
{
Shape s = new Shape();


public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

cbbShape.DataSource = s.GetShape();
}

private void btnCal_Click(object sender, EventArgs e)
{
// Circle

if (cbbShape.SelectedIndex == 0)
{

txtArea.Text = s.CircleArea(Convert.ToDouble(txt1.Text)).ToString();



}
// Triangle

else if (cbbShape.SelectedIndex == 1)
{
txtArea.Text = s.TangleArea(Convert.ToDouble(txt1.Text), Convert.ToDouble(txt2.Text)).ToString();

}
// Rectangle
else if(cbbShape.SelectedIndex == 2)
{
txtArea.Text = s.RectangleArea(Convert.ToDouble(txt1.Text), Convert.ToDouble(txt2.Text)).ToString();
}
}

private void cbbShape_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbbShape.SelectedIndex == 0)
{
label4.Text = "Radius";
txt2.Visible = false;
lblHeight.Visible = false;
}
else if(cbbShape.SelectedIndex == 1)
{
label4.Text = "hight";
lblHeight.Visible = true;
txt2.Visible = true;

}
else if (cbbShape.SelectedIndex == 2)
{
label4.Text = "width";
lblHeight.Visible = true;
txt2.Visible = true;

}
}

private void btnDraw_Click(object sender, EventArgs e)
{
// Circle
if (cbbShape.SelectedIndex == 0)
{
s.DrawCircle(Convert.ToDouble(txt1.Text),this);


}
// Triangle
if (cbbShape.SelectedIndex == 1)
{
s.DrawTang(Convert.ToDouble(txt1.Text),Convert.ToDouble(txt2.Text), this);


}
// Rectangle

else if (cbbShape.SelectedIndex == 2)
{

s.DrawRect(Convert.ToDouble(txt1.Text), Convert.ToDouble(txt2.Text), this);
}

}


}
}


这篇关于计算并绘制形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆