Recent

Author Topic: Basic Graphics Question  (Read 7609 times)

chris319

  • New Member
  • *
  • Posts: 28
Basic Graphics Question
« on: March 21, 2012, 05:04:15 pm »
What is the absolute simplest way to draw a rectangle directly on the screen which is likely to work across all common platforms (OS's)? I'm not looking to create an image in an off-screen buffer and transfer it to the screen, I just want to draw a primitive shape directly to the display device.

I have searched all over and the examples I find seem way too elaborate for my simple needs. It should basically boil down to:

selectcolor(r,g,b);

drawrectangle(x, y, height, width);

Thanks.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Basic Graphics Question
« Reply #1 on: March 21, 2012, 05:16:26 pm »
Code: [Select]
  Canvas.Pen.Color := clRed;
  Canvas.Brush.Style := bsClear;
  Canvas.Rectangle(0,0, 100, 100);

chris319

  • New Member
  • *
  • Posts: 28
Re: Basic Graphics Question
« Reply #2 on: March 21, 2012, 05:44:10 pm »
Thanks, but no graphics appear.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Basic Graphics Question
« Reply #3 on: March 21, 2012, 06:08:09 pm »
Which Lazarus version are you using?

chris319

  • New Member
  • *
  • Posts: 28
Re: Basic Graphics Question
« Reply #4 on: March 21, 2012, 06:24:23 pm »
0.9.30.2

Under uses I have both Graphics and FPCanvas. Do I have to set up a canvas first?
« Last Edit: March 21, 2012, 06:28:28 pm by chris319 »

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Basic Graphics Question
« Reply #5 on: March 21, 2012, 06:42:35 pm »
Which OS?

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Basic Graphics Question
« Reply #6 on: March 21, 2012, 07:04:30 pm »
Test the code in Form's onPaint event, or from some button etc. The code that typo gave, draws a small red rectangle to top left corner. If there is TPanel or anything else there to obstruct the drawing, you can't see it because it is drawn on the TForm itself.

If by "drawing to display device" you mean that you need to draw anything you want, anywhere on the screen (even off the form), then there might not be crossplatform way. Some older Windowses, maybe WinXP supported drawing onto DC 0 (desktop) but that is hard to do.

So... if you could explain your needs better, we can give more precise answer on how to draw it.

also, everything given in this thread so far is only drawing "fragile" things. Any slight movement of a dialog over the graphics will erase it off.
« Last Edit: March 21, 2012, 07:10:44 pm by User137 »

chris319

  • New Member
  • *
  • Posts: 28
Re: Basic Graphics Question
« Reply #7 on: March 21, 2012, 09:27:15 pm »
Windows 7 and Mac OS X, but I would like to support earlier versions of Windows (except, say, 95 and 98).

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12768
  • FPC developer.
Re: Basic Graphics Question
« Reply #8 on: March 21, 2012, 09:45:32 pm »
Windows 7 and Mac OS X, but I would like to support earlier versions of Windows (except, say, 95 and 98).

Most people are moving away from ALL win9x support, so Win ME too.  >:D

eny

  • Hero Member
  • *****
  • Posts: 1665
Re: Basic Graphics Question
« Reply #9 on: March 21, 2012, 09:59:03 pm »
Most people are moving away from ALL win9x support, so Win ME too.  >:D
What about win2000?  O:-)
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

chris319

  • New Member
  • *
  • Posts: 28
Re: Basic Graphics Question
« Reply #10 on: March 21, 2012, 10:11:16 pm »
Windows 7 and Mac OS X, but I would like to support earlier versions of Windows (except, say, 95 and 98).
Most people are moving away from ALL win9x support, so Win ME too.  >:D
Except 95, 98, ME and all versions prior to 95. That leaves XP, Vista, 7, 8 and maybe even 2000 and possibly Linux, but Linux is not a must-have.

I have a TForm from which all gadgets have been cleared so as to draw a 20 x 20 pixel box in the upper left corner, and nothing. It is invoked from TForm1.FormCreate(Sender: TObject);.

Quote
everything given in this thread so far is only drawing "fragile" things. Any slight movement of a dialog over the graphics will erase it off.
This will be a level meter for an audio program which will be refreshed every 1/20 second. I won't have other dialogs/gadgets being rendered over the graphics.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Basic Graphics Question
« Reply #11 on: March 22, 2012, 12:03:07 am »
Quote
I have a TForm from which all gadgets have been cleared so as to draw a 20 x 20 pixel box in the upper left corner, and nothing. It is invoked from TForm1.FormCreate(Sender: TObject);.
Surely you won't see anything. You have to do it on FormPaint, otherwise when that part of the form needs to be redrawn (form size change, clipped by other forms, etc.) nothing would be done and that explains why you can't see it (even the paint event gets called when the form is shown for the first time).

chris319

  • New Member
  • *
  • Posts: 28
Re: Basic Graphics Question
« Reply #12 on: March 22, 2012, 01:09:19 am »
Finally got it to work; thanks everyone. Here is the code that gives me what I want:

Code: [Select]
procedure TForm1.PauseButtonClick(Sender: TObject);
begin
  Canvas.Brush.Style := bsSolid;
  Canvas.Pen.Color := clRed;
  Canvas.Brush.Color := clRed;
  Canvas.Rectangle(0, 0, 50, 50);

  Canvas.Pen.Color := clBlue; //MUST CHANGE PEN AND BRUSH
  Canvas.Brush.Color := clBlue;
  Canvas.Rectangle(50, 50, 70, 70);
end;

chris319

  • New Member
  • *
  • Posts: 28
Re: Basic Graphics Question
« Reply #13 on: March 22, 2012, 02:37:33 am »
Now, how can I draw these graphics outside of a user-initiated event? I have a second thread which reads an audio buffer and which needs to refresh an audio meter every 1/20 second on its own. How do I invoke these graphics routines from this other thread?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Basic Graphics Question
« Reply #14 on: March 22, 2012, 03:11:40 am »
Quote
Now, how can I draw these graphics outside of a user-initiated event? I have a second thread which reads an audio buffer and which needs to refresh an audio meter every 1/20 second on its own. How do I invoke these graphics routines from this other thread?
Open this, press Ctrl+F and type Synchronize.

 

TinyPortal © 2005-2018