C++ Builder

La Isla Bonita



Moving a TImage along a straight line
Moving a TImage along a straight line


Description

This article explains how to move an image (TImage) along a straight line from any arbitrary point P1 to another point P2. The image is shown during the movement. The Timer should be disabled after the move operation is complete. All The components on the form are created dynamically.

MoveTImage.cpp

#include <vcl.h>
#pragma hdrstop

#include "MoveTImage.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

const int X1 = 200;
const int Y1 = 200;

const int X2 = 300;
const int Y2 = 300;

const int DX = X2-X1;
const int DY = Y2-Y1;

const int D  = 50;             //Denominator

const int DIM_IMAGES  = 1;     //NUMBER OF IMAGES
TImage*   Image1[DIM_IMAGES];
TButton*  Button1;
TButton*  Button2;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
//
    Width  = 800;
    Height = 600;
    Position = poScreenCenter;

    Button1 = new TButton(this);
    Button1->Parent = this;
    Button1->Top    =  10;
    Button1->Left   =  50 + 100;
    Button1->Width  =  200;
    Button1->Height =  30;
    Button1->Visible = true;
    Button1->Caption = "Load";
    Button1->OnClick = Button1Click;

    Button2 = new TButton(this);
    Button2->Parent = this;
    Button2->Top    =  10;
    Button2->Left   =  Button1->Left + Button1->Width + 5;
    Button2->Width  =  200;
    Button2->Height =  30;
    Button2->Visible = true;
    Button2->Caption = "Move";
    Button2->OnClick = Button2Click;

      //We Use an array for future enhancements.
    for ( int i=0; i< DIM_IMAGES; i++ )
    {
        Image1[i] = new TImage(this);
        Image1[i]->Parent = this;
        Image1[i]->Top    =   Y1;
        Image1[i]->Left   =   X1;
        Image1[i]->Width  =  100;
        Image1[i]->Height =  100;
        Image1[i]->Visible = false;
    }

    //Enable the Timer
    Timer1->Enabled = false;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
//
   Image1[0]->Picture->LoadFromFile( "C:\\Graphics\\bmp\\pic1.bmp" );
   Image1[0]->Visible = true;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
//MOVE
       Timer1->Enabled = true;   
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
      if ( DX > 0 )
      {
          if ( Image1[0]->Left < X2  )
          {
             Image1[0]->Left += DX/D;
          }
      }
      if ( DX < 0 )
      {
          if ( Image1[0]->Left > X2  )
          {
             Image1[0]->Left += DX/D;
          }
      }

      if ( DY > 0 )
      {
          if ( Image1[0]->Top < Y2 )
          {
             Image1[0]->Top += DY/D;
          }
      }
      if ( DY < 0 )
      {
          if ( Image1[0]->Top > Y2 )
          {
             Image1[0]->Top += DY/D;
          }
      }
}

MoveTImage.h

#ifndef MoveTImageH
#define MoveTImageH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp> ;
#include <Buttons.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        TTimer *Timer1;
        void __fastcall Button1Click(TObject *Sender);
        void __fastcall Button2Click(TObject *Sender);
        void __fastcall Timer1Timer(TObject *Sender);


private:	// User declarations
public:		// User declarations
        __fastcall TForm1(TComponent* Owner);
        
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Homepage

Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.