1. Draw a Rectangle and Two Points - From Creating A Project to Drawing

Back to Cocoa Graphics table of contents


This tutorial will show you the steps from creating a project to drawing a rectangle and two points. Xcode 4.5.2 was used here. I assume you know the basics of Objective-C.

The focus of this tutorial is to show you the steps to draw primitive shapes in a view. Therefore, I will avoid using other techniques not related to Cocoa graphics. In addition, I will not try to draw beautiful things for now. These extra efforts might distract you from graphics. However, I encourage you to play with the code and see the effects, which is a great way to REALLY learn graphics and any other programming techniques. After you gaining the knowledge and skills about Cocoa graphics, you will be able to enjoy generating beautiful drawings and maybe share them with the world.

As the title mentioned, I will show you how to create a project, create a view class, and draw a rectangle and a few points.


Create the basic structure of the program


Create a customed view class - MyView, and draw a blue rectangle


Draw two points

To my knowledge, there is no native support for drawing a point. Instead, we can use rect to draw a point. In the following code, the drawRect: function is revised to change the background to black and draw a white point. An additional function, drawPointAtX: andY:, was created to draw a red point at (x, y). The following is the revised code:

The following is the result.

And, the following is the zipped final project.

RectangleAndPoint.zip