This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / System.Drawing / Samples / System.Drawing / RegionsGraphicsPath.cs
1 //
2 // Sample application for region graphics functions using GraphicsPaths implementation
3 //
4 // Author:
5 //   Jordi Mas i Hernandez, jordi@ximian.com
6 //
7
8 //
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Drawing.Imaging;
33 using System.Drawing;
34 using System.Drawing.Drawing2D;
35
36 //
37 public class Regions
38 {       
39         
40         public static void Main () 
41         {
42
43                 Bitmap bmp = new Bitmap (600, 300);
44                 Graphics dc = Graphics.FromImage (bmp);                 
45                 Font fnt = new Font ("Arial", 8);
46                 Font fnttitle = new Font ("Arial", 8, FontStyle.Underline);
47                 Matrix matrix = new Matrix ();          
48                 GraphicsPath patha = new GraphicsPath ();
49                 GraphicsPath pathb = new GraphicsPath ();
50                 Pen redPen = new Pen (Color.Red, 2);            
51                 Region rgn1;
52                 Region rgn2;            
53                 int x = 0;              
54                 
55                 SolidBrush whiteBrush = new SolidBrush (Color.White);                           
56                 
57                 dc.DrawString ("Region samples using GraphicsPath", fnttitle, whiteBrush, 5, 5);                                
58                 
59                 /* First*/              
60                 patha.AddLine (60, 40, 90, 90);
61                 patha.AddLine (90, 90, 10, 90);
62                 patha.AddLine (10, 90, 60, 40);                 
63                 dc.DrawPath (redPen, patha);            
64                                 
65                 pathb.AddEllipse(30, 55, 60, 60);
66                 dc.DrawPath(redPen, pathb);
67                                 
68                 rgn1 = new Region (patha);
69                 rgn2 = new Region (pathb);                              
70                 rgn1.Complement (rgn2);
71                 dc.FillRegion (Brushes.Blue, rgn1);                     
72                 dc.DrawString ("Complement (" + rgn1.GetRegionScans (matrix).Length +")", fnt, whiteBrush, 10, 140);    
73                 dc.DrawRectangles (Pens.Yellow, rgn1.GetRegionScans (matrix));
74                 x += 110;
75                 
76                 /* Second*/             
77                 patha.Reset ();
78                 pathb.Reset ();
79                 patha.AddLine (60+x, 40, 90+x, 90);
80                 patha.AddLine (90+x, 90, 10+x, 90);
81                 patha.AddLine (10+x, 90, 60+x, 40);                                     
82                 
83                 dc.DrawPath (redPen, patha);                                            
84                                 
85                 pathb.AddEllipse (30+x, 55, 60, 60);                                            
86                 dc.DrawPath(redPen, pathb);
87                                 
88                 rgn1 = new Region (patha);
89                 rgn2 = new Region (pathb);                              
90                 rgn1.Exclude (rgn2);
91                 dc.FillRegion (Brushes.Blue, rgn1);                             
92                 dc.DrawString ("Exclude ("  + rgn1.GetRegionScans (matrix).Length +")", fnt, whiteBrush, 140, 140);     
93                 dc.DrawRectangles (Pens.Yellow, rgn1.GetRegionScans (matrix));
94                 x += 110;
95                 
96                 /* Third*/              
97                 patha.Reset ();
98                 pathb.Reset ();
99                 patha.AddLine (60+x, 40, 90+x, 90);
100                 patha.AddLine (90+x, 90, 10+x, 90);
101                 patha.AddLine (10+x, 90, 60+x, 40);                     
102                 
103                 dc.DrawPath (redPen, patha);            
104                                 
105                 pathb.AddEllipse (30+x, 55, 60, 60);
106                 dc.DrawPath (redPen, pathb);
107                                 
108                 rgn1 = new Region (patha);
109                 rgn2 = new Region (pathb);                              
110                 rgn1.Intersect (rgn2);
111                 dc.FillRegion (Brushes.Blue, rgn1);             
112                 dc.DrawString ("Intersect ("  + rgn1.GetRegionScans (matrix).Length +")", fnt, whiteBrush, 270, 140);           
113                 dc.DrawRectangles (Pens.Yellow, rgn1.GetRegionScans (matrix));  
114                 x += 110;
115                 
116                 /* Four*/               
117                 patha.Reset ();
118                 pathb.Reset ();
119                 patha.AddLine (60+x, 40, 90+x, 90);
120                 patha.AddLine (90+x, 90, 10+x, 90);
121                 patha.AddLine (10+x, 90, 60+x, 40);                     
122                 
123                 dc.DrawPath (redPen, patha);            
124                                 
125                 pathb.AddEllipse (30+x, 55, 60, 60);
126                 dc.DrawPath (redPen, pathb);
127                                 
128                 rgn1 = new Region (patha);
129                 rgn2 = new Region (pathb);                              
130                 rgn1.Xor (rgn2);
131                 dc.FillRegion(Brushes.Blue, rgn1);              
132                 dc.DrawString ("Xor ("  + rgn1.GetRegionScans (matrix).Length +")", fnt, whiteBrush, 380, 140);         
133                 dc.DrawRectangles (Pens.Yellow, rgn1.GetRegionScans (matrix));  
134                 x += 110;       
135                 
136                 /* Fifth */             
137                 patha.Reset ();
138                 pathb.Reset ();
139                 patha.AddLine (60+x, 40, 90+x, 90);
140                 patha.AddLine (90+x, 90, 10+x, 90);
141                 patha.AddLine (10+x, 90, 60+x, 40);                     
142                 
143                 dc.DrawPath (redPen, patha);            
144                                 
145                 pathb.AddEllipse (30+x, 55, 60, 60);
146                 dc.DrawPath (redPen, pathb);
147                                 
148                 rgn1 = new Region (patha);
149                 rgn2 = new Region (pathb);                              
150                 rgn1.Union (rgn2);
151                 dc.FillRegion(Brushes.Blue, rgn1);              
152                 dc.DrawRectangles (Pens.Yellow, rgn1.GetRegionScans (matrix));
153                 dc.DrawString ("Union (" + rgn1.GetRegionScans (matrix).Length +")", fnt, whiteBrush, 490, 140);                                                        
154                 
155                 bmp.Save("regionsgp.bmp", ImageFormat.Bmp);                             
156         }       
157
158 }
159
160