Autor Beitrag
mccom
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 59



BeitragVerfasst: Di 31.01.06 20:05 
Erstmal hallo, so als ganz neuer gleich eine Farge zu stellen ist hoffe ich nicht unhöflich^^ So, worum gehts. ich hab ein DrawGrid, wenn man ein Feld anklickt, soll es die Farebn wechseln. Also erst Weiß, dann rot, dann grün dann wieder weiß. Die Farben sollen als Integer wert (0,1,2,3) in einer Array gespeichert werden.

gefunden habe ich einen Script der Mit boolean also Ja/nein Wiß/rot einfärbt. cih denke das kann cih ja verwenden?

Erstmal den Org script:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
private
    arr:array[0..4,0..4] of boolean; //der einfachheithalber statisch
    ...
procedure TForm1.FormCreate(Sender: TObject);
Var x,y:integer;
begin
  with drawgrid1 do
    begin
      FixedCols := 0;
      Fixedrows := 0;
      rowcount  := 5;
      colcount  := 5;
    end;
  for x:=0 to 4 do
   for y:=0 to 4 do arr[x,y]:=false;  //array löschen
end;

procedure TForm1.DrawGrid1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
Var arow,acol:integer;
begin
  DrawGrid1.MouseToCell(X, Y,ACol, ARow);
  arr[acol,arow]:= not arr[acol,arow];
  DrawGrid1.Repaint;  //Grid komplett neu zeichnen, nicht ideal ...
end;

procedure TForm1.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  with sender as TDrawgrid do
    begin
      if arr[acol,arow] then canvas.brush.color := clred
                        else canvas.brush.color := clwhite;
      canvas.fillrect(rect);
    end;
end;
end;


Dann meine Version:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
private
    { Private-Deklarationen }
    ar_map: array of array of integer;
...
procedure TForm1.Button1Click(Sender: TObject);
var x,y,x2,y2: integer;
begin
     x :=StrToIntDef(x_koord_zahl.Text, 1);
     y :=StrToIntDef(y_koord_zahl.Text, 1);
     map.ColCount:= x+1;
     map.RowCount:= y+1;
     SetLength(ar_map, x,y);

  for x2:=0 to x do
   for y2:=0 to y do ar_map[x2,y2]:=0;  //array löschen
end;

procedure TForm1.mapDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
    with sender as TDrawgrid do
    begin
      if ar_map[acol,arow] = 1 then canvas.brush.color := clred
 else if ar_map[acol,arow] = 2 then canvas.brush.color := clgreen
 else canvas.brush.color := clwhite;
      canvas.fillrect(rect);
    end;
end;

procedure TForm1.mapMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
Var arow,acol:integer;
begin
  map.MouseToCell(X, Y,ACol, ARow);
  ar_map[acol,arow]:= not ar_map[acol,arow];
  map.Repaint;  //Grid komplett neu zeichnen, nicht ideal ...
end;


Das problem liegt hier: if ar_map[acol,arow] = 1 then canvas.brush.color := clred

fehler meldung ist ein EAcessViolant => zugriffsverletzung...

Kann mir jemand helfen? ein einfacher fehler?

Moderiert von user profile iconChristian S.: Quote- durch Delphi-Tags ersetzt
Logikmensch
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 390

Win XP
Delphi 2007 Prof., XE2, XE5
BeitragVerfasst: Mi 01.02.06 06:44 
Titel: DrawGrid Probleme
Hallo und herzlich willkommen.

Der Fehler besteht darin, dass Du die Array-Grenzen zu hoch setzt. Bei setlength(ar_map,x,y) darfst Du maximal bis x-1,y-1 gehen. Außerdem kann mapDrawCell schon vor Deiner Initialisierung aufteten. Ich habe daher ein bisschen korrigiert und so funzt es. Übrigens ein nettes kleines Progrämmchen! :-)

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
procedure TForm1.Button1Click(Sender: TObject);
var x,y,x2,y2: integer;
begin
  with map do begin
     x :=StrToIntDef(x_koord_zahl.Text, 1);
     y :=StrToIntDef(y_koord_zahl.Text, 1);
     ColCount:= x+1;
     RowCount:= y+1;
     SetLength(ar_map, ColCount,RowCount);
  end; {with}
  for x2:=0 to x-1 do
   for y2:=0 to y-1 do ar_map[x2,y2]:=0;  //array löschen
  map.Repaint;
end;


procedure TForm1.mapDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
    with sender as TDrawgrid do
    begin
      if ar_map[acol,arow] = 1 then canvas.brush.color := clred
 else if ar_map[acol,arow] = 2 then canvas.brush.color := clgreen
 else canvas.brush.color := clwhite;
      canvas.fillrect(rect);
    end;
end;


procedure TForm1.mapMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
Var arow,acol:integer;
begin
  map.MouseToCell(X, Y,ACol, ARow);
  ar_map[acol,arow]:= ar_map[acol,arow]+1;
  map.Repaint;  //Grid komplett neu zeichnen, nicht ideal ...
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  with map do
    setlength(ar_map,colcount,rowcount);
end;


Liebe Grüße und viel Spaß!

Claus.
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Mi 01.02.06 08:10 
Hallo,
user profile iconmccom hat folgendes geschrieben:
...
Also erst Weiß, dann rot, dann grün dann wieder weiß.
...

die Codezeile Nr.: 35 von user profile iconLogikmensch :
ausblenden Delphi-Quelltext
1:
  ar_map[acol,arow]:= ar_map[acol,arow]+1;					

würde ich durch die folgende case-Konstruktion ersetzen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
case ar_map[acol,arow] of
  0 : ar_map[acol,arow] := 1; 
  1 : ar_map[acol,arow] := 2; 
  2 : ar_map[acol,arow] := 0;
end;

damit es nach grün auch wieder weiß werden kann. :wink:

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
mccom Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 59



BeitragVerfasst: Mi 01.02.06 15:40 
Ihr seid beide super! hab beide änderungen eingefugt, hab jetzt auch den Rand weggemacht, also gibt es keien Festen ränder mehr, passt besser in mein Projekt.

ich hätte ncoh zwei fragen:

1. Muss hier nciht gepostet werden nur sagt mir bitte nach WAS ich suchen soll. Ich bräcuhte eine liste aller farebn die Delphi da anzeigen kann. clblack, clwhite etc ist klar, nur was geht noch?

2. Wenn man neben das Gitter klickt kommt ne exception. Ist klar, die Koordinaten sidn ja nicht in der Array! nur jetzt ne Frage, wie kann ich das abfangen? soll ich testen ob die Kooords NICHT im rahmen sidn? oder werden gar keien zurückgegeben? Also wie wäre so eine abfangroutine am besten zu realisieren?
Logikmensch
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 390

Win XP
Delphi 2007 Prof., XE2, XE5
BeitragVerfasst: Fr 03.02.06 05:57 
Titel: Farben und DrawGrid-Exception
Hallo!

Zu Frage 1: Es gibt theoretisch 16 Millionen Farben, wenn man bedenkt, dass TColor vom Prinzip her ein Integer ist. Du willst allerdings konkrete Farben haben. Nun, um die meisten Konstanten zu sehen, klicke z.B. mit der rechten Maustaste auf clRed und wähle aus "Deklaration suchen". Delphi sollte dann die Unit Graphics öffnen, in der einige der Farbkonstanten drinstehen.

Zu Frage 2: MouseToCell ergibt bei den Zellkoordinaten -1, wenn Du nicht in gültigen Zellen reinklickst. Hier sollte ein einfaches

ausblenden Delphi-Quelltext
1:
if (acol>=0) and (arow>=0) then ar_map[acol,arow]:= ar_map[acol,arow]+1;					


genügen. Falls nicht, dann prüfe auch noch die Obergrenzen des Arrays ab (wie gesagt maximal ColCount-1 bzw. RowCount-1).

Viel Spaß!

Claus.
mccom Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 59



BeitragVerfasst: Fr 03.02.06 16:06 
vielen dank! alles erledigt! super schnell reagiert! bis bald!