当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Draw Line & Thickness in a Class: method group / OverLoad issue

问题描述:

I'm trying to set up the code for a Line class with it's own thickness besides the DefaultLineThickness I have for my squares and circles. As much as I would like to use g.DrawLine(Pens.Black, 25, 40, 126, 126); it needs to be in it's own class and inherited which I've done with the other shapes. I found this topic closest to my issue but its in XAML and anywhere else it's just simple g.DrawLine

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

Trying to make a crude bike out of circles, squares, and lines.

*edit

After enough times of playing around I'm on the brink of my goal. My only problem is that after I plug everything in and set the line thickness to x-number, it's giving me an error that it can't assign the number because of a 'method group'

Form1 Code

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)

{

}

}

}

The Line Code

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);

}

}

}

The Shape Code

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;

}

}

}

网友答案:

setLineThickness is a method, not a property.

Change l1.setLineThickness = (6); to l1.setLineThickness(6); (i.e. remove the equals sign)

网友答案:

Found it, after reviewing a article in regards of Overloads my Line code should had been

public override void Draw(Graphics g)
{

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

}

I'm now getting different strokes of lines when I play with the l1.setLineThickness(1); in my form code.

Thank you for your assist Mr.Williams on the clarification about LineThickness.

分享给朋友:
您可能感兴趣的文章:
随机阅读: