I have an abstract class "Element" which its instances have to use the function DrawRectangle() from System.Drawing.
The problem is that because the class is abstract it can't inherit from FORM so the program doesn't recognize the classes..
How can I solve this?
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace MyMouseGame
{
public abstract class Element
{
public enum EnumShape { Square, Circle, Triangle }
public enum EnumType { Chase, Escape, Random }
private double X { get; set; }
private double Y { get; set; }
private int Size { get; }
private int Speed { get; }
private EnumShape Shape { get; set; }
protected EnumType Type { get; set; }
private static Random R = new Random();
public int generateRandomNum()
{
int randomNum = R.Next(20, 100);
return randomNum;
}
public Element()
{
X= 0;
Y = 0;
Shape = 0;
Size = generateRandomNum();
Speed = generateRandomNum();
Type = 0;
}
public void ElementsFactory()
{
}
public void hitTarget(e) { }
public abstract void Draw(Form1 g);
public abstract void Move();
}
}
<pre>using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace MyMouseGame
{
class E_Escape : Element
{
int move_circleX = 0;
int move_circleY = 0;
bool flag = false;
Rectangle circle = new Rectangle(10, 70, 35, 35);
Pen p = new Pen(Color.Black);
public E_Escape()
{
}
public override void Draw(Form1 g) {
g.DrawEllipse(p, circle);
g.FillEllipse(new SolidBrush(Color.Red), circle);
}
public override void Move() {
if (!flag)
{
if (circle.X >= panel1.Width - 2)
flag = true;
circle.X += 10;
}
else
{
if (circle.X <= 30)
flag = false;
circle.X -= 10;
}
}
}
}
<pre lang="C#"><pre>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace MyMouseGame
{
public partial class Form1 : Form
{
Graphics g;
Element [] elements= new Element[3];
public Form1()
{
this.DoubleBuffered = true;
InitializeComponent();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
g = panel1.CreateGraphics();
t.Interval = 100;
t.Tick += new EventHandler(timer1_Tick);
t.Start();
}
private void recThreadTracker()
{
while (true)
{
elements[0].Draw(g);
elements[1].Draw(g);
elements[2].Draw(g);
}
}
public void threadCounter()
{
long counter = 0;
while (true)
{
counter++;
this.Invoke(new Action(() =>
{
lcountRes.Text = counter.ToString();
}));
Thread.Sleep(1000);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Thread counterThread = new Thread(threadCounter);
counterThread.Start();
Thread recTrackThread = new Thread(recThreadTracker);
recTrackThread.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
foreach (Element elements in elements)
{
elements.Move();
}
Invalidate();
}
}
}
What I have tried:
I've tried puting a variable of type Form inside Element and didn't work.