Parameters, return, math, graphics nobody expects the spanish inquisition!

Slides:



Advertisements
Similar presentations
Copyright 2010 by Pearson Education Building Java Programs More Graphics reading: Supplement 3G.
Advertisements

Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Building Java Programs
Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
Graphics Chapter 16.  If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it?
PHY-102 SAPIntroductory GraphicsSlide 1 Introductory Graphics In this section we will learn how about how to draw graphics on the screen in Java:  Drawing.
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Topic 9 more graphics Based on slides bu Marty Stepp and Stuart Reges from
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
Copyright 2008 by Pearson Education Building Java Programs Graphics Reading: Supplement 3G.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
Strings, if/else, return, user input
Week 3 parameters, return, math, graphics Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
Week 3 parameters and graphics Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted,
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
Week 3 parameters, return, math, graphics Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
Week 6 review; file processing Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted,
Week 6 review; file processing Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted,
INTRODUCTION TO PYTHON PART 5 - GRAPHICS CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
1 Graphical objects We will draw graphics in Java using 3 kinds of objects: DrawingPanel : A window on the screen. Not part of Java; provided by the authors.
METHODS AND SCOPE CSCE More on Methods  This is chapter 6 in Small Java.
Copyright 2010 by Pearson Education Building Java Programs Graphics reading: Supplement 3G.
Unit 3 parameters and graphics Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise.
1 Building Java Programs Supplement 3G: Graphics These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold,
Week 3 parameters and graphics Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted,
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Exploration Seminar 4 Basics Special thanks to Scott Shawcroft, Ryan Tucker, Paul Beck and Roy McElmurry for their work on these slides. Except where otherwise.
Introductory Graphics Chapter Three. Computer Graphics §Full motion pictures (“Star Wars”) l animated films §Virtual reality §Games §Simulators (air craft.
Building Java Programs Graphics Reading: Supplement 3G.
Introduction to Graphics. Graphical objects To draw pictures, we will use three classes of objects: –DrawingPanel : A window on the screen. –Graphics.
If/else, return, user input, strings
CS305j Introduction to Computing Parameters and Methods 1 Topic 8 Parameters and Methods "We're flooding people with information. We need to feed it through.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
10/16/07.
Lecture 8: Return values and math
Adapted from slides by Marty Stepp and Stuart Reges
Lecture 10: return values and math
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
CSc 110, Spring 2017 Lecture 6: Parameters (cont.) and Graphics
parameters, return, math, graphics
review; file processing
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Lecture 7: Graphics, return values and math
Building Java Programs
CSc 110, Spring 2018 Lecture 9: Parameters, Graphics and Random
1/22/2008.
Building Java Programs
CSc 110, Autumn 2017 Lecture 9: Graphics and Nested Loops
Building Java Programs
CSc 110, Spring 2018 Lecture 10: More Graphics
Building Java Programs
Building Java Programs
CSc 110, Autumn 2017 Lecture 8: More Graphics
Building Java Programs
Topic 9 More Graphics Based on slides bu Marty Stepp and Stuart Reges from
Building Java Programs
Lecture 30: Lists of Lists
Building Java Programs
Building Java Programs
Making a Smile Face Pepper.
Graphics Reading: Supplement 3G
Building Java Programs
Lecture 11: return values and math
Presentation transcript:

parameters, return, math, graphics nobody expects the spanish inquisition!

parameters parameters are declared by writing their names no types needed! def name(parameter, parameter,..., parameter): statements

parameters >>> def print_many(word, n):... for i in range(n):... print word >>> print_many("spam", 4) spam

exercise rewrite Stars2.java in python ****************************************************************** ***************** ** ******

stars2.py 1 # Draws a box of stars with the given width and height. 2 def box(width, height): 3 print width * "*" 4 for i in range(height - 2): 5 print "*" + (width - 2) * " " + "*" 6 print width * "*" 7 8 # main 9 print 13 * "*" 10 print 7 * "*" 11 print 35 * "*" 12 box(10, 3) 13 box(5, 4)

default parameter values can make parameter(s) optional by specifying a default value >>> def print_many(word, n=1):... for i in range(n):... print word >>> print_many("shrubbery") shrubbery >>> print_many("shrubbery", 4) shrubbery

parameter keywords can pass parameters in any order by specifying their names when calling >>> def print_many(word, n):... for i in range(n):... print word >>> print_many(n=3, word="Ni!") Ni!

math from math import *

Function nameDescription ceil(value)rounds up cos(value)cosine, in radians degrees(value)convert radians to degrees floor(value)rounds down log(value, base)logarithm in any base log10(value)logarithm, base 10 max(value1, value2,...)largest of two (or more) values min(value1, value2,...)smallest of two (or more) values radians(value)convert degrees to radians round(value)nearest whole number sin(value)sine, in radians sqrt(value)square root tan(value)tangent ConstantDescription e pi more:

returning values just like in Java! def name(parameters): statements return value

returning values >>> def ftoc(temp):... tempc = 5.0 / 9.0 * (temp - 32)... return tempc >>> ftoc(98.6) 37.0

drawingpanel put drawingpanel.py in the same folder as your program from drawingpanel import * panel’s canvas field behaves like Graphics g need to put panel.mainloop() at the end of your program

draw.py 1 from drawingpanel import * 2 3 panel = DrawingPanel(400, 300) 4 panel.set_background("yellow") 5 panel.canvas.create_rectangle(100, 50, 200, 300) 6 panel.mainloop()

drawing methods notice methods take x2 / y2 parameters, instead of width/height JavaPython drawLinepanel.canvas.create_line(x1, y1, x2, y2) drawRect, fillRect panel.canvas.create_rectangle(x1, y1, x2, y2) drawOval, fillOval panel.canvas.create_oval(x1, y1, x2, y2) drawStringpanel.canvas.create_text(x, y, text="text") setColor(see next slide) setBackgroundpanel.set_background(color)

colors no fillRect, fillOval, or setColor instead, pass outline and fill parameters when drawing a shape list of all colors: s.png s.png

drawcolors.py 1 from drawingpanel import * 2 3 panel = DrawingPanel(400, 300) 4 panel.canvas.create_rectangle(100, 50, 200, 200, outline="red", fill="yellow") 5 panel.canvas.create_oval(20, 10, 180, 70, fill="blue") 6 panel.mainloop()

polygons draw polygons with create_polygon draw line groups by passing more parameters to create_line 1 from drawingpanel import * 2 3 panel = DrawingPanel(200, 200) 4 panel.canvas.create_polygon(100, 50, 150, 0, 150, 100, fill="green") 5 panel.canvas.create_line(10, 120, 20, 160, 30, 120, 40, 175) 6 panel.mainloop()

exercise draw a car!

car.py 1 from drawingpanel import * 2 3 panel = DrawingPanel(200, 100) 4 panel.set_background("gray") 5 6 panel.canvas.create_rectangle(x, y, x+100, y+50, fill="black") 7 panel.canvas.create_oval(x+10, y+40, x+30, y+60, fill="red", outline="red") 8 panel.canvas.create_oval(x+70, y+40, x+90, y+60, fill="red", outline="red") 9 panel.canvas.create_rectangle(x+70, y+10, x+100, y+30, fill="cyan", outline="cyan") panel.mainloop()

exercise parameterize it!

car2.py 1 from drawingpanel import * 2 3 def car(panel, x, y): 4 panel.canvas.create_rectangle(x, y, x+100, y+50, fill="black") 5 panel.canvas.create_oval(x+10, y+40, x+30, y+60, fill="red", outline="red") 6 panel.canvas.create_oval(x+70, y+40, x+90, y+60, fill="red", outline="red") 7 panel.canvas.create_rectangle(x+70, y+10, x+100, y+30, fill="cyan", outline="cyan") 8 9 panel = DrawingPanel(300, 200) 10 panel.set_background("gray") car(panel, 10, 30) 13 car(panel, 170, 80) 14 car(panel, 50, 120) panel.mainloop()

exercise animate it using panel.sleep() !

car3.py 1 from drawingpanel import * 2 3 def car(panel, x, y): 4 panel.canvas.create_rectangle(x, y, x+100, y+50, fill="black") 5 panel.canvas.create_oval(x+10, y+40, x+30, y+60, fill="red", outline="red") 6 panel.canvas.create_oval(x+70, y+40, x+90, y+60, fill="red", outline="red") 7 panel.canvas.create_rectangle(x+70, y+10, x+100, y+30, fill="cyan", outline="cyan") 8 9 panel = DrawingPanel(500, 100) for x in range(0, 400, 10): 12 panel.canvas.create_rectangle(0, 0, 500, 100, fill="gray", outline="gray") 13 car(panel, x, 10) 14 panel.sleep(10) panel.mainloop()