Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delphi 6.0 Runtime TImage

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
I have the following:

Type

TCards = Record
Suit : ShortInt; // 1:Diamonds 2:Hearts 3:Clubs 4:Spades
Status : ShortInt; // 1:Solved 2:Table 3:Hand
PlacementNo : ShortInt; // 52 placements
Back : Boolean; // Frontside or Backside of card
Name : String[30]; // Eg.: Ace of Diamonds
Filename : String[20]; // Filename for the Bitmap
Picture : TPicture; // Picture for Image
Image : TImage; // Image for the card
End;

And the following Variable:

Var
Cards : Array[1..52] of TCards;

I am then assigning values to each of the Cards:

For I := 1 to 13 Do // Diamonds
Begin
Cards.Suit := 1;
Cards.Status := 3;
Cards.PlacementNo := I;
Cards.Back := False;
Cards.Name := 'Diamonds ' + IntToStr(I);
Cards.Filename := 'D' + IntToStr(I) + '.bmp';
Cards.Picture.LoadFromFile(Cards.Filename);
Cards.Image.Picture := Cards.Picture;
End;

My question is: "How do I create the cards at Runtime".
This keeps getting me Exception Errors.

Thanks in advance
EskimoJan
 
The Cards(i) with the i counter is missing.
I think it is because this forum supports Italic.
 
Begin your code fragment with
code and end it with /code put the insdicators between brackets [...] Steven van Els
SAvanEls@cq-link.sr
 
If it were me, I'd use a TList - I try to avoid arrays simply because I don't like them.

I, personally, would do it like this:-
Type

PCardRec = ^TCards;
TCards = Record
Suit : ShortInt; // 1:Diamonds 2:Hearts 3:Clubs 4:Spades
Status : ShortInt; // 1:Solved 2:Table 3:Hand
PlacementNo : ShortInt; // 52 placements
Back : Boolean; // Frontside or Backside of card
Name : String[30]; // Eg.: Ace of Diamonds
Filename : String[20]; // Filename for the Bitmap
Picture : TPicture; // Picture for Image
Image : TImage; // Image for the card
End;

// don;t forget to Create and free the list.
:
//in your method
var Card : PCardRec;
begin
:
for idx := 1 to 13 do
begin
new(Card); //creates a card
Card^.Suit := ...
Card^.Status := ...
... etc ...
CardList.add(card);
end;

I'm probably over-engineering but I just prefer lists. Feel free to criticise, people [smile].

lou
 
Creating TImage at runtime now works using the following code:

var
cardsimage : array[1..52] of TImage;

begin
For I := 1 to 52 Do
Begin
CardsImage := TImage.Create(self);
CardsImage.Parent := self;
CardsImage.Width := 71;
CardsImage.Height := 96;
CardsImage.Top := 300;
CardsImage.Left := I * 11;
CardsImage.Picture.LoadFromFile(Cards.Filename);
End;

end;
 
You forgot to create your Picture and Image fields of a TCard record. Your code should look somewhat like this :
Code:
  For I := 1 to 13 Do  // Diamonds
  Begin
    Cards[I].Suit := 1;
    Cards[I].Status := 3;
    Cards[I].PlacementNo := I;
    Cards[I].Back := False;
    Cards[I].Name := 'Diamonds ' + IntToStr(I);
    Cards[I].Filename := 'D' + IntToStr(I) + '.bmp';
  // creating objects
    Cards[I].Picture := TPicture.Create;
    Cards[I].Image := TImage.Create(nil);
  // ---
    Cards[I].Picture.LoadFromFile(Cards.Filename);
    Cards[I].Image.Picture := Cards.Picture;
  End;
Hope that helps.

--- markus
 
If you wanted to be a little more object oriented, you might want to create classes instread of records. Something like:

Code:
interface


type
  enCardSuit = ( csClubs, csSpades, csDiamonds, csHearts);
  enCardStatus = (cstSolved, cstHand, cstTable);

  TCard = class( TObject)
  private
    fValue : SmallInt;
    fSuit : enCardSuit;
    fStatus : enCardStatus;
    fName : string;
    fImage : TImage;

  public
    constructor Create( pValue : SmallInt; pSuit : enCardSuit; pImageFilename : string );
  end;

  TPackOfCards : class( TObject )
  private
    fCards: TObjectList;

  public
    constructor Create;
    destructor Destroy;
  end;

implementation

const
  CardValueNames : array[1..13] of string = 
  ( 'Ace', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven',
    'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King' );

  CardSuitNames : array[enCardSuit] of string = 
  ( 'Spades', 'Clubs', 'Diamonds', 'Hearts' );

constructor TCard.Create( pValue : SmallInt; pSuit : enCardSuit; pImageFilename : string );
var
  lPicture : TPicture;
begin
  fValue := pValue;
  fSuit :=  pSuit;
  fStatus := enHand;

  { Load the image. }
  if FileExists( pImageFilename ) then
  begin
    lPicture := TPicture.Create;
    try
      lPicture.LoadFromFile( pImageFilename );
      fImage := TImage.Create;
      fImage.Picture := lPicture;
    finally
      FreeAndNil( lPicture );
    end;
  end;

  { Determine the name }
  fName := CardValueNames[fValue] + ' of ' + CardSuitNames[fSuit];

end;

constructor TPackOfCards.Create;
var
  lValueIndex : integer;
  lSuitIndex : enCardSuit;
begin
  fCards := TObjectList.Create( True );

  for lSuitIndex := csSpades to csHearts do
  begin
    for lValueIndex := 1 to 13 do
    begin
      fCards.Add( TCard.Create( lValueIndex, lSuitIndex, lFilename )); { You'll have to determine the filename prior to this... }
    end;
  end;
end;

Hope this is of use... I've just typed this straight in, so i hope it compiles.. it's only really to give you any idea...

GreenKnight.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top