troubleshooting Question

Basic functionality C# question

Avatar of mpoots
mpoots asked on
C#
2 Comments1 Solution479 ViewsLast Modified:
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
        }
    }
}
ASKER CERTIFIED SOLUTION
ASliborsky

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros