制作一级课程,绘制一个线类& inheritance

Aleeterninja.

新成员
加入
2014年4月18日
消息
1
编程经验
Beginner
我目前陷入了练习我正在做,我担心我可能会过度思考这一点,我正试图为行课设置代码并拥有另外两条线类,每个级别都有自己的厚度,但不能思考除G.Drawline(Pens.Black,50,40,126,211)之外的任何方法;并且因为从我的代码从类设置到具有defaultlinthickness,它不会削减它。




我已经研究过,但没有发现简单的例子,例如给出的那个和播放不同的方法。如果有人能帮助我,那就非常感谢。这是我目前的立场。


Form1
C#:
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;




            
            
           // Making a random thick black line
           LineOne l1 = new LineOne(new PointF(50, 40, 126, 211);
           l1.setFilledState(false);
           l1.setLineColor(Color.Black);
           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)
        {


        }
        
    }
}


形状
C#:
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;
        }


        


    }




    
}


srectangle /空
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;


namespace Bicycle
{
    class sRectangle 
        : Shape
        
    {
            private bool bCenterPivot = true;
            private float width = 20;
            private float height = 20;
            protected RectangleF rect;
            private SizeF size;


            private void initRectangle()
            {
                SizeF offset = new SizeF(size.Width / 2, size.Height / 2);
                PointF p1 = PointF.Subtract(location, offset);
                rect = new RectangleF(p1, size);
            }


            private void init()
            {
                bCenterPivot = true;
                initRectangle();
                setLineColor(Color.DarkBlue);
            }


            public sRectangle()
            {
                size = new SizeF(width, height);
                init();
            }


            public sRectangle(PointF p)
                : base(p)
            {
                size = new SizeF(width, height);
                init();
            }


            public SizeF getSize()
            {
                return (size);
            }


            public void setSize(float value)
            {
                size = new SizeF(value, value);
                initRectangle();
            }


            public void setSize(float W, float H)
            {
                width = W;
                height = H;
                size = new SizeF(width, height);
                initRectangle();
            }


            public virtual void Draw(Graphics g)
            {
                if (bFilled)
                    g.FillRectangle(brush, rect);


                if (bOutLine)
                    g.DrawRectangle(pen, rect.X, rect.Y, rect.Width, rect.Height);
            }




           
        
    }
}


裂缝/空
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;


namespace Bicycle
{
    class sRectangleEmpty
        : Shape
    {
        private bool bCenterPivot = true;
        private float width = 100;
        private float height = 100;
        protected RectangleF rect;
        private SizeF size;


        private void initRectangle()
        {
            SizeF offset = new SizeF(size.Width / 2, size.Height / 2);
            PointF p1 = PointF.Subtract(location, offset);
            rect = new RectangleF(p1, size);
        }


        private void init()
        {
            bCenterPivot = true;
            initRectangle();
            setLineColor(Color.Purple);
        }


        public sRectangleEmpty()
        {
            size = new SizeF(width, height);
            init();
        }


        public sRectangleEmpty(PointF p)
            : base(p)
        {
            size = new SizeF(width, height);
            init();
        }


        public SizeF getSize()
        {
            return (size);
        }


        public void setSize(float value)
        {
            size = new SizeF(value, value);
            initRectangle();
        }


        public void setSize(float W, float H)
        {
            width = W;
            height = H;
            size = new SizeF(width, height);
            initRectangle();
        }


        public virtual void Draw(Graphics g)
        {
            


            if (bOutLine)
                g.DrawRectangle(pen, rect.X, rect.Y, rect.Width, rect.Height);
        }








    }
}
 

jmplhinney.

C#论坛主持人
工作人员
加入
2011年4月23日
消息
3,720
地点
悉尼,澳大利亚
编程经验
10+
代表行的类可能知道如何绘制一条线,但它不知道在哪里绘制它。使用GDI +绘制一行或任何内容+您必须使用图形对象,并且该对象必须来自您要绘制的控件或图像。如果我正在创建一个行课,那么它会这样的:
public class Line
{
    // ...

    public void Draw(Graphics g)
    {
        g.DrawLine(...);
    }

    // ...
}
然后,您可以在多个控件和/或图像上绘制相同的线。
 
最佳 底部