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.

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.
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 You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
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.
Return values.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
Week 5 while loops; logic; random numbers; tuples Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except.
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 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return; double ; System.out.printf reading: 3.2, 3.5, 4.4 videos: Ch.
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, Paul Beck, Hélène Martin, Kim Todd, John Kurkowski, and Marty.
Week 7 Lists Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work is licensed.
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,
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return; double ; System.out.printf reading: 3.2, 3.5, 4.4 videos: Ch.
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.
Microsoft® Small Basic The Math Object Estimated time to complete this lesson: 1 hour.
Unit 5 while loops; logic; random numbers; tuples Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work.
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.
Unit 2 Expressions and variables; for loops Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except.
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,
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, Paul Beck, Hélène Martin, Kim Todd, John Kurkowski, and Marty.
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.
Parameters, return, math, graphics nobody expects the spanish inquisition!
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.
Let’s Learn 3. Modules Saenthong School, January – February 2016
If/else, return, user input, strings
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
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
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
Building Java Programs
Building Java Programs
Building Java Programs
Graphics Reading: Supplement 3G
Building Java Programs
Presentation transcript:

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 noted, this work is licensed under:

2 Parameters def name ( parameter, parameter,..., parameter ): statements –Parameters are declared by writing their names (no types) >>> def print_many(word, n):... for i in range(n):... print word >>> print_many("hello", 4) hello

3 Exercise Recreate the lines/boxes of stars example from lecture: ************* ******* *********************************** ********** * ********** ***** * *****

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

5 Default Parameter Values def name ( parameter = value,..., parameter = value ): statements –Can make parameter(s) optional by specifying a default value –Exercise: Modify stars.py to add an optional parameter for the character to use for the outline of the box (default "*" ). >>> def print_many(word, n=1):... for i in range(n):... print word >>> print_many("shrubbery") shrubbery >>> print_many("shrubbery", 4) shrubbery

6 Parameter Keywords name ( parameter = value,..., parameter = value ) –Can specify the names of parameters as you call a function –This allows you to pass the parameters in any order >>> def print_many(word, n):... for i in range(n):... print word >>> print_many(str="shrubbery", n=4) shrubbery >>> print_many(n=3, str="Ni!") Ni!

7 Math commands 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

8 Returning Values def name ( parameters ): statements... return value >>> def ftoc(temp):... tempc = 5.0 / 9.0 * (temp - 32)... return tempc >>> ftoc(98.6) 37.0

9 DrawingPanel Instructor-provided drawingpanel.py file must be in the same folder as your Python program At the top of your program, write: –from drawingpanel import * Panel's canvas field behaves like Graphics g in Java need to say panel.mainloop() at bottom of program!

10 DrawingPanel Example draw1.py from drawingpanel import * panel = DrawingPanel(400, 300) panel.set_background("yellow") panel.canvas.create_rectangle(100, 50, 200, 300) panel.mainloop()

11 Drawing Methods –Notice, methods take x2/y2 parameters, not width/height JavaPython drawLine panel.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 ) drawString panel.canvas.create_text( x, y, text=" text ") setColor (see next slide) setBackground panel.set_background( color )

12 Colors and Fill Python doesn't have fillRect, fillOval, or setColor. –Instead, pass outline and fill colors when drawing a shape. –List of all color names: –Visual display of all colorsVisual display of all colors drawcolors.py from drawingpanel import * panel = DrawingPanel(400, 300) panel.canvas.create_rectangle(100, 50, 200, 200, outline="red", fill="yellow") panel.canvas.create_oval(20, 10, 180, 70, fill="blue") panel.mainloop()

13 Polygons Draw arbitrary polygons with create_polygon Draw line groups by passing more params to create_line drawpoly.py from drawingpanel import * panel = DrawingPanel(200, 200) panel.canvas.create_polygon(100, 50, 150, 0, 150, 100, fill="green") panel.canvas.create_line(10, 120, 20, 160, 30, 120, 40, 175) panel.mainloop()

14 Exercise Write a modified Projectile program:Projectile –Draw projectiles traveling at: 85 degrees at 30 m/s 85 degrees at 60 m/s 85 degrees at 120 m/s –First write a projectile function to draw a single projectile. v x = v  cos(Θ) v y = v  sin(Θ) totalTime = -2 * v y / GRAVITY dt = totalTime / steps Create a colored circle, with default color black, at each step in time where the projectile should be.

15 Animation Pause the panel with sleep animation.py from drawingpanel import * panel = DrawingPanel(350, 300) for i in range(20): # clear any previous image panel.canvas.create_rectangle(0, 0, 400, 400, outline="white", fill="white") panel.canvas.create_polygon(20 * i, 50, 20 * i, 100, 20 * i + 50, 75) # sleep for 100ms panel.sleep(100)