Intro to Computer Programming
Intro to Computer Programming CSCI 105
Popular in Course
Popular in ComputerScienence
This 23 page Class Notes was uploaded by Trey Daniel on Tuesday October 13, 2015. The Class Notes belongs to CSCI 105 at Lake Superior State University taught by Staff in Fall. Since its upload, it has received 12 views. For similar materials see /class/222332/csci-105-lake-superior-state-university in ComputerScienence at Lake Superior State University.
Reviews for Intro to Computer Programming
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 10/13/15
Bob 5 Burger Barn my Drdgrlnfnrmat n Hamburger French Fne um 1 my Summary NumberMOMem 4 nauarvmmom 5332 SmaHe Order aced 5243 mm Order aced mess Forml Code using using using using using using using using System SystemCollectionsGeneric SystemComponentModel SystemData SystemDrawing SystemLinq SystemText SystemWindowsForms namespace BobsBurgerBarn public partial class Forml Form int numOrdersO double totalOrderValue O minOrder lOOOO maxOrder l FormZ inputForm Form3 summaryForm public Forml lnitializeComponent inputForm new Form2 summaryForm new Form3 private void buttonliclickobject sender EventArgs e const double BURGER7COST 159 SODA7COST 099 int burgers drinks double orderValue inputFormgetOrderlnfoout burgers out fries out drinks orderValue burgers BURGER COST fries FRY7 OST drinks SODAicosT FRYiCOST O 89 MessageBoxShow quotTotal for tnis order quot stringFormatquotO502quotorderValue quotOrder Summaryquot numOrders totalOrderValue orderValue if orderValue gt maxOrder maxOrder orderValue if orderValue lt minOrder minOrder orderValue private void button27Clickobject sender EventArgs e summaryFormshowSummarynumOrders totalOrderValue minOrder maxOrder buttonlFocus private void button37Clickobject sender EventArgs e summaryFormshowSummarynumOrders totalOrderValue minOrder maxOrder Close FormZ Code using using using using using using using using System SystemCollectionsGeneric SystemComponentModel SystemData SystemDrawing SystemLing SystemText SystemWindowsForms namespace BobsBurgerBarn public partial class FormZ Form public Form2 lnitializeComponent private void buttonliclickobject sender EventArgs e Close public void getOrderlnfoout int burgers out int friesout int drinks thisSnowDialog burgers ConvertTolnt32burgerslnText fries ConvertTolnt32frieslnText drinks ConvertTolnt32drinkslnText private void Form27Activatedobject sender EventArgs e burgerslnClear frieslnClear drinkslnClear burgerslnFocus 82 Introduction to Computer Programming Using C Graphics Concepts in Windows One of the most attractive features of graphical user interfaces like Windows is the elegant handling of graphics in a simple device independent manner Prior to the introduction of these environments the programmer needed to worry about the lowlevel hardware characteristics of the video adapter and printer that might be attached to a user39s computer Any time a new piece of equipment hit the market the software developers had to rush out with new device drivers so their products could support the new hardware Under Windows those days are just an unpleasant memory Output devices are treated in a generic manner If a programmer develops an application that works in this generic environment it will behave reasonably in Windows regardless of the particular characteristics of the output device To make matters even better C makes it relatively easy to gain access to the graphics features embedded in Windows In this section we ll take a brief look at some of the C facilities for using graphics In addition we ll take a quick look at creating classes to simplify utilization of those facilities Basic Graphics Concepts in C The simplest graphical elements in C are generated with a Pen and a Brush by drawing on a Graphics variable that has been associated with a C control In their simplest form the Pen and Brush are graphical tools for outlining and filling regions respectively with a particular Color The only real customization we ll apply to the Pen element will be specification of a Width attribute For brushes we ll only use solid brushes of a single color It should be noted though that the programmer can customize these tools in a variety of ways to produce a variety of complex images To create a Graphics variable that is to be associated with the current form you first need to declare a variable out of laziness more than anything else programmers seem to like naming this graphics variable g Graphics 9 Normally you defer assigning a value to this variable until you re ready to use it and enclosing the creation and use of the variable in a using block using 9 thisCreateGraphics 0 instructions to draw on g This particular version associates the variable g with the current form If you instead wanted to draw on one of the controls that s on the form you can instead invoke the CreateGraphicsO function for that control Basic Graphics and Mouse Management Concepts 83 using g Panell Crea teGraphi cs 0 The using block ensures that any errors that might occur are trapped and that any resources used in the drawing process are properly freed Virtually all graphics operations should be performed inside a using block Before examining the specific details of graphical operations in C it is also necessary to consider one further concept the addresses of points associated with a Graphics variable The upper left comer of the variable has the xy coordinates 00 with these addresses increasing from left to right and from top to bottom As noted above when pens and brushes are created a color should be associated with the brush You can store the setting for a color in a variable of type Color and can most readily set a color by using one of the builtin names for colors To obtain a list of these builtin names click on the pulldown tab available in the BackColor attribute of a form and prefixing that name with Color For example C010rBlack is a convenient way to specify the color black A simple pen might be created and used like this Pen p using p new Pen ColorBlack To create a pen with a width greater than 1 instead use this form Pen p using p new Pen ColorBlack width Similarly a solid black brush can be created and used like this Brush b using b new SolidBrush ColorBlack Drawing Lines and Simple Shapes in C Once a pen andor brush have been created it s very easy to use those items to draw on the Graphics component For example to draw a red line from position 20 40 to position 100 120 on the form with a pen that is 3 units wide you could use the instruction sequence using g thisCreateGraphics 0 using pen new Pen ColorRed 3 gDrawLine pen 20 40 100 120 84 Introduction to Computer Programming Using C Of course if you wanted to draw a series of lines you could enclose the series of DrawLine requests in braces Similarly an ellipse or rectangle can be drawn by specifying its brush for a lled shape or pen for an outlined shape the coordinates of the upper lefthand comer of the rectangle in which the shape is to be drawn and the width and height of that bounding rectangle For example a lled rectangle with the upper left comer at position 20 40 and the lower right comer at position 100 120 could be drawn like this gFillEllipsebrush 20 40 100 20 120 40 An outlined rectangle could similarly be drawn by gDraWRectanglepen 20 40 100 20 120 40 If you want a shape to be lled with one color and then outlined with another you should rst draw the lled shape and then draw the outlined shape reversing the order will cause the border to be broken up and jagged Also be sure that the width and height parameters are positive Basic Mouse Management Every time we do anything with the mouse click or release a button or even move the mouse to a new location an quoteventquot is generated If the mouse is positioned over one of the controls on the form then the mouse event is quotownedquot by that control When the mouse is over a region of the form that contains no other controls however the event is owned by the underlying form If you look at the list of events associated with the form remember 7 click the lightning bolt on the Properties window you ll nd several mouserelated events including the following MouseDown Generated whenever one of the mouse39s buttons is pressed MouseUp Generated whenever one of the mouse39s buttons is released MouseMove Generated whenever the mouse is moved The event handlers for these events all have the same header format pri va te voi d Form1MouseXXXX ob j ect sender MouseEven tArgs e The parameters that are conveyed to the handler includes sender the identity of the control that detected the mouse action this is often ignored and the more important and useful MouseEventsArg parameter e which tells you where the mouse is located and which mouse button is being pressed For example within the event handler the position of the mouse can be determined by referencing eX and eY while testing to determine which button is being pressed can be determined by a comparison if eButton MouseButtonsLeft Basm Graphth and Mouse Management Concepts To hllustrate how the information conveyed to a mouserelated event handler can be used let s look at a couple of elementary examples a t t gDrawLine pen 0 o 9x 91 b tttt t if 93utton MauseButtonsLeft 911rawLine pen 0 o Creating aMain Menu Many perhaps most Windows appheatmns Contain a man menu A man nnenn 15 normally found at the top ofone or more ofthe program s chhldwmdows as we To create a mam nnenu se1eet the Menu Strip control and drop at on the form the Vhsual Studm s design wmdow look approth ate1y hhe thhs m nuns Ej j menuStHDl Lntroduetron to Computer Programmmg Uslng ca u ean llck on Type Herequot 39 entry Kyou sink the mouse m that eell the sereen wlll ehange llke thrs u w guess then just arnatter both on the pnmary rnenu smpquot and also on the pullrdown smps below eaeh entry Responding to Menu Clicks w r n n t ttr ulat p n n u ant t n o the vanous llcks To assoerate a Click event handler wth arnenu enh39y1ust doubler llck on that entry on the form and the eyenthandlerwrll be ereated For example lfyou were to type Testing m one of the rnenu eells and then doublercllck that eell an event handler for that eell slmllar to the followmg wlll be ereated mouse h res t private void testin 39oolstripuenultemiclickobject sender EventArgs e l J Basic Graphics and Mouse Management Concepts 87 Using the Windows Color Dialog Tool As noted earlier pens and brushes have a color attribute if you know the name of the color that you want to use you can provide it directly Quite often though an application may need to prompt the keyboard user to select a color For such an instance C allows you to use the standard color dialog box that is a part of Windows The rst step is to select the Color Dialog tool from the tool box and drop it onto your form Like the menu the Color Dialog doesn t have a visual component directly on the form Instead you again need to use the entry below the form in the design window to access the dialog There really aren t very many attributes of the Color Dialog that can be customized The AllowFullOpen attribute can be set to either true or false when set to true the keyboard user can either select one of the displayed color choices or open the Define Custom Color portion of the dialog to have more exibility in de ning a color When set to false only the primary portion of the color selection dialog can be displayed The Color attribute can be set to specify the color that should be highlighted when the dialog box is displayed and will also contain the color that was chosen by the keyboard user A typical usage ofthe Color Dialog box might be something like this if col orDi al 091 ShowDi a1 09 Di a1 ogResul t OK pen new Pen col orDi al 091 Color As you might surmise from the above the Color Dialog box will return the identity of the button that was used to close the box The above code fragment tests to ensure that the OK button was selected before using the selected color to create a pen Tracking Drawing History to Handle Paint Requests There are two different models for screen management in a graphical user interface The more obvious model states that whenever an application covers up a portion of the screen that application is responsible for restoring the screen to its previous state Unfortunately though this could be problematic if the hidden portion of the screen was being changed by an application that is running in the background Therefore Windows and most graphical interface environments use an alternate model Whenever an application covers a portion of the screen it is responsible for notifying the previous application when it subsequently releases the screen Under Windows whenever an application needs to restore all or part of the screen a paint request is generated Paint requests are also used to handle window resize events as well An application s form and any of the standard controls placed on that form will automatically handle their own repaint needs However if your application is drawing directly on the screen 88 Introduction to Computer Programming Using C then you need to take charge of repainting as well Ifyour application s drawing activity is fairly simple this isn t too big of a challenge All you need to do is select the Paint event from the list of available form events and then add instructions to the handler to reconstruct the screen For an application that is generating a more sophisticated image or an image that may be at least under partial control of the keyboard user you ll need to maintain a complete record of the screen details Fortunately C provides two tools that can greatly simplify this task The first of these tools is the class Actually we ve been using classes all along i remember that the form itself is a class The class is a fundamental building block of most modern programming languages and is a combination of data items and functions for operating on those data items In the context of the repainting process we can use a class to keep track of the information needed to draw each piece of the screen image In fact if we go one step further and provide a drawing function as an element of the class we can allow each screen component to paint itself onto the screen as needed We ll discuss the design ofa simple class in the next section The second tool we ll be using is the ArrayList This is a special variable that can be used to hold class variables and we ll use it to hold the individual screen components To use an ArrayList you must first add the directive using System Collections to the set of using directives at the beginning of your program For the remainder of this discussion assume that we ve created a class named ScreenElement that describes the individual components that can be placed on the screen To create an ArrayList variable to hold the actual ScreenElement variables you d need a declaration like the following at the start of your form definition ArrayList screenItems new ArrayList As each screen component is created a variable of type ScreenElement will need to be created If this variable is named nextComponent then it can be added to the screenItems list with the instruction screenItems add nextComponen t Now whenever a repaint request generated the following function could be used to restore the screen The skeleton for this function was created by selecting the Paint event handler from the form s event handler window private void Form1Paint object sender PaintEventArgs e using Graphics 9 thisCreateGraphics foreach ScreenElement s in screenItems sDrawg Basic Graphics and Mouse Management Concepts 89 The foreach is another of C s repetition instructions in this context the instruction is used to cycle through the set of all screenItems pulling them out and redrawing them one by one The nal detail that needs to be covered is removing items from the ArrayList variable To clear the entire list you can use the instruction screenItems Cl ear Alternatively to remove a specific element from the list you can use the instruction ScreenEl emen t x Wha tever screenItems Remove 1 This will remove the rst occurrence of the speci ed item from the ArrayList Introduction to Simple Classes As noted in the previous section a class is a de nition of a structure that contains both data items and functions for manipulating those items Rather than examine classes in great detail though let s just take a quick look at a class that might be useful for managing screen components For simplicity imagine that a screen layout consists of a combination of lines and circles For each line we need to track the end points along with the color of the pen used to draw the line For each circle we ll track the color of the brush as well as the upper left and lower right comers of the box containing the circle We begin by declaring the data components of our class which we ll name ScreenElement cl ass ScreenEl emen t public enum Shape LINE CIRCLE int x1 yl x2 y2 Color shapeColor Shape elementKind The only really new thing here is the rst entry public enum Shape This tells the rest of the world that a Shape refers to either a Line or a Circle This in turn will allow the program that is making use of the ScreenElement class to de ne what kind of shape the element represents The next item in the class is the constructor This is a function that is used to create and initialize a variable of type ScreenElement and looks like this public ScreenElement Shape kind int firstX int firstY int secondX int secondY Color c 8 10 Introduction to Computer Programming Using C elementKind kind if kind ShapeLINE x1 firstX y1 firstY x2 secondX y2 secondY else if firstX lt secondX x1 firstX x2 secondX firstX else x1 secondX x2 firstX secondX if firstY lt secondY y1 firstY y2 secondY firstY else y1 secondY y2 firstY secondY shapeColor c I realize that this seems a bit complicated but remember 7 for a line you need only specify the two endpoints For rectangles or ellipses though you need to specify the upper left comer of the enclosing rectangle along with the width and the height Therefore when I stored the information for a line I just stored the endpoints of the line For a circle though I instead made sure that the first point was the upper left comer and the second point was actually converted to the width and height of the enclosing square Now that the program knows how to create a ScreenElement we ll turn our attention to the other task associated with a ScreenElement e that of drawing the component to a graphics element For this we ll use a function named draw which has a single parameter 7 the Graphics handle to the control on which the element is to be drawn The implementation of this function is straightforward public void drawGraphics g Brush b Pen p if shape ShapeCIRCLE Basic Graphics and Mouse Management Concepts 811 using b new SolidBrush shapeColor gFillEllipseb b x1 y1 x2 y2 else using p new Pen penColor gDrawLinep x1 y1 x2 y2 With this class de nition in place we now have the missing piece that will allow us to fully implement the Paint event handler As each new screen element is constructed your program needs to create a variable of type ScreenElement to hold the details of that component and add it to the collection of all ScreenElement variables as described earlier For example if your program needs to create a blue line with one endpoint at position startXstartY and the other endpoint at endXendY you might use the instruction ScreenEl emen t nex tEl emen t nextElement new ScreenElementShapeLINE startX startY endX endY ColorBlue ScreenItems Add nextEl emen t Similarly a red circle centered at midXmidY with radius size might be created like this ScreenEl emen t nex tEl emen t nextElement new ScreenElementShapeCIRCLE midX size midY size midX size midY size ColorRed ScreenItems Add nextEl emen t Now that the collection of screen elements has been created the PaintO function will be able to repaint the screen s contents any time that any portion of the screen has been covered as well as whenever the screen has been resized 842 Lntroduetlon to Computer Programming Using ca Project Assignments Project 81 The Computerized EtchrArSkelch Some 50 years ago one of the hottest Christmas toys was the EtchrArSketch they39re sull around today in ayanety offorms andthe basic eoneept hasn39t ehanged all that mueh The toy eonslsts sereen When the left knob is turned a horizontal hne is drawn on the sereen while tummg the nght L Vw wd h k W Hr anworkwlll be erased with enough praeuee it39s possible to generate some moderately sophlsueated drawmgs on the sereen Forlhl nrl V 39 L I h L 39H l t thin p of eolors and hne Widths m the drawmgs Step 1 Design the Basis Fnrm As a first step set the form s Windnwstate attnbute to Maximiled and its Cnlnr attnbute to White Then lay out the remamder of the form so it looks apprommately as follows Super ElcnrArskelch Line Wldlh 1 The form eontarns a Panel eomponent on which all other eomponents are to be plaeed The Duck attnbute of tlus panel is set to Tap which Will lock the panel in plaee along the top of the form 39 n th F W Line Width followed by a NumerieUpann eontrol Set the Minimum and NIaximum attributes of the NumerieUpann eomponent to l and 5 respeeuyely The net effect of these settings Will be to e displayedm the Text an eontrol up and down talung on any of the values 1 through 5 Basic Graphics and Mouse Management Concepts 813 The purpose of the three buttons on the panel should be selfevident You could go ahead and implement the event handler for the Quit button since this a fairly trivial task Also add a ColorDialog component to the form to support color selection Step 2 De ning the Necessary Support Variables The program s behavior is really fairly simple If one of the mouse s buttons is pressed then whenever the mouse is moved across the form the program will draw a line in the currently set color using the currently de ned pen width Clicking the Clear button will erase the screen while clicking the Quit button will terminate the program To keep track of the mouse button status add a variable named drawing of type boo set the initial value of this variable to false Whenever a mouse down event occurs this variable will be set to true while the corresponding mouse up events should reset the value to false As mouse movement is detected the value of this variable will be checked to determine whether or not a line should be drawn in response to that mouse movement Since drawing a line requires you to know both the start and end points of the line you ll also need to create a pair of int variables to hold the previous X and y mouse coordinates These variables should be initially set to 0 You ll also need a variable of type Color to hold the current pen color setting this variable should be given the initial value of C010rBlack Finally to support repainting you ll need to add the declaration using System Collections to the start of your program and de ne a variable of type ArrayList to hold all of the lines as they are drawn on the screen perhaps like this ArrayList screenLines new ArrayList Step 3 De ne a Line Class to Support Repainting In order to support repainting of the lines on the screen whenever any portion of the screen is obscured you ll need to de ne a line class that will track the location color and width of any line that is drawn This class should be placed near the end of your program just ahead of the nal left brace The rst portion of the class de nition should contain these entries class Line int x1 y1 x2 y2 int width Color penColor 8 14 Introduction to Computer Programming Using C The purpose of each of these components should be selfexplanatory The next piece of the class de nition is the constructor which is responsible for creating and initializing a variable of type line It s structure is fairly simple public Lineint x1 int yl int x2 int y2 int width Color penColor thisx1 x1 thisyl yl thisx2 x2 thisy2 y2 thiswidth width thispenColor penColor Notice how the keyword this has been appended to several of the variable references The purpose is to specify that the variable being assigned a value is the variable that is declared as part of the form rather than the variable in the parameter list The only remaining element of the class is the draw function which as the name implies is responsible for actually drawing a line on the screen It s definition follows public void drawGraphics g Pen p using p new Pen penColor int width gDrawLinep x1 yl x2 y2 Since this is the end of the class definition you ll also need to add one more closing brace following this function s declaration Step 4 De ning the MouseDown and MouseUp Event Handlers When a mouse button is pressed it indicates that your program is about to begin drawing This is also the best time to determine what the current line width and color should be To support this create an MouseDown event handler for the main form Be sure you create these handlers for the form and not for the panel or any other component on the form Within the MouseDown event handler add instructions to save the current mouse position in the previous X and y coordinate variables that you defined earlier and set the value of the variable drawing to true Basic Graphics and Mouse Management Concepts 815 When the mouse button is released your program should quit drawing the current line segment Therefore the only instruction you need in the MouseUp event handler is drawing false Step 5 De ne the MouseMove Event Handler Whenever a mouse movemen event occurs your program should check to see if a mouse button is currently down If it is you ll need to create a new line variable using the current line color and line width settings save that variable in the ListArray variable named screenLines that you created earlier and draw the line on the screen Finally you ll need to save the endpoint of the line so it can be the starting point of the next line segment First declare a variable g of type Graphics and a variable 1 of type Line Then if the variable drawing is currently true do the following o Initialize the variable 1 to hold the settings for the next line Ifthe variables preVX and preVY hold the previous mouse position and penColor holds the current color setting for the pen you could do this as follows 1 new LineprevX prevY eX eY int numeri cUpDownl Value penColor Add the new line to the set of screen lines so far and draw the line on the screen allLinesAddl using 9 this CreateGraphics 0 l drawg Save the current mouse location for the next line segment prevX e X prevY e Y Step 6 Implement the Color Button s Event Handler When the Color button is clicked your program should display the ColorDialog Col orDi al 091 ShowDi a1 09 After control returns from this dialog box your program should save the value of the color that was selected from the dialog box in the saved pen color variable Recall that this value can be 8 16 Introduction to Computer Programming Using C found by referencing ColorDialog1Color Step 7 Implement the Clear Button s Event Handler When the keyboard user clicks on the New entry in the menu the drawing area must be erased This task can be easily carried out like this Graphics 9 using 9 this CreateGraphics 0 9 Clear this BackCol or In addition you need to discard all of the save shapes from the previous drawing otherwise they ll suddenly reappear if a screen repaint is required To do this use the instruction screenLines Cl ear Finally restore the current pen to its initial default setting of C010rBlack for the pen and reset the value in the NumericUpDown control to 1 Step 8 Add Support for Repaint Requests Presumably your program can now draw lines on the screen in various colors and widths However whenever the ColorDialog opens or any other screenobscuring events occur a part of your drawing will be erased from the screen To ensure that the screen is restored whenever any portion of it is obscured you should create a Paint event handler for your main form and add instructions to redraw the screen by stepping through the ArrayList of screen elements and redrawing each element on the screen using Graphics 9 thisCreateGraphics foreach Line line in allLines linedrawg Step 9 Try Your Hand at Creating Some Masterpieces This should be selfexplanatory Basic Graphics and Mouse Management Concepts 817 Project 82 Simple Sketching For this project you ll be developing a simple drawing program that uses the mouse to sketch geometric shapes on the screen The program will support lines rectangles and ellipses and will allow for changing of the colors of these shapes Step 1 Design the Basic Form As a rst step set your form39s WindowState attribute to Maximized and its BackColor attribute to White Next add a MenuStrip component with 4 main menu entries New Color Shape and Quit Under the Color entry there should be submenu entries for Brush and Pen while the Shape entry should have submenu options for Line Rectangle and Ellipse Finally add a ColorDialog component to the form Also change the caption at the top of the form to read Paint Shop Amateur An Overview of the Program39s Processing Since this program is expected to perform simple pain operations you probably have a good idea of its basic behavior already Selecting New will clear the window Choosing either the Pen or Brush options from the Color menu entry will allow the user to select an outline or ll color Selecting one of the shapes from the Shape submenu will de ne the current drawing shape and choosing Quit will make the program terminate To actually draw something on the screen the keyboard user would rst select the shape and color attributes and then positions the shape on the screen with the mouse Pressing down the mouse button will de ne the object s starting position while releasing the button will de ne the ending position For a line these two points are then connected For an ellipse or rectangle however the shape is drawn inside the rectangle whose opposite comers are de ned by these points Step 2 De ne a Shape Class to Support Repainting As you should be expecting you ll want to de ne a shape class that can be used to describe the individual components as they are drawn This class should be placed near the end of your program just ahead of the nal left brace The rst portion of the class de nition should contain these entries cl ass Shape public ehum Shapes LINE RECTANGLE ELLIPSE int x1 y1 x2 y2 Col or pen Col or brush Col or Shapes shape 818 Introduction to Computer Programming Using C The enumerated item Shapes is used to differentiate between the 3 supported shape types while the remaining variables de ne the screen location of the shape the coloring information for the shape and the actual identi cation of the type of shape The next portion of the class will contain he constructor for a new variable of type Shape public ShapeShapes shape int x1 int yl int x2 int y2 Color penColor Color brushColor thisshape shape thisx1 x1 thisyl yl thisx2 x2 thisy2 y2 thispenColor penColor thisbrushColor brushColor As you can see all that the constructor does is record the information that is passed to it The nal portion of the class is the Draw function For ellipses and rectangles the function rst draws the interior of the shape and then draws the outline For lines the function uses the DrawLine function to connect the endpoints of the line The de nition of this function follows public void drawGraphics g Brush b Pen p using b new SolidBrush brushColor using p new Pen penColor if shape ShapesELLIPSE gFillEllipseb x1 yl x2 y2 gDrawEllipsep x1 yl x2 y2 else if shape ShapesRECTANGLE gFillRectangleb x1 yl x2 y2 gDrawRectanglep x1 yl x2 y2 else The only choice left is a line gDrawLinep x1 yl x2 y2 Basic Graphics and Mouse Management Concepts 819 Since this is the end of the class de nition you ll also need to add one more closing brace following this function s declaration Step 3 De ne the Necessary Support Variables In order to keep track of everything that s going on within this program you ll need to de ne several form variables Two of these of type Color will be used to keep track of the current pen and brush settings and should be initialized to ColorBlack and ColorTransparent respectively You ll also need a variable of type Shape Shapes that will be used to keep track of the currently selected shape I d suggest naming this variable currShape This variable should be initialized to Shape Shapes LINE In order to keep track of all of the shapes that are created by your program you ll need to de ne and initialize a variable of type ArrayList Recall that this will require you to rst add the directive using System Collections at the start of your program The declaration and initialization of this variable might have the form ArrayList screenshapes new ArrayList Finally you ll need to de ne two variables of type int that can be used to keep track of the mouse s location when the mouse button is rst depressed These variables should both be initialized to 0 Step 4 De ne the Mouse Event Handlers As speci ed earlier the program will draw objects in response to MouseDown and MouseUp events When a mouse button is pressed your program needs to save the current position of the mouse as de ned by the event handler s eX and eY parameters in the variables that you just declared for this purpose When the mouse button is released the program needs to create a new Shape variable corresponding to the currently selected shape the current pen and brush color settings the mouse s position when the mouse button was initially pressed and the mouse s position when the button was subsequently released First you ll need to declare a few variables Shape newshape int x1 yl x2 y2 Graphics 9 If the currently selected shape is a line you need to copy the previous x and y mouse coordinates to the variables x1 and yl then copy the current x and y mouse coordinates e X and e Y to the variables x2 and y2 820 Introduction to Computer Programming Using C If on the other hand the shape is either a rectangle or an ellipse you need to set the variable x1 to the smaller of the previous and current x mouse coordinates and set x2 to the difference between the previous and current x mouse coordinates perhaps like this if prevX lt ex x1 prevX x2 eX prevX else x1 eX x2 prevX eX Of course these instructions assume you ve used the variable name prevX for the previous saved x mouse coordinate A similar instruction sequence will then be used to set y1 and y2 to the smaller of the two y mouse coordinates and the difference between the two y mouse coordinates respectively Once these parameters have been de ned you can create the new shape variable draw the shape on the screen and add the shape variable to the collection of all screen elements S new Shapecurrshape x1 y1 x2 y2 penColor brushColor using 9 this CreateGraphics 0 S drawg screenShapesAdd S This code assumes that you used the name currshape for the variable that you de ned earlier to hold the currently selected shape and the names penColor and brushColor for the variables holding the current pen and brush color settings If you used different names you ll have to make the appropriate adjustments At this point you should be able to test the basic paint program Even though the New Shape and Color options aren t implemented you should be able to use the mouse to draw black lines anyway Step 5 Implement the Quit Option This shouldn t really need any explanation by now Simply doubleclick the menu s Quit entry and place a Close instruction in the corresponding event handler Step 6 Implement the Menu s New Event When the keyboard user clicks on the New entry in the menu the drawing area must be erased This task can be easily carried out like this Basic Graphics and Mouse Management Concepts 821 Graphics 9 using 9 this CreateGraphics 0 9 Clear this BackCol or In addition you need to discard all of the save shapes from the previous drawing otherwise they ll suddenly reappear if a screen repaint is required To do this use the instruction screenshapes Cl ear Finally restore the current pen and brush colors along with the current shape to their initial default settings ColorBlack for the pen ColorTransparent for the brush and Shape Shapes Line for the current shape Step 7 Implement the Remaining Menu Actions When the keyboard user selects either the pen or brush color options from the main menu your program should display the color dialog if it still has its initial default setting you d use the instruction ColorDialogl ShowDialog Once the color dialog box has been closed you can then copy the selected color setting to the appropriate pen or brush color variable When a shape selection entry is selected all you need to do is set the current shape variable to match the selected shape For example if the keyboard user clicks on the ellipse entry you could save that selection with the instruction currshape Shape ShapesELLIPSE At this point you should again test your program and make sure everything is working as expected Step 8 Add Support for Repaint Requests Presumably your program at this point can successfully draw all of the required shapes using any color choices you wish However whenever the ColorDialog opens or any of the menu pulldowns are opened portions of your drawing will be erased from the screen To ensure that the screen is restored whenever any portion of it is obscured you should create a Paint event handler for your main form and add instructions to redraw the screen by stepping through the ArrayList of screen elements and redrawing each element on the screen using Graphics 9 thisCreateGraphics foreach Shape s in screenshapes s drawg
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'