Xiaojuan Cai Computational Thinking 1 Lecture 5 Objects and Graphics Xiaojuan Cai (蔡小娟) Fall, 2015.

Slides:



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

Python Programming: An Introduction to Computer Science
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.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
1 Python Programming: An Introduction to Computer Science Chapter 3 Objects and Graphics.
Chapter 4 Objects and Graphics
Python: Graphics
Computer Science 111 Fundamentals of Programming I User Interfaces Introduction to GUI programming.
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.
Introduction to Visual Basic (VB)
Chapter 3 Working with Symbols and Interactivity.
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.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
An Introduction to Visual Basic
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
1 Input and Interaction. 2 Input Devices Physical input devices Keyboard devices and pointing devices Logical input devices.
Introduction to Windows Programming
Computing Science 1P Lecture 17: Friday 23 rd February Simon Gay Department of Computing Science University of Glasgow 2006/07.
Object-Oriented Design Simple Program Design Third Edition A Step-by-Step Approach 11.
Chapter Two Creating a First Project in Visual Basic.
COSC 235: Programming and Problem Solving Ch. 4 or… Everything is an Object Instructor: Dr. X.
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.
CITA 342 Section 2 Visual Programming. Allows the use of visual expressions (such as graphics, drawings, or animation) in the process of programming.
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.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 13 GUI Programming.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
Creating visual interfaces in python
Xiaojuan Cai Computational Thinking 1 Lecture 6 Defining Functions Xiaojuan Cai (蔡小娟) Fall, 2015.
LING 408/508: Programming for Linguists Lecture 23 November 25 th.
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.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 14 Event-Driven Programming with Graphical User Interfaces.
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)
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
Topics Graphical User Interfaces Using the tkinter Module
Python Programming: An Introduction to Computer Science
Python Programming: An Introduction to Computer Science
Python Programming: An Introduction to Computer Science
An Introduction to Computers and Visual Basic
Python: Simple Graphics and Event-driven Programming
An Introduction to Computers and Visual Basic
Graphics Part I Taken from notes by Dr. Neil Moore
Graphics Part I Taken from notes by Dr. Neil Moore
CS 115 Lecture Graphics II – coordinate systems, Text, Entry, aliases
Lesson 17: Building an App: Canvas Painter
CS 115 Lecture Graphics II – coordinate systems, Text, Entry, aliases
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
Graphics Part I Taken from notes by Dr. Neil Moore
Simple Graphics Package
An Introduction to Computers and Visual Basic
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
Class 6 using the math library
Class 8 setCoords Oval move Line cloning vs copying getMouse, getKey
Presentation transcript:

Xiaojuan Cai Computational Thinking 1 Lecture 5 Objects and Graphics Xiaojuan Cai (蔡小娟) Fall, 2015

Xiaojuan Cai Computational Thinking 2 Objective To understand the concept of objects. To be familiar with the various objects available in the graphics library. To understand the fundamental concepts of computer graphics. To be able to write simple interactive graphics programs using the graphics library.

Xiaojuan Cai Computational Thinking 3 Roadmap Object-oriented approach Graphics programming Graphics objects graphics.py Example: future value Interactive graphics

Xiaojuan Cai Computational Thinking 4 OO methodology data type = data + operations. The traditional programming view: data is passive – it ’ s manipulated and combined with active operations. The object-oriented approach: data is active – it carries out operations.

Xiaojuan Cai Computational Thinking 5 Basic idea of OO Object-oriented: software systems are interaction of objects. Objects know stuff (contain data) and they can do stuff (have operations). Computation (interaction): sending message to objects. Pure OO: everything is Object.

Xiaojuan Cai Computational Thinking 6 Example: student The student object would contain data like: Name, ID number, Courses taken, Home Address, GPA, … The student object would response to these messages: printHomeAddress Print GPA, ….

Xiaojuan Cai Computational Thinking 7 Example: student Print the home address of all the students. Traditional: for every student check the data, find out the home address and print it OO: for every student send printHomeAddress message ( student.printHomeAddress() )

Xiaojuan Cai Computational Thinking 8 Everything is object Each course might be an object containing data: Instructor Student roster … And operations: Add/delete a student …

Xiaojuan Cai Computational Thinking 9 Basic concepts of OO Class: the template of objects, defining the types of data an object can have and operations it can do. Object: the instance of some class Objects of the same class can have different data, E.g., Student A and B have different ID numbers. Message = operation = methods

Xiaojuan Cai Computational Thinking 10 Roadmap Object-oriented approach Graphics programming Graphics objects graphics.py Example: future value Interactive graphics

Xiaojuan Cai Computational Thinking 11 Graphics programming Graphical User Interfaces (GUI) provide windows, icons, buttons and menus. Graphics libraries: Python standard libray: Tkinter Zelle’s graphics.py (beginner-friendly) Put graphics.py in the lib folder, or in the same folder with your graphics programs. put it in your site-package

Xiaojuan Cai Computational Thinking 12 Simple test >>> import graphics >>> win = graphics.GraphWin() >>> win.close() >>> from graphics import * >>> win = GraphWin()

Xiaojuan Cai Computational Thinking 13 Graphics window A graphics window is a collection of pixels. Default size: 200 pixels * 200 pixels. Predefined geometric shapes: point, line, circle, oval, rectangle, polygon, text, button, …

Xiaojuan Cai Computational Thinking 14 Point Point = 1 pixel Create a new point: p = Point(x,y) Operations include: p.getX(), p.getY(), p.draw(win), p.undrawn p.move(dx,dy), p.setfill(c), p.setOutline(c) p.clone()

Xiaojuan Cai Computational Thinking 15 Line Create a new line: l = Line(p1,p2) Operations include: l.getP1(), l.getP2() l.setArrow(string) l.getCenter()

Xiaojuan Cai Computational Thinking 16 Circle Create a new circle: c = Circle(p1,r) Operations include: c.getCenter(), c.getRadius() c.getP1(), c.getP2()

Xiaojuan Cai Computational Thinking 17 Rectangle Create a new rectangle: rec = Rectangle(p1,p2) Operations include: rec.getCenter(),rec.getP1(), recc.getP2()

Xiaojuan Cai Computational Thinking 18 Oval Create a new oval: o = Oval(p1,p2) Operations include: o.getCenter(), c.getP1(), c.getP2()

Xiaojuan Cai Computational Thinking 19 Polygon Create a new polygon: p = Polygon(p1,p2,…), or p = Polygon(pointsList) Operations include: p.getPoints()

Xiaojuan Cai Computational Thinking 20 Text Create a new text: t = Text(center,text) Operations include: t.settext(newtext),t.gettext(), t.setcolor(c)

Xiaojuan Cai Computational Thinking 21 Sample codes >>> win = GraphWin('Shapes') >>> center = Point(100, 100) >>> circ = Circle(center, 30) >>> circ.setFill('red') >>> circ.draw(win) >>> label = Text(center, "Red Circle") >>> label.draw(win) >>> rect = Rectangle(Point(30, 30), Point(70, 70)) >>> rect.draw(win) >>> line = Line(Point(20, 30), Point(180, 165)) >>> line.draw(win) >>> oval = Oval(Point(20, 150), Point(180, 199)) >>> oval.draw(win)

Xiaojuan Cai Computational Thinking 22 Objects: creation Create an object from a class: = (, ) Send message to objects:. (,,…)

Xiaojuan Cai Computational Thinking 23 Draw a circle

Xiaojuan Cai Computational Thinking 24 Make a copy Wrong code leftEye = Circle(Point(80,50),5) rightEye = leftEye rightEye.move(20,0) Correct code leftEye = Circle(Point(80,50),5) rightEye = Circle(Point(100,50),5) Better code leftEye = Circle(Point(80,50),5) rightEye = leftEye.clone() rightEye.move(20,0) leftEye rightEye leftEye rightEye

Xiaojuan Cai Computational Thinking 25 Review: variables >>>x = 3 >>>type(x) >>>x = 'hi' >>>type(x) >>>x = Point(3,3) >>>type(x) x3 ih Point(3,3)

Xiaojuan Cai Computational Thinking 26 Review: alias 3y >>>x = 3 >>>y = x >>>x = 'hi' xih>>>x = 3 >>>y = x >>>x = x + 1 x 3y 4 >>>x = [1,2,3] >>>y = x >>>x[2] = 4 x 1y 4 23

Xiaojuan Cai Computational Thinking 27 Roadmap Object-oriented approach Graphics programming Graphics objects graphics.py Example: future value Interactive graphics

Xiaojuan Cai Computational Thinking 28 Future value Print an introduction Input principal and apr Repeat 10 times: principal = principal * (1 + apr) Output the principal Graphing future values

Xiaojuan Cai Computational Thinking 29 Design the graphic view

Xiaojuan Cai Computational Thinking 30 setCoords(xll,yll,xur,yur) The lower-left corner (xll,yll), the upper- right corner (xur,yur). All the subsequent drawing will be done w.r.t. the altered coords except for plotPixel Advantages: Easy to program Adjust automatically if you change the size of canvas.

Xiaojuan Cai Computational Thinking 31 Roadmap Object-oriented approach Graphics programming Graphics objects graphics.py Example: future value Interactive graphics

Xiaojuan Cai Computational Thinking 32 GUI Users interact with applications by Clicking a button, choosing items from menus, and typing information into text boxes. Event-driven programming draws interface elements and then waits for events.

Xiaojuan Cai Computational Thinking 33 Getting Mouse Clicks GraphWin class has getMouse method, which once invoked, will cause the program pauses and wait for the mouse clicks. And it will return the point where the user clicked. win = GraphWin("Click Me!") p = win.getMouse() print "You clicked (%d, %d)" % (p.getX(), p.getY())

Xiaojuan Cai Computational Thinking 34 Handling Textual Input There ’ s also an Entry object that can get keyboard input. It understands setText and getText, with one difference that the input can be edited. input = Entry(Point(2,3), 5) input.setText("0.0") celsius = eval(input.getText())

Xiaojuan Cai Computational Thinking 35 Conclusion An object is a computational entity that combines data and operations. Every object is an instance of some class. The graphics module provides a number of classes: GraphWin, Point, Line, Circle, Oval, Rectangle, Polygon, Text, Entry, … Alias can cause unexpected results. Use clone instead.