8/16/2023 0 Comments Tutorial mydraw program![]() Change it so that it adds a new MyRectangle when the left mouse button is clicked. Switch to Program and locate the Main method. So we should be able to add a MyRectangle to our Drawing. ![]() Why is that important? Well, you can Add a Shape objects to our program's Drawing object. It can do everything a Shape can - after all, it inherited all of Shape's features, so it really is a Shape. The great thing about MyRectangle, in terms of our program, is that it looks and feels like a Shape. Now let us make use of this new MyRectangle. Object Oriented Programming Pass Task 4.1 – Drawing Program: Different Shapes Note: What we have looked at so far is inheritance, at its core that is all that it is. Shape _color: Color 0.-1_X_y: Integer _width, _height: Integer _shapes _selected: Boolean Drawing + Shape() + Shape ( color, x, y, width, height) + X : Integer property> + Y : Integer property > + Width: Integer > + Selected :: Boolean property> + Draw + Draw Outline + IsAt pt: Point2D ) :: Boolean My Rectangle of 13 All of these fea- tures are inherited from the Shape class. You can ask a MyRectangle object to Draw, to Drawoutline, and if it IsAt a point. That means that at the moment, a MyRectangle knows its location (X.y), its size (width, height), its color, and if it is selected. This code gives MyRectangle all of the features of Shape. public class MyRectangle : Shape Note: You now have the code setup as shown in the following diagram. ![]() Make MyRectangle inherit all of the features from the Shape class. Lets start transforming our program so that it creates the family of Shape types, and uses these in the Drawing 1. As we work through the tutorial we will try to guide your thinking so that you can understand it more fully. Object Oriented Programming Pass Task 4.1 – Drawing Program: Different Shapes Note: Inheritance and polymorphism takes some time to understand, so don't worry if you struggle to understand it at first. Integer property - Une -Line color star X, startendx, andy) EndxFlow property Ende: Float xproperty> Drawoutine + Al(pl: Pinzo Boon Draw Outline Apt Pointa): Bolan Drawoutline Apt Pon): Boolean of 13 Drawing Shape GOCIO Floe shaped selected Bin Shapecolor) X: Fog property Y: Float property - Selected: Bodien property Dondurmesemates (pt Point2o) : Boolean abstracts WHOctangla MyLine _endx, _endy: HOE + rectango + Rectangle (color, x, y, with height) Widt Inger property Height: agercroperty Jednog Cro Crie (color, radius) Radhus. This is read MyRec- tangle is a kind of Shape etc. In a UML class dia- gram inheritance is drawn as a line with a hollow, triangular, arrow head. Each of these classes is a kind of Shape, with each class providing its own custom draw implementation. There will be three new classes to repre- sent three different types of shapes we want to support. The following UML class diagram shows the inheritance relationships (read as is-a or is-a- kind-of) that are going to be created in this program. With the Shape Drawing program, we can use this principle to create a family of Shape classes – each of which draws in its own way. Different classes can inherit com- mon features from a parent class (also know as base class or super class), and add new features or change how inherited features work. With object oriented programming, inheritance can be used to create families of related ob- jects that can be used interchangeably within the program. This may be the correct approach in some cases, but there are challenges if you want to store different data associated with the different kinds of objects. One way this could be achieved is with an enumeration and a case statement to determine how methods of the object will behave when different actions are requested. In the shape drawing program we want to be able to draw a number of different kinds of shapes including rectangles, circles, and lines. What is needed is the ability to use a number of different kinds of shapes. Currently, however, all of the shapes are rectangles. So far you have the ability to create a Drawing object that contains many Shape objects. Object Oriented Programming Pass Task 4.1 – Drawing Program: Different Shapes Instructions This task continues the Shape Drawer from the previous topic. SplashKit.DrawRectangle(SplashKit.ColorBlack(), _x - 2, _y - 2, SplashKit.FillRectangle(_color, _x, _y, _width, _height) } while (!SplashKit.WindowCloseRequested("Shape Drawer")) If (SplashKit.KeyDown(Ke圜ode.DeleteKey) ||įoreach (Shape s in myDraw.SelectedShapes) MyDraw.SelectShapesAt(SplashKit.MousePosition()) If (SplashKit.MouseClicked(MouseButton.RightButton)) MyDraw.Background = SplashKit.RandomColor() ![]() If (SplashKit.MouseClicked(MouseButton.LeftButton))
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |