大家好,我正在尝试创建一个程序,该程序接受用户的命令以在屏幕上绘制形状。该程序应具有绘制图形的基本命令和清除屏幕后的清除命令,用户应能够在文本框中键入moveTo,并且笔应将其设置为的坐标移动到该坐标上,然后用户应能够键入他们想要绘制的形状(圆(半径),矩形(宽度,高度),三角形(侧面,侧面,侧面)。我想让它适用于矩形,但是要努力实现其他零件可以任何人的帮助。
public partial class Form1 : Form
{
String input;
Bitmap drawOutput;
String command, command2, command3;
int x, y;
public Form1()
{
InitializeComponent();
drawOutput = new Bitmap(OutputBox.Size.Width, OutputBox.Size.Height);
OutputBox.Image = drawOutput;
}
private void Form1_Load(object sender, EventArgs e)
{
Graphics g;
g = Graphics.FromImage(drawOutput);
Pen mypen = new Pen(Color.Black);
g.Clear(Color.White);
g.Dispose();
}
private void CMDBox_TextChanged(object sender, EventArgs e)
{
input = CMDBox.Text;
}
private void ExecuteBtn_Click(object sender, EventArgs e)
{
string[] spilt = input.Split(' ');
foreach (String words in spilt)
{
command = spilt[0];
command2 = spilt[1];
command3 = spilt[2];
x = Int32.Parse(command2);
y = Int32.Parse(command3);
Graphics g;
g = Graphics.FromImage(drawOutput);
Pen pen = new Pen(Color.Black, 5);
if (command == "circle")
{
g.DrawEllipse(pen, 0, 0, x, y);
setImage(g);
}
else if (command == "rectangle")
{
g.DrawRectangle(pen, 0, 0, x, y);
setImage(g);
}
}
}
public void setImage(Graphics g)
{
g = Graphics.FromImage(drawOutput);
OutputBox.Image = drawOutput;
g.Dispose();
}
}
此程序的基本上下文是创建一种真正的基本编程语言,该语言需要用户输入并在屏幕上显示它
您无需声明类级别的位图(drawOutput)或从其创建Graphics对象。您需要在Paint事件处理程序中进行绘制。尝试以下操作:
enum Shapes
{
Ellipse,
Rectangle,
Line
}
int x, y, w, h;
Shapes shape;
var cmd = textBox1.Text.Split(' ');
var validCmds = Enum.GetNames(typeof(Shapes));
if (cmd.Length < 5 || !validCmds.Contains(cmd[0], StringComparer.OrdinalIgnoreCase))
{
MessageBox.Show("Enter a valid command.");
return;
}
if(!int.TryParse(cmd[1], out x) ||
!int.TryParse(cmd[2], out y) ||
!int.TryParse(cmd[3], out w) ||
!int.TryParse(cmd[4], out h))
{
MessageBox.Show("Enter a valid shape");
return;
}
shape = (Shapes)validCmds.ToList().FindIndex(a => a.Equals(cmd[0], StringComparison.OrdinalIgnoreCase));
OutputBox.Invalidate();
private void OutputBox_Paint(object sender, PaintEventArgs e)
{
if (w > 0 && h > 0)
{
e.Graphics.Clear(Color.White);
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
using (Pen pn = new Pen(Color.Black, 5))
{
switch (shape)
{
case Shapes.Ellipse:
e.Graphics.DrawEllipse(pn, x, y, w, h);
break;
case Shapes.Rectangle:
e.Graphics.DrawRectangle(pn, x, y, w, h);
break;
case Shapes.Line:
e.Graphics.DrawLine(pn, x, y, w, h);
break;
}
}
}
}
更好的是,Shape类方法,如下所述。
public class Shape
{
#region enums.
public enum Shapes
{
Ellipse,
Rectangle,
Line
}
#endregion
#region Constructors
public Shape()
{ }
public Shape(Shapes shape, int x, int y, int width, int height)
{
ThisShape = shape;
X = x;
Y = y;
Width = width;
Height = height;
}
#endregion
#region Properties
public Shapes ThisShape { get; set; } = Shapes.Ellipse;
public int X { get; set; }
public int Y { get; set; }
public int Width { get; set; }
public int Height { get; set; }
#endregion
#region Methods
public static bool TryParse(string input, out Shape result)
{
result = null;
if (string.IsNullOrEmpty(input))
return false;
var cmd = input.Split(' ');
var validCmds = Enum.GetNames(typeof(Shapes));
if (cmd.Length < 5 || !validCmds.Contains(cmd[0], StringComparer.OrdinalIgnoreCase))
return false;
int x, y, w, h;
if (!int.TryParse(cmd[1], out x) ||
!int.TryParse(cmd[2], out y) ||
!int.TryParse(cmd[3], out w) ||
!int.TryParse(cmd[4], out h))
return false;
if (w <= 0 || h <= 0)
return false;
var shape = (Shapes)validCmds.ToList().FindIndex(a => a.Equals(cmd[0], StringComparison.OrdinalIgnoreCase));
result = new Shape(shape, x, y, w, h);
return true;
}
#endregion
}
Shape shape;
shape = null;
if (!Shape.TryParse(textBox1.Text, out shape))
{
MessageBox.Show("Enter a valid command.");
return;
}
OutputBox.Invalidate();
private void OutputBox_Paint(object sender, PaintEventArgs e)
{
if (shape != null)
{
e.Graphics.Clear(Color.White);
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
using (Pen pn = new Pen(Color.Black, 5))
{
switch (shape.ThisShape)
{
case Shape.Shapes.Ellipse:
e.Graphics.DrawEllipse(pn, shape.X, shape.Y, shape.Width, shape.Height);
break;
case Shape.Shapes.Rectangle:
e.Graphics.DrawRectangle(pn, shape.X, shape.Y, shape.Width, shape.Height);
break;
case Shape.Shapes.Line:
//Note: the Width and Height properties play here the X2 and Y2 roles.
e.Graphics.DrawLine(pn, shape.X, shape.Y, shape.Width, shape.Height);
break;
}
}
}
}