Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#

"Paint.exe" in CsGL

Rate me:
Please Sign up or sign in to vote.
3.91/5 (11 votes)
16 Dec 20037 min read 136.7K   4.3K   37  
Imitate Windows Paint.exe using C# and CsGL
using System;
using System.Drawing;
using CsGL.OpenGL;
using System.Diagnostics;

namespace _2D_draw
{
	/// <summary>
	/// 
	/// </summary>
	public class DrawingObjectOval : DrawingObject
	{
		Point start = new Point();
		Point end   = new Point();
		Rectangle bounds = new Rectangle();
		Curves ellipse = new Curves();

		public DrawingObjectOval()
		{
			strType = "Oval";
			comment = "Oval";
		}

		private void calcRectangle()
		{
			int w = Math.Abs( end.X  - start.X );
			int h = Math.Abs(end.Y  - start.Y );
			bounds = new Rectangle( start , new Size( w , h) ) ;
		}

		public void Draw()
		{
			Draw( GL.GL_RENDER , 0);
		}

		// ******************************************************************
		// draw the ellipse
		public override void Draw( uint mode , uint highlight)
		{
			GL.glLoadName( ID );  // used to select an object
			//System.Console.WriteLine("Draw: " + ID.ToString() + " " + this.strType);
			if ( highlight == ID )
			{
				GL.glColor3ub( 100 , 0 , 200);
			}
			else
			{
				GL.glColor3ub( 0 , 0 , 0);
			}

			calcRectangle();

			// if it's for selection, draw only the rectangle
			if (mode == GL.GL_SELECT)
			{
				GL.glRectd( (double)start.X , (double)start.Y , (double)end.X, (double)end.Y );
			}
			else
			{
				ellipse.DrawEllipse( bounds );
			}
			GL.glFlush();
			//DrawEllipse();
		}

		private void DrawEllipse()
		{
			double  h , k;
			double a , b;

			a = (start.X - end.X)/2.0;
			b = (start.Y - end.Y)/2.0;
			h = (double)start.X + a;
			k = (double)start.Y + b;

			double x , ytop , ybot , delx;
			double X = (double) start.X;

			double s;
			GL.glBegin( GL.GL_LINES );

			int num = 25;
			delx = a/(double)num;

			double xPrev , yTprev , yBprev;
			xPrev = X;
			yTprev = (double)start.Y;
			yBprev = (double)start.Y;

			for ( int i = 1;i<=num;i++)
			{
				x = X + (double)i * delx;
				s = 1 - (x-h)*(x-h) / a * a;
				s = Math.Sqrt(s);
				ytop = k + s;
				ybot = k - s;
				
				GL.glVertex2d( xPrev , yTprev );
				GL.glVertex2d( x , ytop );

				//GL.glVertex2d( xPrev , yBprev );
				//GL.glVertex2d( x , ybot );

				xPrev = x;
				yBprev = ybot;
				yTprev = ytop;

			}

			GL.glEnd();
		}

		public  override void HandleMouseDown(System.Windows.Forms.MouseEventArgs e)
		{
			start = transform( e.X , e.Y );
		}

		public override void HandleMouseUp(System.Windows.Forms.MouseEventArgs e)
		{
			end = transform( e.X , e.Y );
			Draw();
		}

		public override  void HandleMouseMove(System.Windows.Forms.MouseEventArgs e)
		{
			end = transform( e.X , e.Y );
			Draw();
		}



	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions