Save two different draws of an image

I want to create a class that gets some position parameters, draws a dot on an image on this position and saves it in a new image file.
I want this to happen several time, but every time, the previous dots are also painted.

MainComponent.h

public:
MainComponent(float A, float F); //constructor
Image exporty;
Graphics* exportyGraphic;

MainComponent.cpp

//Get Image from Binary Data
exporty = ImageCache::getFromMemory(BinaryData::axis_jpeg, BinaryData::axis_jpegSize);

//Create Graphic with the image
exportyGraphic = new Graphics(exporty);

//Draw dots in the positions (F, A)
exportyGraphic->setColour(Colours::red);
for (int count = 0; count < bands; count++) 
	exportyGraphic->fillEllipse(F, A, 10, 10);

//Get save Directory and file Name
FileChooser* fc = new FileChooser("Select Directory to export the Results",
	File::getSpecialLocation(File::userHomeDirectory),
	".png", true, false);
fc->browseForFileToSave(true);
File destination = fc->getResult();

//create file
File exportFile = destination;
exportFile.create();

//write On file
FileOutputStream    stream(exportFile);
PNGImageFormat png;
png.writeImageToStream(exporty, stream);
   
//Deconstruct
exportyGraphic->~Graphics();
exporty.~Image();

In the Main function of my application, i create two times this class with two different positions F,A :
Main.cpp

myMainComponent = new MainComponent(125, 30);
myMainComponent = NULL;
myMainComponent = new MainComponent(125, 0);

This pops up the saveFile window two times as expected.
The first image file has dot on 125,30 but the second has two dots in both 125,30 and 125,0 as you can see in the images below

Image 1


Image 2

How can i erase any previous draws that have been made?
Why two different implementations of the same class have common things?

The problem you are facing is, that Image uses shared bitmap data, so you are actually drawing into the original.

To create a copy, change that line:

exporty = ImageCache::getFromMemory(BinaryData::axis_jpeg, BinaryData::axis_jpegSize).createCopy();

But your code has several other issues:

  1. Don’t create your Graphics on the Heap but rather on the stack (and not as a member, so on destruction all is flushed)
//Create Graphic with the image
Graphics exportyGraphic (exporty);

//Draw dots in the positions (F, A)
exportyGraphic.setColour(Colours::red);
for (int count = 0; count < bands; count++) 
	exportyGraphic.fillEllipse(F, A, 10, 10);
// ...
  1. Image is also not intended to be created with new

  2. If you create objects on the heap (i.e. calling new), you have to delete them later. But actually avoid the raw pointers and use ScopedPointer instead

  3. Never call the destructor yourself. It will be automatically called on destruction. Especially since the Image exporty is created on the Stack

Hope that helps

Helps 100%
thank you!