Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

I have to do application like Paint. I have to paint Line, Rectangule and Ellipse. I want to use STATE deisgn patern. I made class Line, Rect and Ellipse and Interface IPaint.


In class main I made IPaint paint, and List<IPaint> list= new Line<IPaint>(); Then by 3 buttons i add paint = new Line(); or rect or ellipse() depends on wich button are clicked. But when I`m use list.Add(paint) this not working correct.

When i add first element it`s OK, But when i add second element then first element look like second. When i`m add thirth element then first, and second el. have value like thitrh.

How can i do this right ?

/////edit
Yes it`s just a typo in post. In code is

C#
List<IPaint> list = new List<IPaint>;


Im in work now and i dont have acces to code.
But full code look like:

Interface IPaint
C#
Interface IPaint
{
void Paint{PrintEvenArgs e}
Point BeginPoint{get;set;}
Point EndPoint(get;set)
}


Class Line, Rect and Ellipse look the same:
C#
Class Line: IPaint{
public Begin/Endpoint {get;set;}
void Paint(PrintEventArgs e)
{
Pen p = new Pen(Color red,2)
e.drawLine(p,BeginPoint,EndPoint)
}
//in Rect and ellipse only change drawRectangle or DrawEllipse
}


In Form1 i do:
C#
Class Main{
IPaint point;
List<IPaint> lista = new List<IPaint>();

 Void Main
 {


    Inicianization();
 }

  event MouseDown(Obiect Sender, MouseArgs e)
  {
     point.BeginPoint = e.location;

  }
  
  event mouseMove(...)
  {
    point.EndPoint = e.location;
    Invalidate();
  }

  event mouseup(..)
  {
    lista.Add(point);
  }

event Form1_Paint(Object Sender, PrintEventArgs e)

  If (!Point.BeginPoint.IsNull & !Point.EndPoint.IsnuLL)
  {
    point.Paint(e);
  }

  for (int i =0, i< lista.Count, i++)
  {
    lista[i].Paint(e);
  }

event button1_click(...)
{
point = new Line();
// button 2 - point = New Rectangle or ellipse button 3
}
}



Problem is in add to list. When i Add first element with point`s (2,2)m second (3,3) then first change value to second element on the list, when i add thirth with points (5,6) then first and secont element change value to thirth element and list look like:

list[0] - Begin point - 5
- EndPoint - 6
list[1] - Begin point - 5
- EndPoint - 6
list[2] - Begin point - 5
- EndPoint - 6


It is look like when i add to list object point, it is add by reference. But why ?
Posted
Updated 16-Jan-12 0:27am
v7
Comments
OriginalGriff 16-Jan-12 5:09am    
Sorry, but I can't work out from that what you are doing.
Try showing us the relevant code fragments where you add the new elements.
Use the "Improve question" widget to edit your question and provide better information.

Tip: Make sure the "Treat my content as plain text..." option is disabled, and use the "code" widget to retain formatting when you post code samples. You can see the effect in the preview below your message.
BobJanova 16-Jan-12 5:43am    
What you've written there sounds correct. Did you make sure the painting method in the three classes is actually rendering something different?
phil.o 16-Jan-12 6:08am    
How do you declare and affect your point variable ?
jezollo 16-Jan-12 6:20am    
IPaint point;

Then in buttons i do new Line() or rectangle or ellipse depends wich button was clicked. And on beginning button1 - Line is clicked as defoult.

List<IPaint> list= new Line<IPaint>();


There is a typo here ; you should rather write :

List<IPaint> list = new List<IPaint>();


If this is just a typo in your post (and not in your actual code), did you make your three classes (Line, Rectangle, Ellipse) implement IPaint ?
 
Share this answer
 
v2
Like I sad before, i add on list always the same Object point, when i change value of obiect i one more time add to list but the same obiect and its everthing is by reference.

I have to change in MouseMove event to:
C#
if Line
point = New Line();
if rect;// 
point = New Rect();
else point = (New Ellipse();

point.EndPoint = e.location
 
Share this answer
 
v2

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