Board index » delphi » Graphical 'container' - newbie seeks help

Graphical 'container' - newbie seeks help

Hi there,

I wonder if someone might give me some ideas...

I'm new-ish to Delphi (coming from VB about 6 moths ago).  Settling in just fine.  I'm writing a program to perform a mathematical algorithm called 'bin-stacking' which involves fitting differently sized (but regular shaped) items into bins in the most efficient way - ie using the least number of bins.

So I suppose I need a graphical object that has a capacity (as an integer) and a fill property, as read/write.  Or even a fill method that fills on top of what's there... a bit of ASCII art -

so myContainer.fill(2,'@') might give

|       |
|       |
|       |
|       |
|       |
|@@@@@@@|
|@@@@@@@|
|=======|

and then myContainer.fill(4,'X') would result in

|       |
|XXXXXXX|
|XXXXXXX|
|XXXXXXX|
|XXXXXXX|
|@@@@@@@|
|@@@@@@@|
|=======|

Can anyone give me some beginner's tips?  Is the usual method to draw/paint on the Canvas of a panel, say?

1,000 thanks in advance,
Rob

 

Re:Graphical 'container' - newbie seeks help


A quick clarification =

Quote
When I wrote...

<<.. a bit of ASCII art - >>
<<                         >>
<<so myContainer.fill(2,'@') might give>>

This was a conceptual diagram in text form, I don't want to draw (or fill in) with characters.

Thanks folks,
Rob

Re:Graphical 'container' - newbie seeks help


A TPanel component doesn't have a canvas that you can draw on. What you
should look at for drawing on is probably a TPaintbox. Or you could have a
secondary window (form) in your application that you show as needed and draw
on its canvas, using the whole form.

If you are drawing a lot of the same shapes, you should create yourself one
single bitmap that contains the picture you need (or one bitmap for each
specific object) - you can then use PaintBox.Canvas.Draw(x,y,yourbitmap) to
draw the bitmap at position x,y.

If your symbols are simple enough so that you can easily draw them with GDI
commands (stars, polygons, circles....) then create your own class TSymbol
with a virtual paint method and subclass it for each symbol type overriding
the paint method appropriately.

The class would look something like
    TSymbol=class(TObject)
        Position.TPoint,
        Color:TColor,
        whatever else you need....
        procedure PaintIt(Canvas:TCanvas); virtual,
        end,
A subclass like
    TSymbolA=class(TSymbol)
        any specific properties for this symbol
        procedure Paintit(Canvas:TCanvas), override,
        end;

You then have
    procedure TSymbol.PaintIt,
    begin
    end;

    procedure  TSymbolA.PaintIt,
    begin
        with Canvas do begin
            ...your drawing commands
        end;
    end;

Maintain a list of the symbols you create and in the OnPaint event handler
for the PaintBox do something like
    for K:=0 to SymbolList.count-1 do
        TSymbol(SymbolList[K]).PaintIt(PaintBox.Canvas);

If there are different kinds of symbol in the list it should still find the
correct paintit method.

There are of course a zillion other ways of doing it, but this might make a
start.

Don't paint to a TImage.Canvas unless you really really want to for some
reason.

Quote
Robert <no...@nospam.please.co.uk> wrote in message

news:3d663aa0$1@newsgroups.borland.com...
Quote

> Hi there,

> I wonder if someone might give me some ideas...

> I'm new-ish to Delphi (coming from VB about 6 moths ago).  Settling in

just fine.  I'm writing a program to perform a mathematical algorithm called
'bin-stacking' which involves fitting differently sized (but regular shaped)
items into bins in the most efficient way - ie using the least number of
bins.
Quote

> So I suppose I need a graphical object that has a capacity (as an integer)

and a fill property, as read/write.  Or even a fill method that fills on top
of what's there... a bit of ASCII art -
Quote

> so myContainer.fill(2,'@') might give

> |       |
> |       |
> |       |
> |       |
> |       |
> |@@@@@@@|
> |@@@@@@@|
> |=======|

> and then myContainer.fill(4,'X') would result in

> |       |
> |XXXXXXX|
> |XXXXXXX|
> |XXXXXXX|
> |XXXXXXX|
> |@@@@@@@|
> |@@@@@@@|
> |=======|

> Can anyone give me some beginner's tips?  Is the usual method to

draw/paint on the Canvas of a panel, say?

- Show quoted text -

Quote

> 1,000 thanks in advance,
> Rob

Re:Graphical 'container' - newbie seeks help


Actually, TPanel does have a canvas but it is not exposed.
You can subclass its parent (TCustomPanel) to create a panel with a canvas
and an OnPaint event (for example, see below).

John Williams

____________________________________________________________

unit PaintPanel;

interface

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

type
  TPaintPanel = class(TCustomPanel)
  private
    { Private declarations }
    FOnPaint: TNotifyEvent;
  protected
    { Protected declarations }
    procedure Paint; override;
  public
    { Public declarations }
    property Canvas;
  published
    { Published declarations }
    property Align;
    property Alignment;
    property BevelInner;
    property BevelOuter;
    property BevelWidth;
    property BorderWidth;
    property BorderStyle;
    property DragCursor;
    property DragMode;
    property Enabled;
    property FullRepaint;
    property Caption;
    property Color;
    property Ctl3D;
    property Font;
    property Locked;
    property ParentColor;
    property ParentCtl3D;
    property ParentFont;
    property ParentShowHint;
    property PopupMenu;
    property ShowHint;
    property TabOrder;
    property TabStop;
    property Visible;
    property OnClick;
    property OnDblClick;
    property OnDragDrop;
    property OnDragOver;
    property OnEndDrag;
    property OnEnter;
    property OnExit;
    property OnMouseDown;
    property OnMouseMove;
    property OnMouseUp;
    property OnResize;
    property OnStartDrag;
    property OnPaint: TNotifyEvent read FOnPaint write FOnPaint;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Additional', [TPaintPanel]);
end;

procedure TPaintPanel.Paint;
begin
  inherited Paint;
  if Assigned(FOnPaint) then FOnPaint(Self);
end;

end.
___________________________________________

Quote
"Cleverley Graham" <g...@pt.lu> wrote in message

news:3d6652eb@newsgroups.borland.com...
Quote
> A TPanel component doesn't have a canvas that you can draw on. What you
> should look at for drawing on is probably a TPaintbox. Or you could have a
> secondary window (form) in your application that you show as needed and
draw
> on its canvas, using the whole form.

Other Threads