Python Programming, 2/e1 CS177: Programming in Multimedia Objects Recitation Topic: Graphics Library.

Slides:



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

Python Programming: An Introduction to Computer Science
Graphics Shapes. Setup for using graphics You have to import the graphics library You can use either “import graphics” or “from graphics import *” or.
Introduction to shapes
Graphics Programming. In this class we will cover: Drawing lines, rectangles, and ellipses Copying an area Drawing an image.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
Graphics Output Primitives Pixel Addressing and Fill Area Dr. M. Al-Mulhem Feb. 1, 2008.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 16 : Swing VI Graphics King Fahd University of Petroleum & Minerals College of Computer.
1 Python Programming: An Introduction to Computer Science Chapter 3 Objects and Graphics.
Chapter 4 Objects and Graphics
Python: Graphics
Animation CSC 161: The Art of Programming Prof. Henry Kautz 10/14/2009.
CSC 110 Objects and Graphics [Reading: chapter 4] CSC 110 E 1.
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.
CS 450: Computer Graphics PIXEL AdDRESSING AND OBJECT GEOMETRY
Java Software Solutions Lewis and Loftus Chapter 7 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphics -- Introduction The.
Lesson 1.8 – Space Geometry Homework: Lesson 1.8/1-27 Chapter 1 Test Friday 10/18.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
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.
Introduction to Java Simple Graphics. Objects and Methods Recall that a method is an action which can be performed by an object. –The action takes place.
Loops & Graphics IP 10 Mr. Mellesmoen Recall Earlier we wrote a program listing numbers from 1 – 24 i=1 start: TextWindow.WriteLine(i) i=i+1 If.
Tkinter Canvas.
Some Graphics CS303E: Elements of Computers and Programming.
GRAPHICS MODULE 14 STUDY BOOK. Graphic commands SCREEN - puts the screen into graphics mode WINDOW - allows scaling of the screen LINE - 3 formats –LINE.
Computer Graphics Rendering 2D Geometry CO2409 Computer Graphics Week 2.
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.
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
Documenting a function. Documenting a function definition We have a template for information that we need you to put at the top of each function - if.
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.
5. COMPUTING WITH GRAPHICS OBJECTS Dennis Y. W. Liu and Rocky K. C. Chang October 8, 2015 (Adapted from John Zelle’s slides)
PyGame - Unit 1 PyGame Unit – – Introduction to PyGame.
Vahé Karamian Python Programming CS-110 CHAPTER 4 Objects and Graphics.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
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.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
3. Drawing Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Graphics Taken from notes by Dr. Neil Moore & Dr. Debby Keen
GUI in Python Ismail Abumuhfouz.
Catapult Python Programming Session 4
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
Building Java Programs
Building Java Programs
Python: Simple Graphics and Event-driven Programming
Lesson One: The Beginning Chapter 1: Pixels Learning Processing Daniel Shiffman Presentation by Donald W. Smith Graphics from
CSc 110, Spring 2017 Lecture 6: Parameters (cont.) and Graphics
Graphics Part I Taken from notes by Dr. Neil Moore
Graphics Part I Taken from notes by Dr. Neil Moore
Basic Graphics Drawing Shapes 1.
CS 106A, Lecture 12 More Graphics
Building Java Programs
Digital Media Dr. Jim Rowan ITEC 2110.
CSc 110, Spring 2018 Lecture 9: Parameters, Graphics and Random
SSEA Computer Science: Track A
Chapter 2 Graphics Programming with C++ and the Dark GDK Library
Graphics Part I Taken from notes by Dr. Neil Moore
slides courtesy of Eric Roberts
Simple Graphics Package
Topics Graphical User Interfaces Using the tkinter Module
Class 7 coordinates: pixel and with setCoords aspect ratio Rectangle
Presentation transcript:

Python Programming, 2/e1 CS177: Programming in Multimedia Objects Recitation Topic: Graphics Library

Python Programming, 2/e2 Objectives To be familiar with the various graphic objects available in the graphics library. To understand the fundamental concepts of computer graphics, especially the role of coordinate systems To be able to write simple graphics programs using the graphics library.

Python Programming, 2/e3 Simple Graphics Programming This chapter uses the graphics.py library supplied with the supplemental materials. Two location choices for graphics.py In Python ’ s Lib directory with other libraries In the same folder as your graphics program

Python Programming, 2/e4 Simple Graphics Programming Since this is a library, we need to import the graphics commands >>> import graphics A graphics window is a place on the screen where the graphics will appear. >>> win = graphics.GraphWin() This command creates a new window titled “Graphics Window.”

Python Programming, 2/e5 Simple Graphics Programming Windows can be closed/destroyed by issuing the command >>> win.close()

Python Programming, 2/e6 Simple Graphics Programming It ’ s tedious to use the graphics. notation to access the graphics library routines. from graphics import * The “from” statement allows you to load specific functions from a library module. “*” will load all the functions, or you can list specific ones.

Python Programming, 2/e7 Simple Graphics Programming Doing the import this way eliminates the need to preface graphics commands with graphics. >>> from graphics import * >>> win = GraphWin()

Python Programming, 2/e8 Simple Graphics Programming A graphics window is a collection of points called pixels (picture elements). The default GraphWin is 200 pixels tall by 200 pixels wide (40,000 pixels total). One way to get pictures into the window is one pixel at a time, which would be tedious. The graphics routine has a number of predefined routines to draw geometric shapes.

Python Programming, 2/e9 Point The simplest object is the Point. Like points in geometry, point locations are represented with a coordinate system (x, y), where x is the horizontal location of the point and y is the vertical location. The origin (0,0) in a graphics window is the upper left corner. X values increase from right to left, y values from top to bottom. Lower right corner is (199, 199)

Python Programming, 2/e10 Point Example >>> p = Point(50, 60) >>> p.getX() 50 >>> p.getY() 60 >>> win = GraphWin() >>> p.draw(win) >>> p2 = Point(140, 100) >>> p2.draw(win)

Circles Given a 2-Dimensional plane, what two things do you need to draw a circle? Python Programming, 2/e11

Python Programming, 2/e12 Circles Like circles in geometry, circles in graphics library are represented by the center of the circle which is a Point and the radius of the circle

Python Programming, 2/e13 Circle Example >>> center = Point(100, 100) >>> win = GraphWin() >>> cir = Circle(center, 40) >>> cir.draw(win)

Python Programming, 2/e14 Rectangle Rectangles are the graphics object in graphics module which take the two points as parameters These two points are the two ends of the diagonals of the rectangle

Python Programming, 2/e15 Rectangle Example >>> p1 = Point(100, 100) >>> p2 = Point(150,150) >>> win = GraphWin() >>> rect = Rectangle(p1,p2) >>> rect.draw(win)

How can you draw a square? We have a problem here. There is no method in graphics library to draw a square. Can you suggest a way? Python Programming, 2/e16

Python Programming, 2/e17 Lines We can interpret lines as a series of points connecting the origin point to the destination point in one direction in a single dimension. Lines in graphics library are represented in the same way and Line() takes two points as parameters and draws a line between those two points.

Python Programming, 2/e18 Line Example >>> p1 = Point(100, 100) >>> p2 = Point(150,150) >>> win = GraphWin() >>> line = Line(p1,p2) >>> line.draw(win)

Python Programming, 2/e19 Ovals Ovals in graphics library are drawn inside a rectangle specified by two points in the window. Oval takes two points as its parameter and constructs an oval in the bounding box determined by those two points.

Python Programming, 2/e20 Oval Example >>> p1 = Point(50, 100) >>> p2 = Point(150,150) >>> win = GraphWin() >>> oval = Oval(p1,p2) >>> oval.draw(win)

Python Programming, 2/e21 Polygons Polygons are the figures that has a closed path consisting of lines joining the given set of points Polygon() takes variable number of points as arguments and creates a polygon joining those set of points Eg. Polygon(point1, point2, point3, …)

Python Programming, 2/e22 Polygon Example >>> p1 = Point(50, 100) >>> p2 = Point(100,150) >>> p3 = Point(75, 50) >>> win = GraphWin() >>> polygon = Polygon(p1,p2) >>> polygon.draw(win)

Generic methods supported by every Graphics object setFill(color) : Fills the interior of object with specified color setOutline(color): Sets the outline of object to the given colot setWidth(pixels): Sets the width of the outline to specified pixels. draw(graphWin): Draws the object to the given window undraw(): Undraws the graphics object from a graphics window move(dx,dy): Moves the graphics object dx units in x direction and dy units in y direction clone(): returns the duplicate of that graphics object. The clones are always in undrawn state. You need to call draw() on those objects to draw them on window. Python Programming, 2/e23

Getting it all together from graphics import * def main(): win = GraphWin() drawFace(win) drawEyes(win) drawNose(win) drawLips(win) drawTeeth(win) def drawFace(win): centerOfFace = Point(100, 100) face = Circle(centerOfFace, 50) centerOfLeftEye = Point(80,75) face.draw(win) def drawEyes(win): centerOfLeftEye = Point(80,75) leftEye = Circle(centerOfLeftEye, 10) centerOfRightEye = Point(120,75) rightEye = Circle(centerOfRightEye, 10) leftEye.draw(win) rightEye.draw(win) Python Programming, 2/e24

Getting it all together (Cont.) def drawNose(win): noseEdge1 = Point(100, 90) noseEdge2 = Point(90, 110) noseEdge3 = Point(110,110) nose = Polygon(noseEdge1, noseEdge2, noseEdge3) nose.draw(win) def drawLips(win): lipsEdge1 = Point(80,125) lipsEdge2 = Point(120, 125) lips = Line(lipsEdge1, lipsEdge2) lips.draw(win) def drawTeeth(win): toothPoint1 = Point(90,125) toothPoint2 = Point(95, 130) toothPoint3 = Point(110, 125) toothPoint4 = Point(115, 130) tooth1 = Rectangle(toothPoint1, toothPoint2) tooth2 = Rectangle (toothPoint3, toothPoint4) tooth1.draw(win) tooth2.draw(win) main() Python Programming, 2/e25

Python Programming, 2/e26