Make a Line Class, Draw a Line Class & inheritance

aleeterninja

New member
Joined
Apr 18, 2014
Messages
1
Programming Experience
Beginner
I'm currently stuck in a exercise I'm doing and I fear I may be over thinking this, I'm trying to set up the code for a Line class and have two other Line classes each with their own thickness but can't think of anyother way besides g.DrawLine(Pens.Black, 50, 40, 126, 211); and because from how I have my code set up from class with having the DefaultLineThickness it won't cut it.




I've researched but found nothing but simple examples such as the one given and played around different methods. If anyone could help me out it'd be much appreciative. This is my current stand point.


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


        }
        
    }
}


Shape
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/Empty
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);
            }




           
        
    }
}


sCircle/Empty
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);
        }








    }
}
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,272
Location
Sydney, Australia
Programming Experience
10+
A class representing a line may know how to draw a line but it doesn't know where to draw it. To draw a line or anything using GDI+ you have to use a Graphics object and that object has to come from either the control or image that you want to draw on. If I was creating a Line class then it would something like this:
public class Line
{
    // ...

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

    // ...
}
You can then draw the same line on multiple controls and/or images.
 
Top Bottom