delphi ile üçgen ,çember vs. çizmede sorun, help me:(

Delphi'de kod yazma ile ilgili sorularınızı bu foruma yazabilirsiniz.
Cevapla
maqro
Üye
Mesajlar: 132
Kayıt: 11 Eki 2005 10:59

delphi ile üçgen ,çember vs. çizmede sorun, help me:(

Mesaj gönderen maqro »

s.a

delphide üçgen,çember vs çiziyorum mouse ile ama çizerken açayip acayip çizgiler çıkıyor. bunları kaldıramazmıyız aynı paint gibi olmasını istiyorum :(
kodum şöyle..

Kod: Tümünü seç

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    GroupBox1: TGroupBox;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    RadioButton4: TRadioButton;
    GroupBox2: TGroupBox;
    RadioButton5: TRadioButton;
    RadioButton6: TRadioButton;
    Timer2: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure RadioButton5Click(Sender: TObject);
    procedure RadioButton6Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Timer2Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  x1,y1:integer;
  basili:boolean;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
timer1.Enabled:=false;
timer1.Interval:=100;
end;

procedure TForm1.RadioButton5Click(Sender: TObject);
begin
timer1.Enabled:=radiobutton1.Checked;
end;

procedure TForm1.RadioButton6Click(Sender: TObject);
begin
timer1.Enabled:=false;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
sekil:integer;
begin
randomize();
canvas.Brush.Style:=tbrushstyle(random(7));
sekil:=random(4);
case sekil of 0: //elips
begin
radiobutton1.Checked:=true;
canvas.Ellipse(random(width),random(height),random(width),random(height));
end;
end;
    end;
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
if (button=mbleft) then
begin
canvas.MoveTo(x,y);
x1:=x;
y1:=y;
basili:=true;
end;
    end;
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
if (basili) then
begin
if (radiobutton1.Checked) then
canvas.Ellipse(x1,y1,x,y);
if  (radiobutton2.Checked)  then
canvas.LineTo(x,y);
if  (radiobutton3.Checked)  then
canvas.Rectangle(x1,y1,x,y);
if  (radiobutton4.Checked)  then
canvas.RoundRect(x1,y1,x,y,25,25);
end;
    end;
procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
basili:=false;
end;



end.
maqro
Üye
Mesajlar: 132
Kayıt: 11 Eki 2005 10:59

Mesaj gönderen maqro »

ilk defa bu foruma bişe yazdım 24 saat geçmesine rağmen cawap yok hayret:(
fduman
Moderator
Mesajlar: 2749
Kayıt: 17 Ara 2004 12:02
Konum: Ankara

Mesaj gönderen fduman »

Sabırlı olalım.. Burada 24 saat görev başında olan maaşlı helpdesk elemanları çalıştırmıyoruz. Bilen birisi mesajını gördüğünde, keyfi de istiyorsa cevap yazar. Ama önce sabır..
maqro
Üye
Mesajlar: 132
Kayıt: 11 Eki 2005 10:59

Mesaj gönderen maqro »

kusura bakma hocam bir anlık gaflet işte:(
Kullanıcı avatarı
Opt2000
Üye
Mesajlar: 216
Kayıt: 09 Tem 2003 10:04

Mesaj gönderen Opt2000 »

Selam,

Aşağıda herhangi bir paintbox üzerine dörtgen, poligon ve çember çizdirebileceğin bir proje gönderiyorum. Uzun gibi duruyor, ama inan bana %80'i hikaye, aslında toplasan sanırım 20-30 satırı gerçekten de dikkat edilmesi gereken kod.

Forma 3 tane RadioButton, 1 tane buton ve bir tane de PaintBox.

RadioButton1 = Dörtgen
RadioButton2 = Poligon
RadioButton3 = Elips
Başlangıçta hiç birisi seçili olmasın. Kodu ona göre yazdım.

Button1'in Enabled değerini false yap ve başlığını da Poligonu çiz olarak değiştir.


Kod: Tümünü seç

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Math;

type TPaintTools = (ptNone, ptRectangle, ptEllipse, ptPolygon);

type                       
  TForm1 = class(TForm)
    PaintBox1: TPaintBox;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    Button1: TButton;
    procedure PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure FormCreate(Sender: TObject);
    procedure PaintBox1Paint(Sender: TObject);
    procedure PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure RadioButton1Click(Sender: TObject);
    procedure RadioButton2Click(Sender: TObject);
    procedure RadioButton3Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    FActiveTool: TPaintTools;
    procedure SetActiveTool(const Value: TPaintTools);
  private
    MouseDownPos:TPoint;
    Buffer:TBitmap;
    Img:TBitmap;
    PolygonPointList:TList;
    property ActiveTool:TPaintTools read FActiveTool write SetActiveTool;
    procedure PreviewPolygon;
    procedure ClearPolygonBuffer;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  pt:PPoint;
begin
  if Button = mbLeft then
  begin
    MouseDownPos := Point(X,Y);

    if ActiveTool = ptPolygon then
    begin
      New(pt);
      pt.X := X;
      pt.Y := Y;
      PolygonPointList.Add(pt);

      PreviewPolygon;
    end;
  end;

end;

procedure TForm1.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  Area:TRect;
begin
  if not (ssLeft in Shift) then Exit;


  case ActiveTool of
    ptRectangle:
    begin
      Buffer.Assign(Img);

      if ssCtrl in Shift then
      begin
        Area.TopLeft := MouseDownPos;

        if (abs(MouseDownPos.X - X) > abs(MouseDownPos.Y - Y)) then
          Area.BottomRight := Point(X, X)
        else
          Area.BottomRight := Point(Y, Y);
      end
      else
        Area:=Rect(MouseDownPos, Point(X,Y));

      Buffer.Canvas.Rectangle(Area);
      PaintBox1.Repaint;
    end;
    ptPolygon:
    begin
      Exit;
    end;
    ptEllipse:
    begin
      Buffer.Assign(Img);

      if ssCtrl in Shift then
      begin
        Area.TopLeft := MouseDownPos;

        if (abs(MouseDownPos.X - X) > abs(MouseDownPos.Y - Y)) then
          Area.BottomRight := Point(X, X)
        else
          Area.BottomRight := Point(Y, Y);
      end
      else
        Area:=Rect(MouseDownPos, Point(X,Y));

      Buffer.Canvas.Ellipse(Area);
      PaintBox1.Repaint;
    end;
  end;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Form1.DoubleBuffered:=true;

  Img:=TBitmap.Create;
  Img.Width:=PaintBox1.Width;
  Img.Height:=PaintBox1.Height;
  Img.PixelFormat:=pf24bit;

  Buffer:=TBitmap.Create;
  Buffer.Assign(Img);

  PolygonPointList:=TList.Create;
end;

procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
  PaintBox1.Canvas.Draw(0, 0, Buffer);
end;

procedure TForm1.PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  case ActiveTool of
    ptRectangle, ptEllipse :
      Img.Assign(Buffer);
  end;
end;

procedure TForm1.SetActiveTool(const Value: TPaintTools);
begin
  FActiveTool := Value;
end;

procedure TForm1.RadioButton1Click(Sender: TObject);
begin
  ActiveTool:=ptRectangle;
  Button1.Enabled:=false;
end;

procedure TForm1.RadioButton2Click(Sender: TObject);
begin
  ActiveTool := ptPolygon;
  ClearPolygonBuffer;
  Button1.Enabled:=true;
end;

procedure TForm1.RadioButton3Click(Sender: TObject);
begin
  ActiveTool:=ptEllipse;
  Button1.Enabled:=false;
end;

procedure TForm1.ClearPolygonBuffer;
var
  I:integer;
  pt:PPoint;
begin
  for I:=0 to PolygonPointList.Count-1 do
  begin
    pt:=PolygonPointList.Items[I];
    Dispose(pt);
  end;
  PolygonPointList.Clear;

end;

procedure TForm1.Button1Click(Sender: TObject);
var
  I:integer;
  Pt:PPoint;
  Points:array of TPoint;
begin

  SetLength(Points,PolygonPointList.Count);
  for I:=0 to PolygonPointList.Count-1 do
  begin
    pt := PolygonPointList.Items[I];
    Points[I].X := pt.X;
    Points[I].Y := pt.Y;
  end;

  Buffer.Assign(Img);
  Buffer.Canvas.Polygon(Points);
  Img.Assign(Buffer);
  PaintBox1.Repaint;
  ClearPolygonBuffer;
end;

procedure TForm1.PreviewPolygon;
var
  I:integer;
  Pt:PPoint;
begin
  if PolygonPointList.Count = 0 then Exit;

  Pt:=PolygonPointList.Items[0];
  Buffer.Canvas.PenPos := Pt^;
  Buffer.Canvas.Ellipse(pt.X - 2, pt.Y - 2, pt.X + 2, pt.Y + 2);
  for I:=1 to PolygonPointList.Count-1 do
  begin
    pt := PolygonPointList.Items[I];
    Buffer.Canvas.Ellipse(pt.X - 2, pt.Y - 2, pt.X + 2, pt.Y + 2);
    Buffer.Canvas.LineTo(pt.X, pt.Y);
  end;

  PaintBox1.Repaint;

end;

end.
Kodu sadece örnek olması için yazdım ve nesne yönelimli programlamaya göre bazı hatalar var. Zaten yarattığım nesnelerin hiçbirisini silmiyorum, ama en azından sana yol gösterecektir. Küçük bir hatırlatma: Bu tür çizim projelerinde direkt orjinal resim üzerine çizdirme. Önce geçici bir resme çizdir, sonra da orjinal resme kopyala. Sana gönderdiğim örnekte de Img orjinal resim, Buffer'da geçici resim.

Bu arada kodu yazdığımda belki online'sındır diye seni MSN listeme ekledim, ama malesef değilmişsin. Eğer kodda anlamadığın bir yer olursa MSN'den sorabilirsin.

Kolay gelsin,
Bahadır Alkaç
maqro
Üye
Mesajlar: 132
Kayıt: 11 Eki 2005 10:59

Mesaj gönderen maqro »

abi seni Allah yolladı bana :) bu werdiğin kodlar tamda benim yapmak istediğim şey. sağol warol @----<-----
Cevapla