Board index » cppbuilder » TImage object trouble

TImage object trouble


2003-11-23 08:06:17 PM
cppbuilder64
Hi at all.
I've a panel and i want make n TImages inside this panel, so i use this to
do:
for(int loop = 0; loop < n; loop++)
{
TImage *Image;
Image->parent = panel;
Image->width = ......
....
}
now i have a function that draw inside the TImage. But the question is, how
can establish where to draw if in TImage n? or TImage nX?
Thanx for helping.
 
 

Re:TImage object trouble

"gif" < XXXX@XXXXX.COM >wrote:
Quote
I've a panel and i want make n TImages inside this panel, so
i use this to do:
for(int loop = 0; loop < n; loop++)
{
TImage *Image;
Image->parent = panel;
Image->width = ......
....
}
This code leaks memory. When you allocate memory, you must
keep a pointer to that memory OR you must set the Owner so
that when the owner is destroyed, the memory will be freed.
Since you want to later reference each object, you need to
save a pointer to the objects. You can then use the pointers
to delete the objects or you can let the Owner clean them up
IF you set the Owner property when you allocate them.
Assuming that you're most likely displaying the images in
columns and rows, I suggest that you use a 2D array of pointers:
int ColCount = 5, RowCount = 10;
TImage*** ImageArray = NULL;
ImageArray = new TImage**[ ColCount ];
for( int Col = 0; Col < ColCount; ++Col ) ImageArray[ Col ] = NULL;
for( int Col = 0; Col < ColCount; ++Col )
{
ImageArray[ Col ] = new TImage*[ RowCount ];
for( int Row = 0; Row < RowCount; ++Row ) ImageArray[ Col ][ Row ] = NULL;
for( int Row = 0; Row < RowCount; ++Row )
{
ImageArray[ Col ][ Row ] = new TImage( this );
...
ImageArray[ Col ][ Row ]->Parent = Panel1;
}
}
Now, using this method, if you set the Owner, you don't need
to delete the object but you still need to delete the memory
allocated to save the pointers:
for( int Col = 0; Col < ColCount; ++Col )
{
// this is optional if you set the Owner but good practice
for( int Row = 0; Row < RowCount; ++Row )
{
delete ImageArray[ Col ][ Row ];
}
delete [] ImageArray[ Col ];
}
delete [] ImageArray;
Quote
[...] how can establish where to draw if in TImage n? or
TImage nX?
You could define a global TImage pointer and use the OnClick
Event to make it point to the image that was just clicked.
TImage* CurrentImage;
void __fastcall TForm1::ImageClick(TObject* Sender)
{
CurrentImage = static_cast<TImage*>( Sender );
}
but for this to work, you'll also need to assign the OnClick
event to the TImage when you allocate it:
ImageArray[ Col ][ Row ]->OnClick = ImageClick;
~ JD
 

Re:TImage object trouble

Sorry i just correct the code with:
for(int loop = 0; loop < n; loop++)
{
TImage *Image
ImageLevel = new TImage(this);
Image->parent = panel;
Image->width = ......
....
}
 

{smallsort}

Re:TImage object trouble

Is there a method at the place of using array?
i thoungh to set a name at every image during the creation then to scan
every parent of panel and serch for one of that name.Is it possible?
Infact it is possible to reffer also without needs on click event.
"JD" < XXXX@XXXXX.COM >ha scritto nel messaggio
Quote

"gif" < XXXX@XXXXX.COM >wrote:
>I've a panel and i want make n TImages inside this panel, so
>i use this to do:
>for(int loop = 0; loop < n; loop++)
>{
>TImage *Image;
>Image->parent = panel;
>Image->width = ......
>....
>}

This code leaks memory. When you allocate memory, you must
keep a pointer to that memory OR you must set the Owner so
that when the owner is destroyed, the memory will be freed.

Since you want to later reference each object, you need to
save a pointer to the objects. You can then use the pointers
to delete the objects or you can let the Owner clean them up
IF you set the Owner property when you allocate them.

Assuming that you're most likely displaying the images in
columns and rows, I suggest that you use a 2D array of pointers:

int ColCount = 5, RowCount = 10;
TImage*** ImageArray = NULL;

ImageArray = new TImage**[ ColCount ];
for( int Col = 0; Col < ColCount; ++Col ) ImageArray[ Col ] = NULL;
for( int Col = 0; Col < ColCount; ++Col )
{
ImageArray[ Col ] = new TImage*[ RowCount ];
for( int Row = 0; Row < RowCount; ++Row ) ImageArray[ Col ][
Row ] = NULL;
for( int Row = 0; Row < RowCount; ++Row )
{
ImageArray[ Col ][ Row ] = new TImage( this );
...
ImageArray[ Col ][ Row ]->Parent = Panel1;
}
}

Now, using this method, if you set the Owner, you don't need
to delete the object but you still need to delete the memory
allocated to save the pointers:

for( int Col = 0; Col < ColCount; ++Col )
{
// this is optional if you set the Owner but good practice
for( int Row = 0; Row < RowCount; ++Row )
{
delete ImageArray[ Col ][ Row ];
}
delete [] ImageArray[ Col ];
}
delete [] ImageArray;

>[...] how can establish where to draw if in TImage n? or
>TImage nX?

You could define a global TImage pointer and use the OnClick
Event to make it point to the image that was just clicked.

TImage* CurrentImage;
void __fastcall TForm1::ImageClick(TObject* Sender)
{
CurrentImage = static_cast<TImage*>( Sender );
}

but for this to work, you'll also need to assign the OnClick
event to the TImage when you allocate it:

ImageArray[ Col ][ Row ]->OnClick = ImageClick;

~ JD

 

Re:TImage object trouble

"gif" < XXXX@XXXXX.COM >wrote:
Quote
Is there a method at the place of using array? [...]
I'm guessing that you want to associate each Image in the
array with a filename. If that's what you want to do, since
you have to maintain the list of names, use a TStringList for
that.
Then I would suggest that you use a simple array v/s a 2D
array of Images. It's a bit more complicated to postion the
Images on the Panel but everything else is easier to work with.
TStringList* NameList = NULL;
NameList = new TStringList;
// populate NameList
TImage** ImageArray = NULL;
ImageArray = new TImage*[ NameList->Count ];
for( int x = 0; x < NameList->Count; ++x ) ImageArray[ x ] = NULL;
for( int x = 0; x < NameList->Count; ++x )
{
ImageArray[ x ] = new TImage( this );
...
ImageArray[ x ]->Parent = Panel1;
}
Then to associate a name with an Image, use the StringList
IndexOf method:
TImage* CurrentImage = ImageArray[ NameList->IndexOf( "the name" ) ];
Finally, to delete the array:
for( int x = 0; x < NameList->Count; ++x ) delete ImageArray[ x ];
delete [] ImageArray;
delete NameList;
~ JD
 

Re:TImage object trouble

"gif" < XXXX@XXXXX.COM >wrote in message
Quote
Is there a method at the place of using array?
I do not understand what you are asking for.
Quote
i thoungh to set a name at every image during the creation
then to scan every parent of panel and serch for one of that
name.Is it possible?
TComponent has a FindComponent() method for that purpose.
Gambit
 

Re:TImage object trouble

"gif" < XXXX@XXXXX.COM >wrote in message
Quote
now i have a function that draw inside the TImage. But the
question is, how can establish where to draw if in TImage n?
or TImage nX?
It is unclear what you are asking for exactly. Please clearify. What
exactly are you trying to accomplish? Please provide more details.
Gambit