Autor Beitrag
Massa
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 25



BeitragVerfasst: Mi 16.01.08 20:08 
Hallo zusammen.

Wieder einmal habe ich ein kleines Problem.
Ich habe mir eine kleine Landschaft gezeichnet und möchte nun ein Paar Ballons "steigen lasse". Dies geschieht durch Klick auf ein Button. Meine Ballons haben jetzt aber nur eine Farbe. Was ich aber möchte ist, dass die Farbe zufällig ausgewählt wird. Einzige Bedingung ist das es nur drei Farben sein sollen (rot, gelb und blau). Ich schätze mal, dass man es mit random machen muss nur ich weiß nicht genau wie.
Hier mal der Anfang:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure TForm1.zeichneBallon (xpos,ypos: integer);
  begin
     with ImBild.Canvas do begin
      brush.color:=clRed;                    //     hier muss denke ich mal mit - 
      ellipse(xpos,ypos,xpos+10,ypos+15);    //     random gearbeitet werden
                                                

     end;
  end;



Gruß Massa
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8548
Erhaltene Danke: 477

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Mi 16.01.08 20:13 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
case random(3of
  0: brush.color := clRed;
  1: brush.color := clBlue;
  else brush.color := clGreen;
end;

_________________
We are, we were and will not be.
Yogu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2598
Erhaltene Danke: 156

Ubuntu 13.04, Win 7
C# (VS 2013)
BeitragVerfasst: Mi 16.01.08 20:14 
Hallo,

zuerst solltest du deine Farben definieren. Das machst du mit Konstanten, und zwar so:

ausblenden Delphi-Quelltext
1:
2:
3:
const
  Colors = array[0..2of TColor (clRed, clBlue, clYellow);
  ColorCount = 3;

In deiner Zeichnen-Methode greifst du jetzt, wie du richtig erkannt hast, mit Random auf eine Farbe zu. Vorher musst du aber noch den Zufalls-Generator initialisieren:

ausblenden Delphi-Quelltext
1:
2:
Randomize;
Brush.Color := Colors[Random(ColorCount)];

Wenn du jetzt weitere Farben hinzufügen willst, kannst du einfach die Konstantenwerte ändern. Das ist der Vorteil gegenüber dem Code von user profile iconGausi.

Grüße,
Yogu
Massa Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 25



BeitragVerfasst: Mi 16.01.08 20:17 
Hey vielen dank.

Ich hätte ja nicht so schnell mit einer Antwort gerechnet, aber DANKE.
Es funktioniert auch!

Und auch danke an Yogu.

Gruß Massa