Link to home
Start Free TrialLog in
Avatar of mpoots
mpoots

asked on

Basic functionality C# question

Hi,

I have just started work with C# after working many years with Delphi. I am trying to write a simple program to get the feel of C#. In my program I want to draw a Tangram Puzzle. I have a Base class called Shape and classes that inherit from them called Square and Triangle. The base class Shape holds an array of points. When I create a triangle and a Square both will have the same points. Why is that?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TangramWorld;
using Shapes;
 
namespace Tangram
{
    public partial class frmMain : Form
    {
 
        private TangramPuzzle _TangramPuzzle;
 
        public TangramPuzzle Tangram
        {
            get{
                if (_TangramPuzzle == null)
                    _TangramPuzzle = new TangramPuzzle(pbMain);
                return _TangramPuzzle;
            }
        }
        public frmMain()
        {
            InitializeComponent();
        }
 
        private void btnInitialize_Click(object sender, EventArgs e)
        {
            Tangram.Initialize();
        }
    }
}
 
using System;
using Shapes;
using System.Drawing;
using System.Windows.Forms;
 
namespace TangramWorld
{
    public class TangramPuzzle
    {
        private System.Windows.Forms.PictureBox _PictureBox;
        private Boolean _Initialized;
        public static Shape[] ShapeArray;
 
        public TangramPuzzle(System.Windows.Forms.PictureBox aPictureBox)
        {
            _PictureBox = aPictureBox;
        }
 
        public void Paint()
        {
            if (_Initialized)
            {
                /*
                foreach (Shape Shp in ShapeArray)
                {
                    if (Shp != null)
                    {
                        Shp.Paint();
                    }
                }*/
 
                for (int i = 0; i <2; i++)
                {
                    if (ShapeArray[i] != null)
                    {
                        ShapeArray[i].Paint();
                        //MessageBox.Show("First");
                        break;
                    }
                }
            }
            else 
                Initialize();
 
		}
 
        public void Initialize()
        {
            ShapeArray    = new Shape[2];
            
			ShapeArray[0]             = new Triangle(new Point(100,100),new Point(200,200),new Point(100,300));
			ShapeArray[0]._PictureBox = this._PictureBox;
			ShapeArray[0].DrawPen     = new System.Drawing.Pen(Color.Blue, 2);
 
			ShapeArray[1]             = new Square(new Point(210, 100), new Point(310, 100), new Point(310, 200), new Point(210, 200));
			ShapeArray[1]._PictureBox = this._PictureBox;
			ShapeArray[1].DrawPen     = new System.Drawing.Pen(Color.Green, 2);
           
            _Initialized = true;
            Paint();
        }
    }
}
 
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
 
/// <summary>
/// Summary description for Class1
/// </summary>
namespace Shapes
{
    public class Shape
    {
        private System.Drawing.Graphics g;
        public  System.Drawing.Pen DrawPen  = new System.Drawing.Pen(Color.Red, 2);
        private System.Drawing.Pen ClearPen = new System.Drawing.Pen(SystemColors.Control, 3); // To make a shape invisiable
 
        protected static Point[] Points;
        public System.Windows.Forms.PictureBox _PictureBox;
        public GraphicsPath path = null;
        public Color BackColor = Color.Aqua;
        public Color ForeColor = Color.White;
 
        public Shape()
        {
            // TODO: Add constructor logic here
        }
 
        public void Paint()
        {
            g = _PictureBox.CreateGraphics();
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.DrawPolygon(ClearPen, Points);
            g.DrawPolygon(DrawPen, Points);
        }
    }
 
    public class Triangle : Shape
    {
        public Triangle(Point aPoint1, Point aPoint2, Point aPoint3)
        {
            Points = new Point[4];
            Points[0] = aPoint1;
            Points[1] = aPoint2;
            Points[2] = aPoint3;
            Points[3] = aPoint1; // Back to startpoint 1
        }
    }
 
    public class Square : Shape
    {
        public Square(Point aPoint1, Point aPoint2, Point aPoint3, Point aPoint4)
        {
            Points = new Point[5];
            Points[0] = aPoint1;
            Points[1] = aPoint2;
            Points[2] = aPoint3;
            Points[3] = aPoint4;
            Points[4] = aPoint1; // Back to startpoint 1
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ASliborsky
ASliborsky

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rajasii
Rajasii

Since you declared points as static .

 protected static Point[] Points;