Graphics Part I Taken from notes by Dr. Neil Moore

Slides:



Advertisements
Similar presentations
Graphics Shapes. Setup for using graphics You have to import the graphics library You can use either “import graphics” or “from graphics import *” or.
Advertisements

Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Week 7: Input and Output 1.  Now we are going to talk a little bit about output  You have a lot of experience with System.out.println() and System.out.print()
Python Programming, 2/e1 CS177: Programming in Multimedia Objects Recitation Topic: Graphics Library.
Four simple expressions in meta. Data objects Pieces of data in a computer are called objects Today, we’ll talk about four kinds of objects Numbers Pictures.
Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.
Four simple expressions in meta. Data objects Pieces of data in a computer are called objects Today, we’ll talk about four kinds of objects Numbers Pictures.
1 Python Programming: An Introduction to Computer Science Chapter 3 Objects and Graphics.
InDesign CS3 Lesson 3 Working with Frames. Using Frames Frames are containers in which you place graphics or text. Frames can also be used as graphic.
Chapter 4 Objects and Graphics
Python: Graphics
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.
Introduction to Python
Art 321 Lecture 7 Dr. J. Parker. Programming In order to ‘make things happen’ on a computer, you really have to program it. Programming is not hard and.
PowerPoint Basics Tutorial 3: Graphics In this tutorial we’ll be looking at graphics, and the various types of illustrations that can be included in a.
Sketchup For 3D Design Part 2: 3D Text.
Graphic Basics in C ATS 315. The Graphics Window Will look something like this.
Tkinter Canvas.
Game Maker – Getting Started What is Game Maker?.
PowerPoint Basics Tutorial 3: Graphics In this tutorial we’ll be looking at graphics, and the various types of illustrations that can be included in a.
Some Graphics CS303E: Elements of Computers and Programming.
CSC 1010 Programming for All Lecture 7 Input, Output & Graphics.
Graphics Concepts CS 2302, Fall /17/20142 Drawing in Android.
Creating visual interfaces in python
CPSC 217 T03 Week II Part #1: SimpleGraphics.py Hubert (Sathaporn) Hu.
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.
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.
Graphics Lab: MyPaint Dan Maselko 1. MyPaint Description  For this assignment you will be implementing a very simple paint program.  You should be able.
CS 115 Lecture 6 Graphics Taken from notes by Dr. Neil Moore.
CompSci 4 Java 4 Apr 14, 2009 Prof. Susan Rodger.
ENGINEERING 1D04 Tutorial 4. What are we doing today? Focus Functions Scope of Variables Returning Values Objects Graphics library Aliasing Events Mouse.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
Graphics Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Drawing Geometric Objects
Sprites (Images) and Sounds
Development Environment
GUI in Python Ismail Abumuhfouz.
Graphical Output Basic Images.
Graphics CIS 40 – Introduction to Programming in Python
Pixels, Colors and Shapes
PYGAME.
Introduction to Python
Python Programming: An Introduction to Computer Science
Learn Animations in Fireworks
Inserting and Working with Images
Workshop 3.1 Sketching DesignModeler.
Week 8 - Monday CS 121.
Python: Simple Graphics and Event-driven Programming
Graphics Part I Taken from notes by Dr. Neil Moore
Exceptions and files Taken from notes by Dr. Neil Moore
Chapter 5 Working with Images
MS PowerPoint 2010 Week 2.
Learning to program with Logo
Graphics Part I Taken from notes by Dr. Neil Moore
CS 115 Lecture Graphics II – coordinate systems, Text, Entry, aliases
CS 106A, Lecture 12 More Graphics
CS 115 Lecture Graphics II – coordinate systems, Text, Entry, aliases
SSEA Computer Science: Track A
Exceptions and files Taken from notes by Dr. Neil Moore
CS 100: Roadmap to Computing
Chapter 2 Graphics Programming with C++ and the Dark GDK Library
Fusion 360 Pac Man Tutorial
slides courtesy of Eric Roberts
Simple Graphics Package
Topics Graphical User Interfaces Using the tkinter Module
Class 6 using the math library
Chapter 1 Introducing Small Basic
Functions Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Presentation transcript:

Graphics Part I Taken from notes by Dr. Neil Moore CS 115 Lecture Graphics Part I Taken from notes by Dr. Neil Moore

The graphics library So far all our programs have interacted with the user through standard input and output (keyboard and shell window) Plain-text input (input) and output (print) Can we do something that looks nicer? The graphics library by John Zelle is one way. Not part of Python; a third-party library Download it from the 115 web page or from Zelle’s site. Then what?

The graphics library Then what? Either put it in the same directory as your code … … or find your system Python directory and put it there. http://www.cs.uky.edu/~keen/115/graphics-fix.html import sys print(sys.path) and find the site-packages directory Other graphics packages take different approaches: turtle does turtle graphics, based on moving a cursor. Tkinter does graphical user interfaces based around widgets like checkboxes, labels, text fields, …

Classes, objects and constructors Object oriented programming: classes, objects and methods A class is a type (the kind of thing that can be stored in a variable) Especially a user- or library-defined type In Python, str, float, int, etc. are also classes. An object is a particular thing of that type. So int is a class, and 42 is an object of that class. str is a class and “abracadabra” is an object. Point is a class, Point(100, 100) is an object.

Classes, objects and constructors Why did we have to write it as Point(100, 100)? Unlike Python’s built-in types (3, 4.5, ‘abc’), most classes don’t have literals – symbols that stand for an object. Instead, you call a constructor to make a new object. A special function that returns a new object The name of the constructor is the same as the name of the class.

Classes in the graphics library The Zelle graphics library defines several classes. Among them: GraphWin – a window for drawing graphics Point – an (x, y) coordinate Line – a line segment with two endpoints Circle – a circle with a center point and radius Rectangle – a rectangle (given by two opposite corners) Oval – an oval that fits inside a “bounding box” Polygon – defined by connecting a sequence of Points. Text – text with a given string value, position, size, etc. Entry – box for user to enter input, has position, size, etc. Image – object that holds a GIF file, has position, size, etc. The complete reference page: http://mcsp.wartburg.edu/zelle/python/graphics/graphics/graphics.html

Getting started Begin by importing the library: import graphics Or: from graphics import * Now we need to create a window to draw in. The class for windows is called GraphWin Constructor: graphics.GraphWin (title, width, height) Or just graphics.GraphWin() (default values: “Graphics Window”, 200 x 200) Call the constructor, and save the new object in a variable (an assignment statement) We’ll need it later. window = graphics.GraphWin(“115 Program”, 600, 400)

Getting started The window usually closes when the program exits. Keep it open by waiting for a mouse click: win.getMouse() More about getMouse later Assuming the window will close when the program finishes doesn’t always work in the IDE Or when the program crashes Can eat up lots of system resources and eventually need a reboot! Be safe by always calling win.close()at the end!

Drawing graphics objects Let’s make a line going from the upper left to lower right of the graphics window. To do that, we use the Line class. Constructor: graphics.Line(point1, point2) What’s a “point”? Another class! Constructor: graphics.Point(x, y) (x and y are floats or ints) By default, (0, 0) is the upper left corner of graphics window Upside-down compared to Cartesian plane in math!

Drawing graphics objects You can use a constructor as an argument, so from graphics import Line, Point diagonal = Line(Point(0,0), Point(600, 400)) Making the line does not actually draw it! One more step: tell it to draw itself in the window diagonal.draw(window) Why? Programs can have multiple windows open! Or you might want to set the line’s color first.

Methods What’s going on with the diagonal.draw(window)? draw is a method of the Line class A method is like a function that works on an object “Something the object can do” In OOP, methods are how the program interacts with objects Syntax: obj.method(arguments) obj is an object (usually a variable) method is the name of the method

Methods Semantics: calls the function named “method” in obj’s class, sending it obj as the object to work on Methods can return values just like ordinary functions x = point.getX() The draw method does not return anything (like the function print) It’s a stand-alone statement. diagonal.draw(win)

More shapes: circles The Circle class represents a circle (surprise!) What information is needed to draw a circle? The Center: that’s a Point. The Radius: that’s a number (distance from center to edge). eye = Circle (Point(250, 250), 200) its center is at (250, 250) Its radius is 200, top is at (y = 50), bottom at (y = 450) As with Line, we have to draw the circle to display it: eye.draw(win)

Rectangles We could draw a rectangle already, using four Lines. But there is an easier way which we will see also has another benefit shortly What information do we need to draw a rectangle? Four corners? We really only need two opposite corners The graphics library can figure out the other two points.

Rectangles What is its width? 250 – 50 = 200 Height? 350 – 100 = 250 box = Rectangle (Point(50,100), Point(250, 350)) What is its width? 250 – 50 = 200 Height? 350 – 100 = 250 We gave the upper-left and lower-right corners but you don’t have to do those specifically. Just make sure you give two opposite corners. box = Rectangle (Point(250, 100), Point(50, 350))

Objects vs. Shapes If you use the Rectangle constructor, you generate an object with properties like corner points, colors, thickness of lines, a name, etc. If you want to “fill in” the object with a color, there is a method setFill that does it easily IF you had drawn the rectangle by using four separate Line objects, the shape on the screen will look just like the Rectangle object BUT you could not use setFill because that assumes the object has an “interior” – Lines don’t have that If you wanted to move the Rectangle object, there is a nice method called move – moves it as one thing. But if you had used 4 Lines, you would have to move each one separately.

Polygons We can also make a general polygon shape: Any number of sides, at any angle How could we specify all that? List the vertices (corners) tri = Polygon(Point(100, 100), Point (300, 100), Point (200, 250)) tri would be a triangle (has 3 corners) You can have any number of points, 1 or more. Draws a line from the first point to the second Then from the second to the third Finally, from the last point back to the first Order matters! If you have more than 3 points, anyway.

Ovals An oval is a stretched-out “squashed” circle. How would we specify an oval? Several possibilities: center and two radii, two foci, … The graphics library uses a bounding box. Class Oval The constructor takes two Point arguments The corners of a rectangle (the bounding box) The oval will fit in the box as tightly as possible. Does not actually draw the bounding box! ov = Oval (Point(100, 200), Point(400, 300)) ov.draw(win)

Images The graphics library can draw images It supports GIF format files, not JPEG! You give the position where you want the center of the image, and a filename pic = Image(Point(250, 250), “pic.gif”) The image will be centered at (250, 250) The GIF file should be in the same folder location as your py file. Whoever runs your program needs the image file too. As usual, use pic.draw(win) to display the image.

More methods for graphics objects You’ve seen all the constructors now And how to draw them Let’s look at some more things you can do with the objects once we have them (some more methods) obj.setWidth(pixels) Changes the width of the shape’s lines obj.move(dx, dy) Moves the shape by dx in the x direction, by dy in the y direction (can be positive or negative or zero) The numbers are added to the original coordinates Can do this even after the shape is drawn – so animation!

More methods for graphics objects obj.undraw() Erases the shape immediately Anything “behind” becomes visible again Be careful not to UNdraw something that has not been DRAWN yet! Gives a Run-time error!

Color methods Shapes have two different colors associated with them: the fill and the outline The fill color is used for inside the shape box.setFill(“blue”) You specify the color name as a string Points and Lines don’t have an “inside” This is why Rectangle and Polygon are better than just a bunch of Lines – they have “insides”! The outline color is used for the border line1.setOutline(“red”) For a Line or a Point, that’s the entire shape The window as a whole (GraphWin) has a background color. win.setBackground(“yellow”)

Color methods The color names are a bit obscure (“firebrick”? “purple4”?) http://www.tcl.tk/man/tcl8.5/TkCmd/colors.htm http://wiki.tcl.tk/37701 Or you can specify Red Green Blue values line1.setOutline (color_rgb(255, 128, 0)) # orange Or look at the Python References and Tutorials page on the CS 115 page – links to pages with all named colors drawn

Recap of OOP terminology Object: a thing that can be stored in a variable Class: a type that represents a particular kind of thing a template for making objects of that type GraphWin, Line, str, … are classes. The object “Hello” belongs to the class str. Constructor: a function that creates an object belonging to a class Has the same name as the class (is not the SAME thing AS the class! Is part of the class) Uses the class template to “stamp out” a new object. Point (100, 100) is a constructor call.

Recap of OOP terminology Method: a function that belongs to an object and does something to or with the object In myline.draw(win), draw is a method of the Line class Methods are defined by classes and work on any object of that class