Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have some Graphical Classes whose public member are manipulated by user at runtime.

I want to access these class through polymorphism. The classes public parameters will differ (i.e; some properties match such as width, height , etc and others donot match)

The following is my code so far. Please help me throughly.

C#
public interface GraphicsObject
    {
        int Width { get; set; }
        int Height { get; set; }

        int Repetation { get; set; }
        int RepetationMin { get; set; }
        int RepetationMax { get; set; }
        bool IsRepetationRandom { get; set; }

        bool IsTop { get; set; }
        bool IsBtm { get; set; }
        bool IsOffset { get; set; }

        LineType LineType { get; set; }
        bool IsLineTypeRandom { get; set; }

        Shapes Shapes { get; set; }
        bool IsShapesRandom { get; set; }

        GraphicsPath[] Draw();
    }


My Graphical Classes:
C#
public class FallingLines : GraphicsObject
    {
        #region Properties
        public int Width { get; set; }
        public int Height { get; set; }

        public int Repetation { get; set; }
        public int RepetationMax { get; set; }
        public int RepetationMin { get; set; }
        public bool IsRepetationRandom { get; set; }

        public bool IsTop { get; set; }
        public bool IsBtm { get; set; }
        public bool IsOffset { get; set; }

        public LineType LineType { get; set; }
        public bool IsLineTypeRandom { get; set; }
        #endregion

        #region Constructor
        public FallingLines(int width, int height)
        {
            this.Width = width;
            this.Height = height;

            this.Repetation = 10;
            this.RepetationMin = 8;
            this.RepetationMax = 10;
            this.IsRepetationRandom = true;

            this.IsTop = true;
            this.IsBtm = false;
            this.IsOffset = true;

            this.LineType = GraphicalDesigns.LineType.Irregular;
            this.IsLineTypeRandom = false;
        }
        #endregion

        #region Function
        public GraphicsPath[] Draw()
        {
            // does some work
        }

        private Rectangle[] GetRectangle(int randomOffset)
        {
            // does some work
        }

        private GraphicsPath GetPath(int x, int y, int width, int height)
        {
            // does some work
        }

        private Point[] GetPoints(int count)
        {
            // does some work
        }

        private Point[] DrawWave(Point top, Point btm, int cntWave)
        {
            // does some work
        }

        private GraphicsPath DrawIrregularity(Point[] points, int width)
        {
            // does some work
        }

        private Point[] GetRandomPointInLine(Point[] points, int curveCnt)
        {
            // does some work
        }

        private Point[] DrawIrregularWave(Point top, Point btm, int width)
        {
            // does some work
		}
    }

    public enum LineType
    {
        Straight,
        SineCurve,
        Irregular
    }


C#
public class GeometricalShapes : GraphicsObject
    {
        #region Properties
        public int Width { get; set; }
        public int Height { get; set; }

        public int Repetation { get; set; }
        public int RepetationMax { get; set; }
        public int RepetationMin { get; set; }
        public bool IsRepetationRandom { get; set; }

        public bool IsOffset { get; set; }
        
        public Shapes Shapes { get; set; }
        public bool IsShapesRandom { get; set; }
        #endregion

        public GeometricalShapes(int width, int height) 
        {
            this.Width = width;
            this.Height = height;

            this.Repetation = 6;
            this.RepetationMax = 10;
            this.RepetationMin = 4;
            this.IsRepetationRandom = true;

            this.IsOffset = true;

            this.Shapes = GraphicalDesigns.Shapes.Circle;
            this.IsShapesRandom = true;
        }

        public GraphicsPath[] Draw()
        {
            // some work is done
        }
        
        private int[] GetIncBoxSize(int repetation)
        {
            // some work is done
        }

        private GraphicsPath GetPath(int x, int y, int wt, int ht)
        {
            // some work is done
        }
    }

    public enum Shapes
    {
        Triangle,
        Square,
        Rectangle,
        Circle,
        Oval,
        Pentagon,
        Hexagon,
        Octagon
    }


For Controlling through the main Form:
C#
private GraphicsPath[] gPath = null;
        public GraphicsPath[] GPath 
        {
            get { return this.gPath; }
        }

        private PointF[] points = null;
        public PointF[] Points 
        {
            get { return points; }
        }

        public GraphicsMode mode; 

        public int Width { get; set; }
        public int Height { get; set; }

        public int Repetation { get; set; }
        public int RepetationMin { get; set; }
        public int RepetationMax { get; set; }
        public bool IsRepetationRandom { get; set; }

        public bool IsTop { get; set; }
        public bool IsBtm { get; set; }
        public bool IsOffset { get; set; }

        public LineType LineType { get; set; }
        public bool IsLineTypeRandom { get; set; }

        public Shapes Shapes { get; set; }
        public bool IsShapesRandom { get; set; }

        public GraphicsObject gObject;

        public ModelGraphics(int width, int height) 
        {
            this.Width = width;
            this.Height = height;
        }

        public void SetGraphicalObject(GraphicsMode mode)
        {
            this.mode = mode;

            switch (mode)
            {
                case GraphicsMode.GeometricalShape:
                    gObject = new GeometricalShapes(this.Width,this.Height);
                    this.Repetation = gObject.Repetation;
                    this.RepetationMax = gObject.RepetationMax;
                    this.RepetationMin = gObject.RepetationMin;
                    this.IsRepetationRandom = gObject.IsRepetationRandom;
                    this.IsOffset = gObject.IsOffset;
                    this.Shapes = gObject.Shapes;
                    this.IsShapesRandom = gObject.IsShapesRandom;
                    break;

                case GraphicsMode.FallingLine:
                    gObject = new FallingLines(this.Width,this.Height);
                    this.Repetation = gObject.Repetation;
                    this.RepetationMax = gObject.RepetationMax;
                    this.RepetationMin = gObject.RepetationMin;
                    this.IsRepetationRandom = gObject.IsRepetationRandom;
                    this.IsTop = gObject.IsTop;
                    this.IsBtm = gObject.IsBtm;
                    this.IsOffset = gObject.IsOffset;
                    this.LineType = gObject.LineType;
                    this.IsLineTypeRandom = gObject.IsLineTypeRandom;
                    break;
            }
        }

        public void GetGraphicalObject()
        {
            gPath = (GraphicsPath[])gObject.Draw().Clone();
        }


How do i set the values from mainForm to graphical classes and vice-versa.

I know the concept of OOP doesnot match. Please help me understand and help me figure out my structure for ease when i have more graphical classes such as FallingLines and GeometricalShapes
Thankyou
Posted
Updated 15-Apr-12 19:21pm
v4

1 solution

You should not ask how to implement polymorphism. At this moment, this is a totally wrong question. This code shows, that you first need to answer totally different question: "what is polymorphism"? Because right now you didn't even get the requirements for the design of anything polymorphic.

I can tell you why. This is the declaration enum Shapes. Basically, the main idea not only of the polymorphism, but the main idea of whole OOP is this: you never need such declarations. OOP is really much more powerful that you might thing.

So, how to help here? The sooner you forget what you are doing, the better. You need to start from the very beginning and just understand what OOP is all about. And why.

Perhaps, start from here:
http://en.wikipedia.org/wiki/Object-oriented_programming[^],
http://en.wikipedia.org/wiki/Virtual_function[^],
http://en.wikipedia.org/wiki/Abstract_class[^],
http://en.wikipedia.org/wiki/Interface_%28computing%29#Software_interfaces_in_object_oriented_languages[^],
http://en.wikipedia.org/wiki/Dynamic_dispatch[^],
http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29[^].

Main thing here is this:

As Yoda wrote:
Unlearn what you have learned.
—SA
 
Share this answer
 
Comments
VJ Reddy 17-Apr-12 8:00am    
Good links. 5!
Sergey Alexandrovich Kryukov 17-Apr-12 11:18am    
Thank you, VJ.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900