CSc 110, Autumn 2017 Lecture 9: Graphics and Nested Loops

Slides:



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

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.
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 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
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.
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,
Building Java Programs Graphics Reading: Supplement 3G.
CS305j Introduction to Computing Simple Graphics 1 Topic 11 Simple Graphics "What makes the situation worse is that the highest level CS course I've ever.
Introduction to Graphics. Graphical objects To draw pictures, we will use three classes of objects: –DrawingPanel : A window on the screen. –Graphics.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
Adapted from slides by Marty Stepp and Stuart Reges
10/16/07.
Adapted from slides by Marty Stepp and Stuart Reges
Lecture 8: Return values and math
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2017 Lecture 16: Fencepost Loops and Review
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Building Java Programs
Building Java Programs
CSc 110, Autumn 2017 Lecture 23: lists as Parameters
Building Java Programs
CSc 110, Spring 2017 Lecture 6: Parameters (cont.) and Graphics
CSc 110, Autumn 2017 Lecture 31: Dictionaries
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Lecture 7: Graphics, return values and math
Building Java Programs
CSc 110, Spring 2017 Lecture 5: Constants and Parameters
CSc 110, Spring 2018 Lecture 9: Parameters, Graphics and Random
Building Java Programs
CSc 110, Autumn 2016 Lecture 19: lists as Parameters
CSc 110, Autumn 2016 Lecture 5: Loop Figures and Constants
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2017 Lecture 4: Nested Loops and Loop Figures
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2018 Lecture 7: input and Constants
CSc 110, Spring 2018 Lecture 23: lists as Parameters
Adapted from slides by Marty Stepp and Stuart Reges
CSE 142 Lecture Notes Graphics with DrawingPanel Chapter 3
CSc 110, Spring 2018 Lecture 5: Loop Figures and Constants
Building Java Programs
CSc 110, Spring 2018 Lecture 8: input and Parameters
CSc 110, Spring 2018 Lecture 10: More Graphics
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
CSc 110, Autumn 2017 Lecture 8: More Graphics
Building Java Programs
Lecture 29: Lists of Lists
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
Graphics Reading: Supplement 3G
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Presentation transcript:

CSc 110, Autumn 2017 Lecture 9: Graphics and Nested Loops 12/2/2018 CSc 110, Autumn 2017 Lecture 9: Graphics and Nested Loops Adapted from slides by Marty Stepp and Stuart Reges Can you write this in Python? 1

Drawing parameter question Modify draw_car to allow the car to be drawn at any size. Existing car: size 100. Second car: (150, 10), size 50. Once you have this working, use a for loop with your function to draw a line of cars, like the picture at right. Start at (10, 130), each size 40, separated by 50px.

Animation with sleep DrawingPanel's sleep function pauses your program for a given number of milliseconds. You can use sleep to create simple animations. panel = DrawingPanel(250, 200) for i in range(1, NUM_CIRCLES + 1): panel.draw_oval(15 * i, 15 * i, 30, 30) panel.sleep(500) Try adding sleep commands to loops in past exercises in this chapter and watch the panel draw itself piece by piece.

Drawing parameter answer def main(): panel = DrawingPanel(260, 100, background="light gray") draw_car(panel, 10, 30, 100) draw_car(panel, 150, 10, 50) for i in range(0, 5): draw_car(panel, 10 + i * 50, 130, 40); def draw_car(p, x, y, size): p.fill_rect(x, y, size, size / 2, "black") p.fill_oval(x + size / 10, y + size / 5 * 2, size / 5, size / 5, "red") p.fill_oval(x + size / 10 * 7, y + size / 5 * 2, size / 5, size / 5, "red") p.fill_rect(x + size / 10 * 7, y + size / 10, size / 10 * 3, size / 5, "cyan")

How to add parameters The panel must always be a parameter to a function that draws Add in position (x, y) parameters These change x and y but not width and height of figures Add size parameter This changes width and height and sometimes x and y Think of all sizes and placements as percentages of the size size (width) was 100, wheel was 70 from left, that is 70% from the left so, size / 10 * 7

Nested Loops What does the following code output? def main(): for i in range(1, 10): for j in range(1, 10): print(j * i, end="\t") print() main()

Outputting a Grid controls the number of rows for i in range(n, m): for j in range(n, m): ... creates a single row

Output the following figures Left grid: x = 100 y = 100 circle size = 20 number of circles = 5 Right grid: x = 300 y = 300 circle size = 40 number of circles = 2