1/22/2008.

Slides:



Advertisements
Similar presentations
2/7/2008. >>> Overview * boolean * while * random * tuples.
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.
Applets An applet is similar to a Java program, but it runs within a Web-browser on a client machine. For security, you cannot change anything on a client.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs Lecture 3-1: Parameters (reading: 3.1)
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, 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.
Week 2 ______ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
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.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs.
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.
11/27/07. >>> Overview * objects * class * self * in-object methods * nice printing * privacy * property * static vs. dynamic * inheritance.
Week 4. >>> Overview * if else * returns * input.
Copyright 2010 by Pearson Education Building Java Programs Graphics reading: Supplement 3G.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
Week 7 Review and file processing. >>> Methods Hello.java public class Hello { public static void main(String[] args){ hello(); } public static void hello(){
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 Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
Parameters, return, math, graphics nobody expects the spanish inquisition!
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.
1/10/2008. >>> About Us Paul Beck * Third quarter TA * Computer Engineering * Ryan Tucker * Second quarter TA * Computer.
10/9/07 ______ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
8/2/07. >>> About Me Scott Shawcroft * Junior * Computer Engineering * Third Quarter TA * Creative Commons Intern * Small-time Open Source Developer
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
Recitations Sep 8-9 u conditionals: if-else statement u boolean expressions u classes and instances.
10/30/07. >>> Overview * boolean * randomness * while * tuples.
11/13/07. >>> Overview * lists/arrays * reading files * writing files.
10/30/07.
10/16/07.
10/9/07 ______ < Moo? > \ ^__^ \ (oo)\_______ (__)\ )\/\
Adapted from slides by Marty Stepp and Stuart Reges
Parameters Readings: 3.1.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
A note on Programming Assignment
Building Java Programs
CSc 110, Spring 2017 Lecture 6: Parameters (cont.) and Graphics
parameters, return, math, graphics
Topic 8 graphics "What makes the situation worse is that the highest level CS course I've ever taken is cs4, and quotes from the graphics group startup.
Building Java Programs
Building Java Programs
Parameters Readings: 3.1.
CSE 142 Lecture Notes Graphics with DrawingPanel Chapter 3
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Topic 9 More Graphics Based on slides bu Marty Stepp and Stuart Reges from
Building Java Programs
Parameters Readings: 3.1.
Building Java Programs
Building Java Programs
Making a Smile Face Pepper.
Graphics Reading: Supplement 3G
Building Java Programs
1.
Presentation transcript:

1/22/2008

>>> Overview * parameters * graphics

>>> Parameters Parameters are easy in Python once you know Java's. Simply remove all types from the method header and do the normal conversion. public static void printSum(int x, int y) { System.out.println(x + y); } PrintSum.java 1 2 3 4 def print_sum(x, y): print str(x + y)‏ sum(2, 3)‏ print_sum.py 1 2 3 4

>>> Parameters Example ************* ******* *********************************** ********** * * ***** * *

>>> Example Solution def draw_line(num): print "*" * num def draw_box(width, height): draw_line(width) for i in range(height-2): print "*" + " " * (width-2) + "*" #main draw_line(13) draw_line(7) draw_line(35) draw_box(10,3) draw_box(5,4)

>>> Defaults Unlike Java, Python's parameters can have default values to use when one is not given. def print_range(start=1, end=1, interval=1, sep=" "): for i in range(start, end, interval): print str(i) + sep, print end print range(0,7)‏ print_range(1,7,1,", ")‏ print_range.py 1 2 3 4 5 6 7

>>> Keywords When calling a function with a number of parameters with defaults you can modify particular parameters with a keyword so that you do not need to specify all preceding parameters. def print_range(start=1,end=1,interval=1,sep=" "): for i in range(start,end,interval): print str(i) + sep, print end print range(0,7)‏ print_range(1,7,1,", ")‏ print_range(end=7,sep=", ")‏ print_range.py 1 2 3 4 5 6 7 8 9

>>> Graphics Graphics in Python are similar to graphics in Java drawingpanel.py needs to be in the same directory as your program that uses it The Graphics (g) in Python behaves like the Graphics (g) in Java and is passed as a parameter in the same fashion. To let the Python interpreter know that you want to use the drawingpanel.py file you must add “from drawingpanel import *” at the top of your file panel.mainloop() must be put at the end of the program

>>> Graphics Methods Java Python g.drawLine(x1, y1, x2, y2); g.create_line(x1, y1, x2, y2) g.create_line(x1, y1, x2, y2, x3, y3,…, xN, yN) g.drawOval(x1, y1, width, height); g.create_oval(x1, y1, x2, y2) g.drawRect(x1, y1, width, height); g.create_rectangle(x1, y1, x2, y2) panel.setBackground(Color); g[“bg”] = “ <color> “

>>> Graphics Methods DrawingPanel panel = new DrawingPanel(300, 200); Graphics g = panel.getGraphics(); panel.setBackground(Color.YELLOW); Java 1 2 3 4 panel = DrawingPanel(300, 200) g = panel.get_graphics() g["bg"] = "yellow" Python 1 2 3 4

>>> Graphics What about...? g.setColor() g.fillRect(), g.fillOval() Fill colors and borders in Python are set as parameters in the methods. g.setColor(Color.RED); g.fillRect(x, y, w, h,); g.setColor(Color.BLACK); g.drawRect(x, y, w, h); Java 1 2 3 4 g.create_rectangle(x, y, x+w, y+h, fill=“red”, outline=“black”)‏ Python 1 2 3 4

>>> Graphics Example 1 from drawingpanel import * panel = DrawingPanel(400,380) g = panel.get_graphics() g["bg"]="black“ g.create_rectangle(100, 100, 200, 200, fill="red", outline="yellow") panel.mainloop() Python 1 2 3 4 5 6

>>> Graphics What about...? Triangles Hexagons Etc. g.create_polygon(x1, y1, x2, y2,..., xN, yN) from drawingpanel import * panel = DrawingPanel(100,100) g = panel.get_graphics() g.create_polygon(50, 50, 100, 0, 100, 100, fill="green") panel.mainloop() Python 1 2 3 4 5

>>> Graphics Example 2 Let’s recreate the Java car example in Python: import java.awt.*; public class DrawCar { public static void main(String[] args) { DrawingPanel panel = new DrawingPanel(200, 100); panel.setBackground(Color.LIGHT_GRAY); Graphics g = panel.getGraphics(); g.setColor(Color.BLACK); g.fillRect(10, 30, 100, 50); g.setColor(Color.RED); g.fillOval(20, 70, 20, 20); g.fillOval(80, 70, 20, 20); g.setColor(Color.CYAN); g.fillRect(80, 40, 30, 20); }

>>> Example 2 (auf Python) Let’s recreate the Java car example in Python: from drawingpanel import * panel = DrawingPanel(200,100) g = panel.get_graphics() g["bg"] = "gray" g.create_rectangle(10, 30, 10+100, 30+50, fill="black") g.create_oval(20, 70, 20+20, 70+20,fill="red“, outline=“red”) g.create_oval(80, 70, 80+20, 70+20, fill="red“, outline=“red”) g.create_rectangle(80, 40, 80+30, 40+20, fill="cyan”, outline=“cyan”)

>>> Example 2 - Parameterized Now, let’s use parameters so that we can place the cars all over the DrawingPanel.

>>> Example 2 - Parameterized from drawingpanel import * def draw_car(g, x, y): g.create_rectangle(x, y, x+100, y+50, fill="black") g.create_oval(x+10, y+40, x+10+20, y+40+20, fill="red", outline="red") g.create_oval(x+70, y+40, x+70+20, y+40+20, fill="red", outline="red") g.create_rectangle(x+70, y+10, x+70+30, y+10+20, fill="cyan“, outline=“cyan”) # main panel = DrawingPanel(260,100) g = panel.get_graphics() g["bg"] = "gray" draw_car(g, 10, 30) draw_car(g, 150, 10)

© 2007 Scott Shawcroft, Some Rights Reserved Except where otherwise noted, this work is licensed under http://creativecommons.org/licenses/by-nc-sa/3.0 Python® and the Python logo are either a registered trademark or trademark of the Python Software Foundation. Java™ is a trademark or registered trademark of Sun Microsystems, Inc. in the United States and other countries.