5. COMPUTING WITH GRAPHICS OBJECTS Dennis Y. W. Liu and Rocky K. C. Chang October 8, 2015 (Adapted from John Zelle’s slides)

Slides:



Advertisements
Similar presentations
Python Programming: An Introduction to Computer Science
Advertisements

Graphics Shapes. Setup for using graphics You have to import the graphics library You can use either “import graphics” or “from graphics import *” or.
Noadswood Science,  To know how to use Python to produce windows and colours along with specified co-ordinates Sunday, April 12, 2015.
Python Programming, 2/e1 CS177: Programming in Multimedia Objects Recitation Topic: Graphics Library.
Why ROOT?. ROOT ROOT: is an object_oriented frame work aimed at solving the data analysis challenges of high energy physics Object _oriented: by encapsulation,
Creating an OOED Application
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Flowchart Start Input weight and height
1 Python Programming: An Introduction to Computer Science Chapter 3 Objects and Graphics.
Chapter 3 Planning Your Solution
Chapter 4 Objects and Graphics
Python: Graphics
Microsoft® Small Basic The Controls Object Estimated time to complete this lesson: 1 hour.
CSC 110 Objects and Graphics [Reading: chapter 4] CSC 110 E 1.
1 Bitmap Graphics It is represented by a dot pattern in which each dot is called a pixel. Each pixel can be in any one of the colors available and the.
Slides adapted from: Bjarne Stroustrup, Programming – Principles and Practice using C++ Chapter 12 A display model Hartmut Kaiser
Chapter 5 Graphics.  We’ve been doing command line programming, but now it’s time to try GUI programming—programming in a graphical user interface, using.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
Creating your own form from scratch.. To create a custom form, you can modify an existing form or design and create a form from scratch. In either case,
Computers Are Your Future © 2006 Prentice-Hall, Inc.
Getting Started With AutoCAD ENGR 2 Week #1 Laboratory.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved CheckWriter Application Introducing Graphics and Printing.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 12: A Display Model 1 Based on slides created by Bjarne.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 2 Graphics Programming with C++ and the Dark GDK Library Starting.
Creating a 7-Circuit Right-Handed Cretan Labyrinth 11 Steps in Designing Your Own Labyrinth.
Chapter Two Creating a First Project in Visual Basic.
CSCE 121: Introduction to Program Design and Concepts, Honors Dr. J. Michael Moore Spring 2015 Set 15: GUIs 1.
Making Python Pretty!. How to Use This Presentation… Download a copy of this presentation to your ‘Computing’ folder. Follow the code examples, and put.
Some Graphics CS303E: Elements of Computers and Programming.
Graphical User Interface You will be used to using programs that have a graphical user interface (GUI). So far you have been writing programs that have.
Visual Basic Programming Introduction VB is one of the High level language VB has evolved from the BASIC language. BASIC stands for Beginners All-purpose.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Python Programming, 1/e1 Programming Thinking and Method (5a) Zhao Hai 赵海 Department of Computer Science and Engineering Shanghai Jiao Tong University.
CSC 1010 Programming for All Lecture 7 Input, Output & Graphics.
Graphics Concepts CS 2302, Fall /17/20142 Drawing in Android.
Xiaojuan Cai Computational Thinking 1 Lecture 5 Objects and Graphics Xiaojuan Cai (蔡小娟) Fall, 2015.
Creating visual interfaces in python
Getting Started with Adobe Illustrator CS6. Objectives Define illustration software Start Adobe Illustrator CS6 and change preference settings View the.
CMPF114 Computer Literacy Chapter 3 The Visual Basic Environment 1.
Interaction with Graphics Also displaying GIFs. Text Output on the graphics window There is a class called Text which will put the message on the graphics.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 4 Objects and Graphics.
10. MORE OBJECTS Rocky K. C. Chang October 18, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and adapted from John.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 4 Objects and Graphics.
CS 115 Lecture 7 Graphics – coordinate systems, Text, Entry, aliases Taken from notes by Dr. Neil Moore.
Vahé Karamian Python Programming CS-110 CHAPTER 4 Objects and Graphics.
CS 115 Lecture 6 Graphics Taken from notes by Dr. Neil Moore.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 4 Objects and Graphics Killer cars.
Programming Logic and Design Seventh Edition Chapter 12 Event-Driven GUI Programming, Multithreading, and Animation.
ENGINEERING 1D04 Tutorial 4. What are we doing today? Focus Functions Scope of Variables Returning Values Objects Graphics library Aliasing Events Mouse.
Graphics Taken from notes by Dr. Neil Moore & Dr. Debby Keen
GUI in Python Ismail Abumuhfouz.
Python Programming: An Introduction to Computer Science
Adapted from slides by Marty Stepp and Stuart Reges
Python Programming: An Introduction to Computer Science
Python Programming: An Introduction to Computer Science
Example: Card Game Create a class called “Card”
Python: Simple Graphics and Event-driven Programming
Graphics Part I Taken from notes by Dr. Neil Moore
GUI Using Python.
Graphics Part I Taken from notes by Dr. Neil Moore
CS 115 Lecture Graphics II – coordinate systems, Text, Entry, aliases
CS 115 Lecture Graphics II – coordinate systems, Text, Entry, aliases
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
Graphics Part I Taken from notes by Dr. Neil Moore
Getting Started with Adobe Illustrator CS6
Topics Graphical User Interfaces Using the tkinter Module
Chapter 15: GUI Applications & Event-Driven Programming
Class 7 coordinates: pixel and with setCoords aspect ratio Rectangle
Chapter 4 Enhancing the Graphical User Interface
Presentation transcript:

5. COMPUTING WITH GRAPHICS OBJECTS Dennis Y. W. Liu and Rocky K. C. Chang October 8, 2015 (Adapted from John Zelle’s slides)

Objectives To construct a Graphical User Interface (GUI) window; To construct various shapes, including dots, straight lines, circles, rectangles, triangles and ovals; To combine various shapes to construct complicated graphics; To understand the process of developing a graphical interface; To understand the coordinate system in a GUI window; To construct a bar chart and a line graph; To create interactive graphics using event-driven programming.

The Object of Objects Basic idea – view a complex system as the interaction of simpler objects. An object is a sort of active data type that combines data and operations. Objects know stuff (contain data) and they can do stuff (have operations). Objects interact by sending each other messages.

Simple Graphics Programming The utilization of objects in Python can be better illustrated using Graphics programming.

EXERCISE 5.1 Download the graphics.py library from and save it to the following path: C:\ \Lib\site-packages E.g., C:\Python34\Lib\site-packages For MAC: Macintosh HD\Library\Frameworks\Python.framework\Versions\3.4\lib\python3.4\site- packages

EXERCISE 5.2 Type the following code in the Shell: >>> import graphics >>> win = graphics.GraphWin()

Window Creation and Termination This creates a window on the screen. Here the variable “win” represents the window and soon you will add other graphics on it. To close the window, type >>> win.close() We will be working with quite a few commands from the graphics library. Python has an alternative form of import that can help out: >>> from graphics import * >>> win = GraphWin() [Question] How to change the caption and the size of the window?

EXERCISE 5.3 Type the following code: >>> p = Point(50, 60) >>> win = GraphWin() >>> p.draw(win)

Drawing a Dot By default, the window size created by the program is 200 x 200 pixels (height x width). To locate a point (x, y) on the window, where x is the horizontal position and y is the vertical position, we use (0, 0) to represent the upper-left corner of the window. (199, 199) are the coordinates of lower-right corner of the window.

EXERCISE 5.4 Type the following code: >>> p1 = Point(20, 30) >>> p2 = Point(180, 165) >>> line = Line(p1, p2) >>> line.draw(win)

Drawing a Line To draw a line, you have specify the coordinates of two ends of the line. [Question] Can you draw a cross?

EXERCISE 5.5 Type the following code: >>> center = Point(100, 100) >>> circ = Circle(center, 30) >>> circ.setFill(‘red’) >>> circ.draw(win)

Drawing a Circle To draw a circle, we have to specify the center and the radius (in pixel) of the circle. [Question] How to draw a circle that is centered at (20, 20) with a radius of 30 pixels?

EXERCISE 5.6 Type the following code: >>> oval = Oval(Point(130, 240), Point(290, 330)) >>> oval.draw(win)

Drawing an Oval To draw an oval, we have to specify the two coordinates of opposite corners of bounding box. [Question] How to construct an oval with a size 50 x 80 pixels centered in the window?

EXERCISE 5.7 Type the following code: >>> win = GraphWin() >>> rect = Rectangle(Point(30, 50), Point(170, 160)) >>> rect.draw(win)

Drawing a Rectangle To construct a rectangle, we have to specify the two coordinates of opposite corners. [Question] How to construct a blue rectangle with a size 38 x 49 pixels with (10, 20) as its top-left corner?

EXERCISE 5.8 Type the following code: >>> win = GraphWin() >>> tri = Polygon(Point(30, 30), Point(70, 70), Point(90, 20)) >>> tri.draw(win)

Drawing a Triangle There is no triangle class. There is a general class Polygon that can be used for any multi-sided, close shape. It accepts any number of points and creates a polygon by using line segments to connect the points in the order given and to connect the last point back to the first. [Question] Create a trapezium of your desired size. Is there any relationship among the points?

EXERCISE 5.9 Create a graphic window which looks like below. The window should have a dimension of 420 x 360. [Hint: objectname.setOutline(color)]

More complex graphics for data representation Suppose we want to develop a program to determine the future value of an investment. The formula is given by: future value = principle * (1 + annual_interest_rate) year We invest $2000 at 15% interest for 5 years. The following table shows the growth of the investment: YearsValue 0$2, $2, $2, $3, $3, $4,022.71

More complex graphics for data representation We would like to display the information in a bar chart like below:

More complex graphics for data representation How can we achieve it? Programming with graphics requires careful planning. You will probably need a pencil and a piece of paper to draw some diagrams and scratch out calculations. Step 1: Designing using Pseudocode Step 2: Writing the Code

Step 1: Designing using Pseudocode The following pseudocode illustrates the flow of logic of the program: Create a GraphWin Draw scale labels on left side of window Draw bar at position 0 with height corresponding to principal For successive years 1 through 5 Calculate principal = principal * (1 + annual interest rate) Draw a bar for this year having a height corresponding to principal

The Coordinates and Lengths

Step 2: Writing the Code # futval_bar_chart.py from graphics import * def main(): # Let us assume the principal and annual interest rate is fixed principal = 2000 apr = 0.15 # Create a graphics window with labels on left edge win = GraphWin("Investment Growth Chart", 320, 240) win.setBackground("white") Text(Point(20, 230), ' 0.0K').draw(win) Text(Point(20, 180), ' 1.0K').draw(win) Text(Point(20, 130), ' 2.0K').draw(win) Text(Point(20, 80), ' 3.0K').draw(win) Text(Point(20, 30), ' 4.0K').draw(win) # Draw bar for initial principal barWidth = 45 height = principal * 0.05

Step 2: Writing the Code (con’t) bar = Rectangle(Point(50, 230), Point(50 + barWidth, height)) bar.setFill("green") bar.setWidth(2) bar.draw(win) # Draw bars for successive years for year in range(1, 6): # calculate value for the next year principal = principal * (1 + apr) # draw bar for this value xll = year * barWidth + 50 height = principal * 0.05 bar = Rectangle(Point(xll, 230), Point(xll + barWidth, height)) bar.setFill("green") bar.setWidth(2) bar.draw(win) input("Press to quit") win.close() main()

EXERCISE 5.10 Convert the bar chart above to a line chart as shown below:

Interactive Graphics Graphical interface can be used as input as well. In a GUI environment, users typically interact with their applications by clicking on buttons, choosing items from menus, and typing information into on-screen text boxes. A technique called event-driven programming is adopted. An interface element (also called control) is placed on the window and waits for the user act on it (e.g., “click” on a button) to trigger an event. An event will cause a series of code to be executed.

Drawing a Triangle Write a program that allows the user to draw a triangle by clicking on three points in a window.

Drawing a Triangle (con’t) # triangle.py from graphics import * def main(): win = GraphWin("Draw a Triangle") win.setCoords(0.0, 0.0, 10.0, 10.0) message = Text(Point(5, 0.5), "Click on three points") message.draw(win) # Get and draw three vertices of triangle p1 = win.getMouse() p1.draw(win) p2 = win.getMouse() p2.draw(win)

Drawing a Triangle (con’t) p3 = win.getMouse() p3.draw(win) # Use Polygon object to draw the triangle triangle = Polygon(p1, p2, p3) triangle.setFill("peachpuff") triangle.setOutline("cyan") triangle.draw(win) # Wait for another click to exit message.setText("Click anywhere to quit.") win.getMouse() main()

EXERCISE 5.11 Modify triangle.py. For each 3-click, the program should generate a triangle for the three points.

END