Mega Code Archive

 
Categories / Delphi / Graphic
 

An introduction to graphics programming

Title: An introduction to graphics programming Question: A brief intro to graphics programming with delphi through a short tutorial. Answer: Start a project called DD04. Save the main form as DD04f1, the unit as DD04U1. Put a button on the form, caption it 'Do it'. The following will draw a line when you click 'Do it': procedure DD04f1.ButtonClick(Sender:TObject); begin DD04f1.canvas.moveto(100,100); DD04f1.canvas.lineto(200,200); end; Note that the moveto/ lineto calls must not be made in the form's OnCreate event handler. You won't see the line. I think it boils down to the idea that you have been trying to draw on something before it exists. Have a look in the Delphi help files under "Drawing lines and shapes" for the wealth of other things you can do. Unfortunetly, something as simple as the above isn't satisfactory. Run the program. Resize the window smaller to cover up some of the line. Now resize it bigger again... the bit of the line you lost has not come back, has it? Remove the 'Do it' button and its OnClick handler. Add to the form a TImage object... you will find it on the 'Additional' tab of the components palette. Make it 400 wide, 100 high, put top and left at 10. Leave it with its default name of Image1. Put the following in the form's OnCreate handler: var Bitmap:TBitmap;(*Provide a suitable variable*) begin(*main of OnCreate*) Bitmap:=TBitmap.create;(*Create a bitmap object*) Bitmap.width:=400;(*Assign dimensions*) Bitmap.height:=100; Image1.Picture.Graphic:=Bitmap;(*Assign the bitmap to the image component*) Image1.Picture.Bitmap.canvas.pen.color:=clRed; (*Have a look at the canvas Help to see all the properties*) Image1.Picture.Bitmap.canvas.moveto(10,10); Image1.Picture.Bitmap.canvas.lineto(300,50); end; Note that for some reason, now you CAN draw on the object even during the forms OnCreate event. This graphic DOES persist, even after the window is resized or covered. The Delphi 1 help file has lots of good stuff in it.. but the hyper-links and index seem faulty to me. Start with a search for drawing lines. Double click on Drawing Lines and Boxes. Now double click on the topic called Drawing Lines and Shapes. From that, you can use 'See Also' to look at Useing Pens and Using Brushes. See also the help material under the "Style property" topic of the result of a search on "Style" -----------------------------------------------------------------Have fun!!! ----------------------------------------------------------------- If you have a graphic in a .BMP file (as produced, say, with Paintbrush) then replace the two lines in the above setting the bitmap width and height with: Bitmap.LoadFromFile('Demo.BMP'); (You must not resize the bitmap after the LoadFromFile... but you can draw on the Bitmap.canvas as before.) If Image1 wasn't big enough for the bitmap you tried to load, you will loose parts of the bitmap. However, even if Image1.autosize equals false, loading a small bitmap into a large image will shrink the image to fit the bitmap.