Vous êtes sur la page 1sur 4

Tutorial 3: Drawing Shapes In this tutorial, you build a program that demonstrate how to use the main drawing

tools such as Lines, Polygons, Arcs, Circles, Pies to construct a simple image. You learn how to: Create a new project. Use drawing tools to add different shapes to form. Fill the drawn regions. Add some tips to form, such as the current location of the mouse. When you finish, your program will look like the following picture.

Dr. Mohammed Fadhl

Computer Graphic Lab

Step 1: Step 2: Step 3:

Create a Windows Forms Application Project. Set Your Form Properties. Add Buttons controls to your form. Rename your buttons to something more meaningful. Write Codes, and run your program.

************************************* using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Linq; namespace BOAT { public partial class BOAT : Form { . . . . . . private void Form1_MouseClick(object sender, MouseEventArgs e) { ToolTip n = new ToolTip(); string s = "You are here: "+ e.Location.ToString() ;

n.SetToolTip(this, s);
}

Graphics g ; Pen mypen = new Pen(Color.Chocolate, 3);


private void Pie_Click(object sender, EventArgs e) { g = this.CreateGraphics(); g.DrawPie(mypen, 100, 100, 200, 150, 0f, 180f); } private void Line_Click(object sender, EventArgs e) { g = this.CreateGraphics(); g.DrawLine(mypen, 246, 176, 246, 56); g.DrawLine(mypen, 246, 56, 151, 148); g.DrawLine(mypen, 151, 148, 246, 176);
Dr. Mohammed Fadhl Computer Graphic Lab 2

} private void Arc_Click(object sender, EventArgs e) { g = this.CreateGraphics(); g.DrawArc(mypen, 225, 55, 50, 120, 90f, 180f); } private void Polygon_Click(object sender, EventArgs e) { g = this.CreateGraphics(); Point[] p ={new Point (1,229), new Point (1,378), new Point (571,378), new Point (571, 229)}; Pen pp = new Pen(Color.Blue, 3); g.DrawPolygon(pp, p); } private void FillPoly_Click(object sender, EventArgs e) { g = this.CreateGraphics(); Point[] p ={new Point (1,229), new Point (1,378), new Point (571,378), new Point (571, 229)}; g.FillPolygon(Brushes.Blue, p); } private void Clear_Click(object sender, EventArgs e) {

Invalidate();
} } }

Things to do: 1. 2. 3. 4. Use other tools such as circles, ellipses, etc. Add more shapes to the project, such as sun, men, birds, cloads, .. etc Try to add animation to the project, say try to make the boat moving. Try to save the created image in any image format.

Dr. Mohammed Fadhl

Computer Graphic Lab

Dr. Mohammed Fadhl

Computer Graphic Lab

Vous aimerez peut-être aussi