 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
fred Guest
|
Posted: Tue Jul 22, 2003 2:19 pm Post subject: way to rotate an object over an image bmp |
|
|
Is there a way to rotate objects, like watch hands, over a bmp or any other
type picture. Flash can do it but I don't know if Delphi also does this.
Fred
|
|
| Back to top |
|
 |
Poster Guest
|
Posted: Tue Jul 22, 2003 6:35 pm Post subject: Re: way to rotate an object over an image bmp |
|
|
No, Delphi cannot do arty stuff that flash etc can. But if you try hard enuff you can do what you want. Weve been trying to do that for a long time now.
Cheers
"fred" <fmeylan (AT) asulab (DOT) ch> wrote:
| Quote: | Is there a way to rotate objects, like watch hands, over a bmp or any other
type picture. Flash can do it but I don't know if Delphi also does this.
Fred
|
|
|
| Back to top |
|
 |
Jens Gruschel Guest
|
Posted: Tue Jul 22, 2003 8:53 pm Post subject: Re: way to rotate an object over an image bmp |
|
|
| Quote: | Is there a way to rotate objects, like watch hands, over a bmp or any
other
type picture. Flash can do it but I don't know if Delphi also does this.
|
Delphi uses the Windows GDI, so you can do whatever GDI can. This does not
include rotating objects. But you can write your own routines that can do
this (to rotate a bitmap, transform the vector to each pixel by a formula
like x' = cos(a)*x + sin(a)*y; y' = cos(a)*y - sin(a)*x and use direct
memory access with the help of TBitmap.ScanLine to make it fast). You can
also make use of graphics librarys, where others already did that, like
Graphics32 ([url]http://www.g32.org)[/url]. Alternatively you can use the new GDI+,
which gives you more functionality than GDI (including alpha blending and
antialiasing), use your favourite search engine for more information.
Jens
|
|
| Back to top |
|
 |
Don E. Brook Guest
|
Posted: Tue Jul 22, 2003 9:07 pm Post subject: Re: way to rotate an object over an image bmp |
|
|
This program shows two ways to do what you want. The first way is to save the
underlying image before drawing on your bitmap. Then you draw your hands.
Before drawing your hands in the second position, you restore the original
picture, save the next underlying area and draw the hands at the new position.
The second way uses the paintbox which draws directly to the screen. I like
this way because you don't have to save squat.
To recreate this program, drop a TPanel onto a form, set its DoubleBuffered
property to true to stop flicker. Drop a TImage onto the panel. Next, drop a
second TImage onto the form and on top of it, drop a TPaintbox. Make the
TPanel, both TImages, and the TPaintBox square and all the same size for this
demo to work. Drop a TTImer onto the form and set the interval to 100... or
whatever.
Finally, drop a couple of TBitBtns to control the action. Then, the code looks
like this.
***********************
unit movehandsu;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons, ExtCtrls, math;
type
TForm1 = class(TForm)
GoButton: TBitBtn;
StopButton: TBitBtn;
Timer1: TTimer;
Panel1: TPanel;
Image1: TImage;
Image2: TImage;
PaintBox1: TPaintBox;
procedure GoButtonClick(Sender: TObject);
procedure StopButtonClick(Sender: TObject);
procedure RotateTheHandWithSaveRect;
procedure Timer1Timer(Sender: TObject);
procedure PaintBox1Paint(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Pi : double = 3.14159;
Minute : double;
RectSaved : boolean;
HandCount : integer;
CenterX, CenterY, LineLength : integer;
EndX, EndY, LoX, HiX, LoY, HiY : integer;
SaveBmp : TBitmap;
implementation
{$R *.DFM}
// a button to make the clock go
procedure TForm1.GoButtonClick(Sender: TObject);
begin
// make the TPaintBox and TImage the same size so we can
// use the same values for center and line length
Panel1.DoubleBuffered := true;
CenterX := Image1.Width div 2;
CenterY := Image1.Height div 2;
LineLength := (Image1.Width div 2) - 10;
Minute := (2 * Pi) / 60.0;
RectSaved := false;
HandCount := 0;
Timer1.Enabled := true;
end;
// a button to make the clock stop
procedure TForm1.StopButtonClick(Sender: TObject);
begin
Timer1.Enabled := false;
if RectSaved then begin
Image1.Canvas.CopyRect(Rect(LoX - 2, LoY - 2, HiX + 2, HiY + 2),
SaveBmp.Canvas, Rect(0,0,SaveBmp.Width,SaveBmp.Height));
SaveBmp.Free;
end;
end;
// this timer is enabled by buttons above and controls the
// speed at which the hands move
procedure TForm1.Timer1Timer(Sender: TObject);
begin
RotateTheHandWithSaveRect; // do the copyrect version
PaintBox1.Repaint; // do the paintbox version
HandCount := HandCount + 1;
if HandCount > 59 then
HandCount := 0;
Application.ProcessMessages;
end;
// draw directly to the screen - no save needed
procedure TForm1.PaintBox1Paint(Sender: TObject);
var
angle : double;
PaintX, PaintY : integer;
begin
// compute position of hand and draw it
angle := minute * HandCount;
PaintX := CenterX + Round(Cos(angle) * LineLength);
PaintY := CenterY + Round(Sin(angle) * LineLength);
//now draw the line for the hand
PaintBox1.Canvas.Pen.Color := clRed;
PaintBox1.Canvas.MoveTo(PaintX, PaintY);
PaintBox1.Canvas.LineTo(CenterX, CenterY);
end;
// draw with a save of the underlying image to restore it later
procedure TForm1.RotateTheHandWithSaveRect;
var
angle : double;
begin
angle := minute * HandCount;
EndX := CenterX + Round(Cos(angle) * LineLength);
EndY := CenterY + Round(Sin(angle) * LineLength);
// restore underlying rect if there is one,
if RectSaved then begin
Image1.Canvas.CopyRect(Rect(LoX - 2, LoY - 2, HiX + 2, HiY + 2),
SaveBmp.Canvas, Rect(0,0,SaveBmp.Width,SaveBmp.Height));
SaveBmp.Free;
RectSaved := false;
end;
// then save the underlying rect for next hand
LoX := Min(CenterX, EndX);
HiX := Max(CenterX, EndX);
LoY := Min(CenterY, EndY);
HiY := Max(CenterY, EndY);
SaveBmp := TBitmap.Create;
SaveBmp.Width := HiX - LoX + 4;
SaveBmp.Height := HiY - LoY + 4;
SaveBmp.Canvas.CopyRect(Rect(0,0,SaveBmp.Width,SaveBmp.Height),
Image1.Canvas,Rect(LoX - 2, LoY - 2, HiX + 2, HiY + 2));
RectSaved := true;
//now draw the line for the hand
Image1.Canvas.Pen.Color := clRed;
Image1.Canvas.MoveTo(EndX, EndY);
Image1.Canvas.LineTo(CenterX, CenterY);
end;
end.
fred wrote:
| Quote: | Is there a way to rotate objects, like watch hands, over a bmp or any other
type picture. Flash can do it but I don't know if Delphi also does this.
Fred
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|