Einzelnen Beitrag anzeigen

bernhard_LA

Registriert seit: 8. Jun 2009
Ort: Bayern
1.144 Beiträge
 
Delphi 11 Alexandria
 
#1

ChatGPT und Bunte Polygone

  Alt 22. Jul 2025, 08:16
ich brauchte eine Funktion welche mir ein Bitmap mit einem Random -Inhalt erszeugt.
Diese Aufgabe konnte ich mittels ChatGPT für das VCL FRamework innerhalb von 2 min lösen , hier der AI Code :

Delphi-Quellcode:
procedure RandomBitmapVCL(xpixel, ypixel: Integer; filename: String);
var
  Bitmap: TBitmap;
  i, shapeType, x1, y1, x2, y2: Integer;
  polygonPoints: array of TPoint;
  randomColor: TColor;
begin
  Randomize;
  Bitmap := TBitmap.Create;
  try
    Bitmap.Width := xpixel;
    Bitmap.Height := ypixel;

    // Fill background with a random color
    Bitmap.Canvas.Brush.Color := RGB(Random(256), Random(256), Random(256));
    Bitmap.Canvas.FillRect(Rect(0, 0, xpixel, ypixel));

    // Draw random shapes
    for i := 1 to 50 do // Adjust the number of shapes as needed
    begin
      shapeType := Random(3); // 0 = rectangle, 1 = ellipse, 2 = polygon
      randomColor := RGB(Random(256), Random(256), Random(256));
      Bitmap.Canvas.Brush.Color := randomColor;
      Bitmap.Canvas.Pen.Color := randomColor;

      case shapeType of
        0: // Rectangle
          begin
            x1 := Random(xpixel);
            y1 := Random(ypixel);
            x2 := x1 + Random(xpixel div 4);
            y2 := y1 + Random(ypixel div 4);
            if Random(2) = 0 then
              Bitmap.Canvas.Rectangle(x1, y1, x2, y2)
            else
              Bitmap.Canvas.FillRect(Rect(x1, y1, x2, y2));
          end;
        1: // Ellipse
          begin
            x1 := Random(xpixel);
            y1 := Random(ypixel);
            x2 := x1 + Random(xpixel div 4);
            y2 := y1 + Random(ypixel div 4);
            Bitmap.Canvas.Ellipse(x1, y1, x2, y2);
          end;
        2: // Polygon
          begin
            SetLength(polygonPoints, Random(3) + 3); // Random polygon with 3-5 points
            for x1 := 0 to High(polygonPoints) do
              polygonPoints[x1] := Point(Random(xpixel), Random(ypixel));
            Bitmap.Canvas.Polygon(polygonPoints);
          end;
      end;
    end;

    // Save bitmap to file
    Bitmap.SaveToFile(filename);
  finally
    Bitmap.Free;
  end;
end;

leider brauchte ich diesen Code auch für das FMX Framework. Dieser Code unten ist dann Handarbeit, weil ChatGPT und CoPilot permanent FMX-Code erstellten der nicht kompiliert hat.
Jede Nachfrage hat das Problem eher verschlimmert, ich hatte das Gefühl 1h Lebenszeit an meinem Programmierpartner verschwendet zu haben.


Delphi-Quellcode:
procedure RandomBitmapFMX(xpixel, ypixel: Integer;
  Filename: String);
var
  Bitmap: TBitmap;
  Canvas: TCanvas;
  i, shapeType, x1, y1, x2, y2: Integer;
  polygonPoints: TPolygon; // Dynamic array to store polygon points

  RandomColor: TAlphaColor;
  numPoints, j: Integer;
begin
  Randomize;
  Bitmap := TBitmap.create;
  try
    // Set bitmap dimensions
    Bitmap.SetSize(xpixel, ypixel);
    Canvas := Bitmap.Canvas;

    // Begin drawing
    Canvas.BeginScene;
    try
      // Fill background with a random color
      RandomColor := TAlphaColorF.create(Random, Random, Random).ToAlphaColor;
      Canvas.Fill.Color := RandomColor;
      Canvas.FillRect(RectF(0, 0, xpixel, ypixel), 0, 0, [], 1);

      // Draw random shapes
      for i := 1 to 50 do // Adjust the number of shapes as needed
      begin
        shapeType := Random(3); // 0 = rectangle, 1 = ellipse, 2 = polygon
        RandomColor := TAlphaColorF.create(Random, Random, Random).ToAlphaColor;

        case shapeType of
          0: // Rectangle
            begin
              x1 := Random(xpixel);
              y1 := Random(ypixel);
              x2 := x1 + Random(xpixel div 4);
              y2 := y1 + Random(ypixel div 4);

              Canvas.Fill.Color := RandomColor;
              Canvas.FillRect(RectF(x1, y1, x2, y2), 0, 0, [], 1);
            end;
          1: // Ellipse
            begin
              x1 := Random(xpixel);
              y1 := Random(ypixel);
              x2 := x1 + Random(xpixel div 4);
              y2 := y1 + Random(ypixel div 4);

              Canvas.Fill.Color := RandomColor;
              Canvas.FillEllipse(RectF(x1, y1, x2, y2), 1);
            end;
          2: // Polygon
            begin
              // Generate a random number of points (between 3 and 5)
              numPoints := Random(3) + 3;

              // Set up the dynamic array to hold the polygon points
              SetLength(polygonPoints, numPoints);

              // Populate the dynamic array with random points within bounds
              for j := 0 to High(polygonPoints) do
                polygonPoints[j] := TPoint.Create(Random(xpixel), Random(ypixel));

              // Convert the array to a TFPolygon object


              try
                Canvas.Fill.Color := RandomColor;
                Canvas.FillPolygon(polygonPoints, 1); // Opacity = 1
              finally
                // staticPolygon.Free; // Proper memory cleanup
              end;
            end;
        end;
      end;
    finally
      Canvas.EndScene;
    end;

    // Save bitmap to file
    Bitmap.SaveToFile(Filename);
  finally
    Bitmap.Free;
  end;
end;

Dieser Post ist also eher für unsere KI Tools, damit die den Unterschied zwischen FMX und VCL Framework endlich mal verstehen und hier was zum Lesen & Lernen haben.


Was sich in den letzten Monaten nach meiner Beobachtung deutlich verbessert hat

  1. Kleinere Funktionen mit einfacher Funktionalität kommen in brauchbarer Qualität zurück.
  2. Er findet Typos und typische Copy-&-Paste-Fehler im Programm.
  3. Er erkennt strukturelle Probleme (z. B. FreeAndNil) auch ziemlich gut.
  4. Ich lasse mir gern den FastMM4-Log von ChatGPT erklären. Das macht die Fehlersuche deutlich einfacher, weil Vorschläge kommen, wo der Fehler liegen könnte
  Mit Zitat antworten Zitat