HOME - - / - - / - - DELPHI COURSE INDEX - - / - - / - - TUTORIALS INDEX - - / - - / - - Other material for programmers

Delphi Course: More chuncky graphics

This page is information rich, and a has search button at the bottom of the page.

Please don't dismiss it because it isn't full of graphics, scripts, cookies, etc!



You will probably find the text easier to read if you make your browser window much narrower than usual. You may also want to change your browser's zoom level, to enlarge the text. Opera (at least) lets you change zoom level easily. The text will adapt nicely to the settings you decide give the best results for your needs!

The lines of sample Delphi code in these pages will not "wrap". I.e., if a line is too long to show in the width you have set your browser too, parts of the line will be "off the page". Those lines will still copy/paste properly, at least in Opera. Please feel free to send feedback on the choices I've made! (Will you forgive me for not forcing upon you a column of links on the left and a column of ads on the right?)

You may find typos and rough edges in this. None-the-less, the basic information should be accurate. If something seems wrong, or if you find I've assumed knowledge without explaining it in a previous lesson, please let me know. Please forgive matters of typos, etc. for now. I am not inherently sloppy! The blemishes will be dealt with later.




In the first lesson on graphics, we made pretty patterns, but crudely. In the second lesson, we made a start with "proper", pixel based graphics. In this lesson, we revisit doing things with characters. Most of this lesson is not terribly difficult, merely consolidating some important things already touched upon. We do introduce sliders.

Delphi language:
--- For ... := ... To ... Do ... revisited
--- Begin ... End revisited
--- Buttons' "Enabled" property

Delphi component:
--- Slider


This lesson will produce several small applications which draw pretty patterns on the screen. I hope you will be "inspired" to tweak the applications to vary the pattern. One of the great joys and opportunities in programming is the chance to be creative. Make sure you can do the things described in the lesson, but also explore the "I wonder if I can..." ideas which I hope you will have. Look for some, if they don't come by themselves!

Start a new application. Rename the form graph3f1. Save the unit as graph3u1. Save the project as graph3.

Put a memo on the form, filling most of it. Put a button on the form; rename it buDoIt, caption it "Do It".

Put the following into buDoIt's Onclick handler. It is overkill for the moment, but you will see as we go along why the various parts were incorporated....
procedure Tgraph3f1.buDoItClick(Sender: TObject);
var iCount,iCount2,iStart,iEnd:integer;
    sTmp:string;
begin
iStart:=1;
iEnd:=5;
memo1.lines.clear;
for iCount:=iStart to iEnd do
   memo1.lines.add(inttostr(iCount));
end;//buDoItClick
Save it. Run it. You should see 1,2,3,4,5 in a column... and should understand where the numbers came from!

Just like only "one" thing should follow if... then..., as discussed in an earlier lesson, only "one" thing should follow the do of a for... to... do statement. And, as before, we can "cheat" by surrounding any number of things with a begin and an end, thus making the lot of them count as "one thing".

Alter what we had before slightly, making it...
procedure Tgraph3f1.buDoItClick(Sender: TObject);
var iCount,iStart,iEnd:integer;
    sTmp:string;
begin
iStart:=1;
iEnd:=5;
memo1.lines.clear;
for iCount:=iStart to iEnd do begin
   memo1.lines.add(inttostr(iCount));
   memo1.lines.add('=====');
   end;//do begin...
end;//buDoItClick
Run it.. you should get...
1
=====
2
=====
3
=====
4
=====
5
=====
... and understand why that's what you got.

Now we're going to do something clever: We're going to put a loop within the loop.

Rewrite the OnClick handler to make it...
procedure Tgraph3f1.buDoItClick(Sender: TObject);
var iCount,iCount2,iStart,iEnd:integer;
    sTmp:string;
begin
iStart:=1;
iEnd:=5;
memo1.lines.clear;
for iCount:=iStart to iEnd do begin
   sTmp:='';
   for iCount2:=iStart
      to iCount+iStart-1 do sTmp:=sTmp+'* ';
   memo1.lines.add(sTmp);
   end;//do begin...
end;//buDoItClick
Run it. You should get....
*
* *
* * *
* * * *
* * * * * 
Really not a lot of new stuff yet. That's about to change.

From the Win95 tab of the Component Palette, obtain two TrackBars; place them on your form, side by side. Rename them tbStart and tbEnd. Put a lable under each, naming them laStart and laEnd.

Our pattern drawing is going to be able to start with any number of stars (within reason), and end with any number. The numbers are going to be set by using the TrackBars.

Set tbStart's "min" property to 1.
Set tbEnd's "min" property to 2.

Set tbStart's "max" property to 11.
Set tbEnd's "max" property to 12.

Set tbStart's "position" property to 1.
Set tbEnd's "position" property to 5.

Make laStart.caption '1'
Make laEnd.caption '5'

Change the first two lines in buDoIt's Onclick handler to...
iStart:=tbStart.position;
iEnd:=tbEnd.position;


Run the program. You should get....
*
* *
* * *
* * * *
* * * * * 
again.

Give tbStart the following OnChange handler...
procedure Tgraph3f1.tbStartChange(Sender: TObject);
begin
laStart.caption:=inttostr(tbStart.position);
if tbStart.position<tbEnd.position
  then
     buDoIt.enabled:=true // no ; here
  else
     buDoIt.enabled:=false;
end;//tkStartChange
.. and give tbEnd a similar Onchange handler...
procedure Tgraph3f1.tbEndChange(Sender: TObject);
begin
laEnd.caption:=inttostr(tbEnd.position);
if tbStart.position<tbEnd.position
  then
     buDoIt.enabled:=true // no ; here
  else
     buDoIt.enabled:=false;
end;//tkEndChange
Use the Object Inspector to make tbEnd use the same handler for its OnChange event.


Why go to all the trouble we have gone to?

If you use a trackbar to get input from the user, there is no chance that the user will type in something non-numeric where a number is required.

You can restict users to a sensible range of start and end values.

Notice how the "Do It" button gets greyed out, becomes unusable, when a user specifies an illogical request, e.g. "Do it from 4 stars to 2 stars."

That does it for this lesson! I said it would be easier than some. I hope you thought it was.




Click here if you're feeling kind! (Promotes my site via "Top100Borland")
   Search this site or the web        powered by FreeFind
 
  Site search Web search
Site Map    What's New    Search
Ad from page's editor: Yes.. I do enjoy compiling these things for you... I hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!
Click here to visit editor's Sheepdog Software (tm) freeware, shareware page.

Link to Tutorials main page Link to Delphi Course index
Here is how you can contact this page's author, Tom Boyd.


Valid HTML 4.01 Transitional Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org


If this page causes a script to run, why? Because of things like Google panels, and the code for the search button. Why do I mention scripts? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....