In this example, a sine curve must be drawn directly on the form. To prevent incomplete graphics from being output during window switching, the key to drawing images is Code The Code is as follows:
Procedure tform1.formpaint (Sender: tobject );
VaR
X: integer;
Y, A: Double;
Begin
Canvas. Pen. Width: = 3;
Canvas. moveTo (0, trunc (self. clientheight/2 ));
For X: = 0 to self. clientwidth do
Begin
A: = (x/self. clientwidth) * 2 * PI;
Y: = sin ();
Y: = (1-y) * Self. clientheight/2;
Canvas. lineto (trunc (x), trunc (y ));
End;
End;
ProgramFirst, set the width of the paint brush in the canvas object of the form, and move the starting point of the drawing to the position (0, trunc (self. clientheight/2) of the form. Then a: = (x/self. clientwidth) * 2 * the PI statement converts the angle value in a sine period to the radian value, and stores the result of the sine calculation in the Y variable. Finally, the canvas. lineto (trunc (X) and trunc (y) statements in the loop will draw a continuous sine curve on the form.
The program code is as follows:
Unit unit1;
Interface
Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, extctrls, stdctrls;
Type
Tform1 = Class (tform)
Procedure formpaint (Sender: tobject );
Procedure formresize (Sender: tobject );
Procedure formcreate (Sender: tobject );
Private
{Private Declarations}
Public
{Public declarations}
End;
VaR
Form1: tform1;
Implementation
{$ R *. DFM}
Procedure tform1.formpaint (Sender: tobject );
VaR
X: integer;
Y, A: Double;
Begin
Canvas. Pen. Width: = 3;
Canvas. moveTo (0, trunc (self. clientheight/2 ));
For X: = 0 to self. clientwidth do
Begin
A: = (x/self. clientwidth) * 2 * PI;
Y: = sin ();
Y: = (1-y) * Self. clientheight/2;
Canvas. lineto (trunc (x), trunc (y ));
End;
End;
Procedure tform1.formresize (Sender: tobject );
Begin
Refresh;
End;
Procedure tform1.formcreate (Sender: tobject );
Begin
Self. doublebuffered: = true;
// Prevent image flashing
End;
End.
Save the file and press F9 to run the program. When the program is running, the sine curve is automatically drawn on the form, as shown in result 1.
Figure 1 program running result
Through the study of this program, we can not only draw a sine curve, but also draw a cosine curve, tangent curve and other kinds of curves, convenient Scientific Research