Skip to content
TheChatArchiveThe Chat Archive
Googlegemma-3-27b-it· GoogleHugging Face LMArena Human Preference 140K

i need a program in delphi for drawing a picture and animate it in time, so move it and…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with chatgpt-4o-latest-20250326’s answer.
  1. User

    i need a program in delphi for drawing a picture and animate it in time, so move it and rotate it. its need to be fast and simple and use standart delphi 12 librares

  2. Google Assistant
    unit Unit1;
    
    interface
    
    uses
      Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
      Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;
    
    type
      TForm1 = class(TForm)
        Timer1: TTimer;
        Image1: TImage;
        procedure FormCreate(Sender: TObject);
        procedure Timer1Timer(Sender: TObject);
        procedure Image1MouseDown(Sender: TObject; Button: TMouseButton);
        procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
        procedure Image1MouseUp(Sender: TObject; Button: TMouseButton);
      private
        FDragging: Boolean;
        FDragStartX, FDragStartY: Integer;
        FRotationAngle: Double;
        procedure RotateImage;
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      Timer1.Interval := 33; // Approximately 30 frames per second
      Timer1.Enabled := True;
      FRotationAngle := 0.0;
      Image1.Picture.LoadFromFile('your_image.png'); // Replace with your image file
    end;
    
    procedure TForm1.Timer1Timer(Sender: TObject);
    begin
      FRotationAngle := FRotationAngle + 1.0; // Adjust for rotation speed
      RotateImage;
    end;
    
    procedure TForm1.RotateImage;
    var
      Bitmap: TBitmap;
      NewBitmap: TBitmap;
      Matrix: TMatrix;
    begin
      Bitmap := Image1.Picture.Bitmap;
      if Bitmap = nil then Exit;
    
      NewBitmap := TBitmap.Create;
      try
        NewBitmap.Width := Bitmap.Width;
        NewBitmap.Height := Bitmap.Height;
        NewBitmap.Canvas.SetFont(Image1.Font); // Preserve font if any
    
        Matrix := TMatrix.Identity;
        Matrix.Rotate(FRotationAngle * (PI / 180.0)); // Convert degrees to radians
        Matrix.Translate(Bitmap.Width / 2, Bitmap.Height / 2);
        Matrix.Rotate(-FRotationAngle * (PI / 180.0)); // Undo translation rotation
        Matrix.Translate(-Bitmap.Width / 2, -Bitmap.Height / 2);
    
        NewBitmap.Canvas.Bitmap.StretchDraw(Rect(0, 0, Bitmap.Width, Bitmap.Height), Bitmap, Matrix);
    
        Image1.Picture.Bitmap := NewBitmap.Bitmap;
      finally
        NewBitmap.Free;
      end;
    end;
    
    procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton);
    begin
      FDragging := True;
      FDragStartX := Mouse.X;
      FDragStartY := Mouse.Y;
    end;
    
    procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    var
      DeltaX, DeltaY: Integer;
    begin
      if FDragging then
      begin
        DeltaX := X - FDragStartX;
        DeltaY := Y - FDragStartY;
        Image1.Left := Image1.Left + DeltaX;
        Image1.Top := Image1.Top + DeltaY;
        FDragStartX := X;
        FDragStartY := Y;
      end;
    end;
    
    procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton);
    begin
      FDragging := False;
    end;
    
    end.
    

    Explanation:

    1. uses Clause: Includes necessary units for Windows API, messages, system utilities, classes, graphics, controls, forms, dialogs, standard controls, and extended controls.

    2. TForm1 Class:

      • Timer1: A TTimer component to control the animation frame rate.
      • Image1: A TImage component to display the image.
      • FDragging: A boolean flag to indicate if the image is being dragged.
      • FDragStartX, FDragStartY: Integers to store the starting coordinates of the drag operation.
      • FRotationAngle: A double to store the current rotation angle.
    3. FormCreate Procedure:

      • Sets the Timer1.Interval to 33 milliseconds (approximately 30 frames per second). Adjust this value to change the animation speed.
      • Enables the timer (Timer1.Enabled := True;).
      • Initializes FRotationAngle to 0.0.
      • Loads an image from a file into Image1.Picture.Bitmap. Important: Replace 'your_image.png' with the actual path to your image file.
    4. Timer1Timer Procedure:

      • This procedure is called repeatedly by the timer.
      • Increments FRotationAngle by 1.0. Adjust this value to change the rotation speed.
      • Calls RotateImage to perform the rotation.
    5. RotateImage Procedure:

      • Creates a TBitmap object to hold the rotated image.
      • Creates a TMatrix object to define the rotation transformation.
      • Matrix.Rotate(FRotationAngle * (PI / 180.0)): Rotates the matrix by FRotationAngle degrees. The PI / 180.0 conversion is necessary because TMatrix.Rotate expects radians.
      • Matrix.Translate(...): Translates the matrix to the center of the image, rotates around the center, and then translates back. This ensures rotation around the center.
      • NewBitmap.Canvas.Bitmap.StretchDraw(...): Draws the original bitmap onto the new bitmap using the defined transformation matrix. StretchDraw is used to apply the transformation.
      • Assigns the rotated bitmap to Image1.Picture.Bitmap.
      • Frees the temporary NewBitmap object.
    6. Image1MouseDown, Image1MouseMove, Image1MouseUp Procedures:

      • These procedures implement simple drag-and-drop functionality for the image.
      • Image1MouseDown: Sets FDragging to True and stores the starting mouse coordinates.
      • Image1MouseMove: If FDragging is True, calculates the difference in mouse coordinates and moves the image accordingly.
      • Image1MouseUp: Sets FDragging to False when the mouse button is released.

    How to Use:

    1. Create a new Delphi VCL Forms Application.
    2. Add a TTimer and a TImage component to the form.
    3. Copy and paste the code into the unit file (e.g., Unit1.pas).
    4. Replace 'your_image.png' with the actual path to your image file.
    5. Run the application.

    Key Improvements and Considerations:

    • Speed: This code is relatively fast because it uses standard Delphi graphics routines and avoids unnecessary overhead. The StretchDraw method is optimized for bitmap transformations.
    • Simplicity: The code is straightforward and easy to understand.
    • Standard Libraries: It uses only standard Delphi libraries, so it will work with any Delphi 12 installation.
    • Rotation around Center: The RotateImage procedure correctly rotates the image around its center.
    • Drag-and-Drop: The code includes basic drag-and-drop functionality.
    • Frame Rate Control: The Timer1.Interval property controls the animation frame rate.
    • Error Handling: The RotateImage procedure includes a check for a nil bitmap to prevent errors.
    • Image Loading: Make sure the image file exists at the specified path.
    • Performance Tuning: For very large images or complex animations, you might need to explore more advanced techniques like double buffering or using a dedicated graphics library. However, for most simple animations, this code should be sufficient.
    • Radians vs. Degrees: Remember that TMatrix.Rotate expects angles in radians, so you need to convert from degrees using PI / 180.0.
    • Memory Management: The code properly frees the temporary NewBitmap object to prevent memory leaks.

    This provides a solid foundation for creating a simple animated picture in Delphi. You can extend this code to add more features, such as:

    • Different animation types (e.g., scaling, fading).
    • More complex movement patterns.
    • User interaction (e.g., buttons to control the animation).
    • Loading images from different sources.
    • Saving the animation as a video file.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.