画线&类中的厚度:方法组/ OverLoad问题

时间:2014-04-19 04:20:30

标签: c# class line draw overloading

我正在尝试为Line类设置代码,除了我的方块和圆圈的DefaultLineThickness之外,还有它自己的厚度。尽管我想使用g.DrawLine(Pens.Black,25,40,126,126);它需要在它自己的类中并继承我已经完成的其他形状。我发现这个话题最接近我的问题,但它在XAML和其他地方只是简单的g.DrawLine

http://www.c-sharpcorner.com/uploadfile/mahesh/line-in-wpf/

尝试用圆形,正方形和直线制作粗制自行车。

*编辑 经过足够多次的游戏,我正处于目标的边缘。我唯一的问题是,在插入所有内容并将线条粗细设置为x-number之后,由于“方法组”,它会给我一个错误,因为它无法分配数字

Form1代码

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

namespace Bicycle
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;





        LineOne l1 = new LineOne(new PointF(50, 40));
        l1.setFilledState(false);
        l1.setLineColor(Color.Black);
        l1.setLineThickness = (6);
        //cannot assign to 'setLineThickness' because it is a method group'
        l1.Draw(g);


        sRectangle r2 = new sRectangle(new PointF(151, 160));
        r2.setFilledState(true);
        r2.setLineColor(Color.Green);
        r2.setFilledColor(Color.Honeydew);
        r2.Draw(g);

        sRectangleEmpty r1 = new sRectangleEmpty(new PointF(150, 150));
        r1.setFilledState(false);
        r1.setLineColor(Color.Blue);
        r1.Draw(g);

        sCircle c1 = new sCircle(new PointF(180, 130));
        c1.setFilledState(true);
        c1.setLineColor(Color.Orange);
        c1.setFilledColor(Color.Ivory);
        c1.Draw(g);

        sCircleEmpty c2 = new sCircleEmpty(new PointF(120, 130));
        c2.setFilledState(false);
        c2.setLineColor(Color.Black);
        c2.Draw(g);




    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

}
}

线路代码

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

namespace Bicycle
{
class LineOne : sRectangle

{


    private void setDrawingAttributes()
    {
        const int lenght = 10;

        Pen SmallPen = new Pen(Brushes.DeepSkyBlue);

        SmallPen.LineJoin = System.Drawing.Drawing2D.LineJoin.Bevel;

        PointF p1 = PointF.Add(location, new Size(-lenght / 2, 0));

    }

    private void init()
    {
        setDrawingAttributes();
    }

    public LineOne()
    {
        init();
    }


    public LineOne(PointF p)
        : base(p)
    {
        init();
    }




    public override void Draw(Graphics g)
    {
        g.DrawEllipse(pen, rect);
    }





}
}

形状代码

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

namespace Bicycle
{
class Shape
{
    private Color DefaultLineColor = Color.Black;
    private Color DefaultFillColor = Color.Blue;
    private float DefaultLineThickness = 2;
    protected bool bOutLine;
    protected bool bFilled;
    protected Pen pen;
    protected Brush brush;
    protected PointF location;

    private void setDrawingAttributes()
    {
        pen = new Pen(DefaultLineColor, DefaultLineThickness);
        brush = new SolidBrush(DefaultFillColor);
    }

    private void init()
    {
        bOutLine = true;
        setDrawingAttributes();
    }

    public Shape()
    {
        init();
    }

    public Shape(PointF p)
    {
        location = p;
        init();
    }

    public Color getFillColor()
    {
        return (DefaultFillColor);
    }

    public bool getFilledState()
    {
        return (bFilled);
    }

    public Color getLineColor()
    {
        return (DefaultLineColor);
    }

    public float getLineThickness()
    {
        return (DefaultLineThickness);
    }

    public bool getOutLineState()
    {
        return (bOutLine);
    }

    public bool isOutLine()
    {
        return (bOutLine);
    }

    public bool isFilled()
    {
        return (bFilled);
    }

    public void setFilledColor(Color C)
    {
        DefaultFillColor = C;
        setDrawingAttributes();
    }

    public void setLineColor(Color C)
    {
        DefaultLineColor = C;
        setDrawingAttributes();
    }

    public void setLineThickness(float value)
    {
        DefaultLineThickness = value;
        setDrawingAttributes();
    }


    public void setFilledState(bool value)
    {
        bFilled = value;
    }

    public void setOutLineState(bool value)
    {
        bOutLine = value;
    }



}



}

2 个答案:

答案 0 :(得分:1)

setLineThickness是一个方法,而不是属性。

l1.setLineThickness = (6);更改为l1.setLineThickness(6);(即删除等号)

答案 1 :(得分:1)

找到它后,在审阅了一篇关于Overloads的文章后,我的行代码应该是

public override void Draw(Graphics g)
{

    g.DrawLine(pen, new Point(0, 0), new Point(120, 95));

}

当我使用l1.setLineThickness(1)玩时,我现在得到不同的线条笔划;在我的表格代码中。

感谢你帮助Mr.Williams关于LineThickness的澄清。