Canvas is not updated as expected

Hi,
I´m trying to create a new object each time i press a key, but the previous drawn object doesn´t disappear completly. There is a polyline I draw inside my objects Display() method that disappears and several Points, the vertices of the Polyline, that stay on the canvas forever. Both are created as PShapes. It really drives me mad…
thanks in advance
Best M.

(I hope the code is not to long, I already tried to break it down)

import toxi.geom.*;

public enum DrawMode{DRAW_MODE, ERASE_MODE};
public enum DamageMode{POINT_MODE,LINE_MODE,SURFACE_MODE};
public DrawMode drawMode = DrawMode.DRAW_MODE;
public DamageMode damageMode = DamageMode.LINE_MODE;

PGraphics gfx; 
public Grid grid = null;
Damage damage = null;
ArrayList<Damage> damages;

public void setup(){
  size(1280, 720,JAVA2D);
  
  grid = new Grid(new Point2D(200,200),10,10,50,50);
  damages = new ArrayList<Damage>();
  damage = new LineDamage("");  
  gfx = createGraphics(width,height);  
}

public void draw(){
  
  background(50);  
  
  gfx.beginDraw();
  gfx.background(50);    
  
  grid.Update(mouseX,mouseY);  
  gfx.shape(grid.Display());
  GeometryBase activeGeom = grid.GetActiveObject(damageMode,mouseX,mouseY);      
   
   if(damage != null)
   {
     if(mousePressed && activeGeom != null)
     {
       damage.Add(activeGeom);
     }
     gfx.shape(damage.Display());
   }  
  
  gfx.endDraw(); 
  image(gfx,0,0); 
}

void keyPressed()
{
  if(key == 'n')
  {
    damages.add(damage);
    damage = new LineDamage("");
  }
}

public enum DamageType{UNSET,POINT_DAMAGE,LINE_DAMAGE,SURFACE_DAMAGE};
//#####################################################################################################
//----------------------------------------------------------------------------------------------
  public abstract class Damage{
  public String Id;
  public DamageType Type;    
  public ArrayList<GeometryBase> Geometry;
  public ArrayList<String> Classification;
  public color DRAW_COLOR = color(255,0,0);
  public color ERASE_COLOR = color(0,0,255);
  PShape Shape;    
  //----------------------------------------------------------------------------------------------
  public abstract void Add(GeometryBase geom);
  public abstract PShape Display();
  //----------------------------------------------------------------------------------------------
}
//###################################################################################
class LineDamage extends Damage{
  //----------------------------------------------------------------------------------------------
  private Polyline2D Poly;
  //----------------------------------------------------------------------------------------------
  public LineDamage(String id){
    Id = id;
    Geometry = new ArrayList<GeometryBase>();
    Classification = new ArrayList<String>();
    Type = DamageType.LINE_DAMAGE;    
    Poly = new Polyline2D();
  }
  //----------------------------------------------------------------------------------------------
  public void Add(GeometryBase geom)
  {    
    Point2D pt = (Point2D) geom;
    if(!Geometry.contains(pt))
    {     
      Geometry.add(pt);
      Poly.Add(pt);
    }    
  }
  //----------------------------------------------------------------------------------------------
  public PShape Display()
  { 
      Shape = createShape(GROUP);
      
      Poly.IsActive = true;
      
      for(GeometryBase geom : Geometry)
      {
        if(geom.Type == GeometryType.POINT_GEOMETRY)
        {          
          Point2D pt = (Point2D) geom;        
          pt.IsActive = true;          
          Shape.addChild(pt.Display());          
        }        
      }
      Shape.addChild(Poly.Display());
      return Shape;
  }
  //----------------------------------------------------------------------------------------------
}

//####################################################################################
public enum GeometryType{UNSET,POINT_GEOMETRY,LINE_GEOMETRY,POLYLINE_GEOMETRY,FACE_GEOMETRY};
//####################################################################################
public abstract class GeometryBase{
  public GeometryType Type;
  public Vec2D Location;
  public PShape Shape;
  public boolean MouseOver;
  public boolean IsActive;
  public boolean IsVisible;
  public abstract void Update(int x,int y);
  public abstract PShape Display(); 
  public abstract String ToString();
}
//####################################################################################
public class Point2D extends GeometryBase
{  
  //------------------------------------------------------------------------------------------------
  public float X;
  public float Y;    
  public int DefaultSize = 5;
  public int ActiveSize = 20;
  public color DefaultFill = color(255,30);
  public color MouseOverFill = color(255,0,0,30); 
  public color ActiveFill = color(255,0,0);
  //------------------------------------------------------------------------------------------------
  public Point2D()
  {
    Type = GeometryType.POINT_GEOMETRY;
  }
  public Point2D(float x,float y)
  {  
    Type = GeometryType.POINT_GEOMETRY;
    this.X = x;
    this.Y = y;
    Location = new Vec2D(x,y);
  }   
  //------------------------------------------------------------------------------------------------
  public void Update(int x,int y)
  {
    if(Location.distanceTo(new Vec2D(x,y)) < 10){
      MouseOver = true;
    }
    else
    {
      MouseOver = false;
    }    
  }
  //------------------------------------------------------------------------------------------------
  public PShape Display()
  {  
    int size = DefaultSize;
    color fillColor = DefaultFill;
    if(drawMode == DrawMode.DRAW_MODE && damageMode == DamageMode.LINE_MODE || drawMode == DrawMode.DRAW_MODE && damageMode == DamageMode.POINT_MODE)
    {     
      if(MouseOver){
        size = ActiveSize;
        fillColor = MouseOverFill;
      }
      if(IsActive){
        size = ActiveSize;
        fillColor = ActiveFill; 
      }
    } 
    
    gfx.pushStyle();
      gfx.noStroke();
      gfx.fill(fillColor);     
      Shape = gfx.createShape(ELLIPSE,X,Y,size,size);
    gfx.popStyle();    
   
    return Shape;
  }
  //------------------------------------------------------------------------------------------------
  public String ToString(){
    return String.format("Type: %s, X: %f, Y: %f",Type.name(),X,Y);
  }
  //------------------------------------------------------------------------------------------------
}
//####################################################################################
public class Polyline2D extends GeometryBase
{ 
  LineStrip2D strip;
  public int StrokeWidth = 5;
  public color StrokeColor = color(255,0,0);    
  //------------------------------------------------------------------------------------------------
  public Polyline2D()
  {
    Type = GeometryType.POLYLINE_GEOMETRY;
    strip = new LineStrip2D();
    MouseOver = false;
    IsActive = false;
  }   
  //------------------------------------------------------------------------------------------------
  public Polyline2D(ArrayList<Point2D> vertices)
  {   
    Type = GeometryType.POLYLINE_GEOMETRY;
    Location.set(vertices.get(0).X,vertices.get(0).Y);
    MouseOver = false;
    IsActive = false; 
    strip = new LineStrip2D();
    for(Point2D vert : vertices)
    {
      strip.add(new Vec2D(vert.X,vert.Y));
    }
  }
  //------------------------------------------------------------------------------------------------
  public void Add(Point2D pt){    
    strip.add(new Vec2D(pt.X,pt.Y));
  }
  //------------------------------------------------------------------------------------------------
  public void Update(int x,int y)
  {
    MouseOver = false;
    for(toxi.geom.Line2D line : strip.getEdges()){
      if(line.distanceToPoint(new Vec2D(x,y)) < 10){
        MouseOver = true;
      }
      break; 
    }    
  }  
  //------------------------------------------------------------------------------------------------
  public PShape Display()
  {    
      Shape = createShape();
      Shape.beginShape();
        Shape.noFill();
        Shape.stroke(StrokeColor);
        Shape.strokeWeight(StrokeWidth);
        for(Vec2D vert : strip.getVertices())
        {
          Shape.vertex(vert.x,vert.y);     
        }  
      Shape.endShape();       
    
    return Shape;
  }
  //------------------------------------------------------------------------------------------------
  public String ToString(){
    return String.format("Type: %s",Type.name());
  }
  //------------------------------------------------------------------------------------------------
}
//####################################################################################
public class Face2D extends GeometryBase
{
  //------------------------------------------------------------------------------------------------
  public int[] id;
  public Polygon2D Poly;
  public ArrayList<Point2D> Vertices;
  public ArrayList<Line2D> Edges;
  public PShape Shape;
  public int StrokeWidth = 1;
  public color StrokeColor = color(255);  
  boolean MouseOver = false;
  boolean IsActive;
  public color DefaultFill = color(255,30);
  public color MouseOverFill = color(255,0,0,30); 
  public color ActiveFill = color(255,0,0);
  //------------------------------------------------------------------------------------------------
  public Face2D(){
    Type = GeometryType.FACE_GEOMETRY;
  }
  public Face2D(ArrayList<Point2D> vertices)
  {
    Type = GeometryType.FACE_GEOMETRY;
    Poly = new Polygon2D();
    for(Point2D vert : vertices){
      Poly.add(new Vec2D(vert.X,vert.Y));
    }
    Vertices = new ArrayList<Point2D>();
    Edges = new ArrayList<Line2D>();
    IsActive = false;
    Location = new Vec2D(vertices.get(0).X,vertices.get(0).Y);
  }
  //------------------------------------------------------------------------------------------------
  public boolean ContainsPoint(Point2D pt){
    boolean output = false;
    if(Poly.containsPoint(new Vec2D(pt.X,pt.Y))) output = true;
    return output;
  }
  //------------------------------------------------------------------------------------------------
  public void Update(int x,int y)
  {
    if(Poly.containsPoint(new Vec2D(x,y))){
      MouseOver = true;      
    }
    else
    {
      MouseOver = false;      
    }
  }  
 //------------------------------------------------------------------------------------------------
  public PShape Display()
  {    
    color fillColor = DefaultFill;
    if(drawMode == DrawMode.DRAW_MODE && damageMode == DamageMode.SURFACE_MODE)
    {     
      if(MouseOver && !IsActive)fillColor = MouseOverFill;
      if(IsActive)fillColor = ActiveFill;      
    }
    Shape = createShape();    
    Shape.beginShape();
      Shape.fill(fillColor);
      Shape.stroke(StrokeColor);
      Shape.strokeWeight(StrokeWidth);
      for(Vec2D vert : Poly)
      {
        Shape.vertex(vert.x,vert.y);
      }
    Shape.endShape(CLOSE); 
        
    return Shape;
  }  
  //------------------------------------------------------------------------------------------------
  public String ToString(){
    return String.format("Type: %s",Type.name());
  }
  //------------------------------------------------------------------------------------------------
}
//####################################################################################
public class Grid
{
  //------------------------------------------------------------------------------------------------
  public int Columns;
  public int Rows;  
  public int CellWidth;
  public int CellHeight;  
  public Point2D Location;
  
  public Point2D[][] Points;
  public ArrayList<Face2D> Faces;
  
  public color MajorLineColor = color(255);
  public color MinorLineColor = color(150);
  public PShape Shape;
  //------------------------------------------------------------------------------------------------
  public Grid(Point2D loc,int nU,int nV,int w,int h)
  {
    Location = loc;
    Columns = nU;
    Rows = nV;
    CellWidth = w;
    CellHeight = h;
    Points = new Point2D[Columns][Rows];    
    for(int u=0;u<Columns;u++)
    {
      for(int v=0;v<Rows;v++)
      {
        float xPos = Location.X + CellWidth * u;
        float yPos = Location.Y + CellHeight * v;            
        Points[u][v] = new Point2D(xPos,yPos);
      }
    }
    
    Faces = new ArrayList<Face2D>();
    for(int u=0;u<Columns-1;u++)
    {
      for(int v=0;v<Rows-1;v++)
      {        
        ArrayList<Point2D> corners = new ArrayList<Point2D>();
        corners.add(Points[u][v]);
        corners.add(Points[u+1][v]);
        corners.add(Points[u+1][v+1]);
        corners.add(Points[u][v+1]);
                
        Face2D face = new Face2D(corners);
        Faces.add(face);
      }
    }     
  }
  //------------------------------------------------------------------------------------------------
  public void Update(int x,int y)
  {
    
    for(int u=0;u<Columns;u++)
    {
      for(int v=0;v<Rows;v++)
      {       
        Points[u][v].Update(x,y);
      }
    }    
    
    for(Face2D face : Faces)
    {      
      face.Update(x,y);
    }
  }
  //------------------------------------------------------------------------------------------------
  public PShape Display()
  {
    Shape = createShape(GROUP);
    for(Face2D face : Faces)
    {      
      Shape.addChild(face.Display());
    }
    for(int u=0;u<Columns;u++)
        {
          for(int v=0;v<Rows;v++)
          {  
        Shape.addChild(Points[u][v].Display());        
      }
    }       
    return Shape;
  }
  //------------------------------------------------------------------------------------------------
  public GeometryBase GetActiveObject(DamageMode mode,int x,int y)
  {
    GeometryBase output = null;
    switch(mode)
    {      
      case POINT_MODE:
        for(int u=0;u<Columns;u++)
        {
          for(int v=0;v<Rows;v++)
          {  
            Point2D pt = Points[u][v];
            if(x < pt.X +10 && x > pt.X - 10 && pt.Y < y + 10 && pt.Y > y - 10)
            {
              output = pt;
              break;
            }        
          }
        }       
        break;
      case LINE_MODE:
        for(int u=0;u<Columns;u++)
        {
          for(int v=0;v<Rows;v++)
          {  
            Point2D pt = Points[u][v];
            if(x < pt.X +10 && x > pt.X - 10 && pt.Y < y + 10 && pt.Y > y - 10)
            {
              output = pt;
              break;
            }        
          }
        } 
        break;
      case SURFACE_MODE:
      for(Face2D face : Faces)
        {
          if(face.ContainsPoint(new Point2D(x,y)))
          {
            output = face;
            break;
          }
        }
        break;     
    }
    return output;
  }
  //------------------------------------------------------------------------------------------------
}
//####################################################################################

Thats a lot of code. Which object is misbehaving?

It sounds like you might have errors with either copying a PShape or attempting to update one (PShape is, in general, not designed for updating – it is often easier to draw a new PShape and throw the old one away). But it would be really helpful for debugging (yours and ours) if you removed a lot of the code that isn’t related to your problem, making as close to an MCVE as you can.

You often – but not always – capitalize class variable names (normally that indicates a class name, and methods and variables should be lower camel case ). This makes it hard for me to understand what is going on, but at a glance I suspect that you might have problems in your innermost classes with Vertices vs vertices – but that is just a first guess.

Second guess: try commenting out around line 415.

    for (int u=0; u<Columns; u++)
    {
      for (int v=0; v<Rows; v++)
      {  
        // Shape.addChild(Points[u][v].Display());
      }
    }