Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output,

Slides:



Advertisements
Similar presentations
Chapter 3 – Web Design Tables & Page Layout
Advertisements

Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Graphical Output,
MiniDraw Testing COMP 102 # T1
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.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects.
Chapter 2 First Java Programs
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Methods COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Design, Debugging & Introduction.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output.
Fall 2007ACS-1903 BlueJ Ron McFadyen Lab 1: Using BlueJ at UofW The purpose of this lab is to give you some experience with BlueJ. Over the course of the.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Review COMP 102 #
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Program Elements and Syntax.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
© Xiaoying Gao, Peter Andreae Class, method, statements COMP 102 #3 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington GUI and the UI API COMP.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington While loops and the UI API.
© Xiaoying Gao, Peter Andreae UI methods, Variables, Constants COMP 102 #4 2015T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Create Objects,
©Xiaoying Gao, Peter Andreae First Java Program COMP 102 #2 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
First Java Program COMP 102 #2 2015T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Call a Method,
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 2 Graphics Programming with C++ and the Dark GDK Library Starting.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Event-driven Input COMP 102.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Methods COMP.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Exercise, printf,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Event-driven Input TextFields,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Classes, Objects, Fields,
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Fields, Constructors.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods Calling Methods Return.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington UI methods, Graphical.
2011-T1 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Rashina Hoda and Peter Andreae COMP 102 Rashina Hoda.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
© Xiaoying Gao, Peter Andreae Variables, Constants, UI methods COMP 102 #4 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Event-driven Input COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Programs with Choice Booleans,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects.
© Peter Andreae Java Programs COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Event-driven Input COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Program Elements and Syntax.
Introduction to Algorithm. What is Algorithm? an algorithm is any well-defined computational procedure that takes some value, or set of values, as input.
Fields, Constructors COMP 102 # T2
Programs with graphics output
Building Java Programs
Chapter 2 First Java Programs
Computer Programming Methodology File Input
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.
CS Week 2 Jim Williams, PhD.
Presentation transcript:

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output, Choice COMP 102 #5 2011

© Peter Andreae COMP102 5:2 Menu A model for the computer: calling methods What methods can you call on the UI object? Using documentation to find out about classes Programs with Graphical output[L-D-C: Appendix F] Good design with variables Methods with parameters[L-D-C: 4.1 – 4.3 ] Administrivia: Class Rep Assig 2 is available 2 programs, based on TemperatureConverter and Drawer Optional Tutorial, Friday 2-3 AM 104

© Peter Andreae COMP102 5:3 Class Rep Stefan Koetsier I completed the Certificate of University Preparation course at Victoria over the summer, so I know what the role entails and I'm confident that having held similar class representative positions previously, I'd be a good liaison between the COMP 102 students and their lecturers. Joshua Scott I may not be as qualified to be a representative as some other candidates, but I'm willing to take the role head on. Currently started on my Bsc in computer science and am planning on a Master's in Network Engineering, so I would like this chance to improve both my leadership skills and knowledge of the subject as a whole. As for what I can bring to the role, I have an above par understanding of computers and learn things very quickly. I'm both helpful and polite as well as easy to approach if you have any questions or need any help.

© Peter Andreae COMP102 5:4 A model/metaphor for the computer If you are giving instructions to someone/something, it helps to understand how they think and what they can do! Your program is run by the "Java Virtual Machine" The JVM is like a clerk with alzheimers – only remembers what he writes down with a clipboard for the instructions he is currently working on looking out at the user through an opaque window (the UI window) which he can write on (but the writing only appears on the outside) can see a stream of characters that the user types on the keyboard

© Peter Andreae COMP102 5:5 Method Definition: Like a pad of worksheets public void fahrenToCelsius(){ double fahren = UI.askDouble("Fahrenheit:"); double celsius = (fahren – 32) * 5 / 9; UI.println(“ -> " + celsius + “ C"); } Calling a Method: tempCalc1.fahrenToCelsius(); ⇒ get a “copy” of the method worksheet ⇒ perform each action in the body. ⇒ throw the worksheet away (losing all the information on it) Method Calls: worksheet metaphor public void fahrenToCelsius(){ double fahren = UI.askDouble("Fahrenheit:"); double celsius = (fahren – 32) * 5 / 9; UI.println(“ -> " + celsius + “ C"); }

© Peter Andreae COMP102 5:6 What can the UI do? UI is a predefined object (strictly, it is a class with static methods, which acts like a predefined object ) Has methods for text input from the user eg UI.askString("What is your name?") text output eg UI.println(" * " + name + " * "; graphical output eg UI.drawRect(100, 100, 300, 150) making buttons, sliders, etc How do you find out about all the methods? How do your find out what arguments you need to provide?

© Peter Andreae COMP102 5:7 Read the Documentation! Full documentation for all the standard Java library code (the "API" : Application Programming Interface) Version of Java API documentation on course web site: "Java Documentation" in side bar Tailored for Comp 102 Includes documentation of the comp102 library: (UI, Trace, etc,) puts most useful classes at the top of the list. Use the documentation while you are programming!

© Peter Andreae COMP102 5:8 Some UI methods Text: UI.clearText()UI.print(anything ) UI.println(anything ) UI.println() UI.printf( format-string, values…) UI.askString(prompt-string ) UI.askDouble(prompt-string ) UI.askInt(prompt-string ) UI.askBoolean(prompt-string ) UI.next()UI.nextInt()UI.nextDouble UI.nextLine() UI.hasNext() UI.hasNextIntUI.hasNextDouble() Graphics: UI.clearGraphics() UI.setColor(color ) UI.drawRect(left, top, wd, ht )UI.fillRect(left, top, wd, ht ) … UI.drawOval(left, top, wd, ht )UI.fillOval(left, top, wd, ht ) … UI.drawLine(x1, y1, x2, y2 )UI.drawImage(file, left, top )

© Peter Andreae COMP102 5:9 Programs with graphics output Write a program to draw a face with a hat: Design What shapes can we draw? UI has methods to draw rectangles, ovals, lines, arcs,… ⇒ Draw one green rectangle, one green line, one orange oval, How do we draw them? Need to set the color first then call the draw/fill methods: must specify the positions and size rectangles/ovals: left, top, width, height lines: x and y of each end. one orange oval, one green rectangle, one green line x y (0,0) Shapes drawn on top of previous shapes Coordinates measured from left and top

© Peter Andreae COMP102 5:10 FaceWithHat program Design: Method faceWithHat(): set color to orange draw oval set color to green draw rectangle draw line Must work out the coordinates: oval: rectangle: line: x y ≈ 600 ≈500

© Peter Andreae COMP102 5:11 Writing the program Need import statements Need a class (with a descriptive comment) what name? Need a method (with a descriptive comment) what name? what actions? import comp102.*; import awt.Color*; /** Draws little pictures on the graphics pane */ public class Drawer { /** Draw an orange face with a brimmed hat */ public void faceWithHat() { // actions } } Write the method body as comments first

© Peter Andreae COMP102 5:12 Writing the program: using comments import comp102.*; import awt.Color*; /** Draws little pictures on the graphics pane */ public class Drawer { /** Draw an orange face with a brimmed hat*/ public void faceWithHat() { // set color to orange // draw 100x150 // set color to green // draw 100x50 // draw line (180,140) to (320, 140) } Now express each comment in Java (look up the documentation if necessary) Do it in BlueJ!

© Peter Andreae COMP102 5:13 Writing the program import comp102.*; import awt.Color*; /** Draws little pictures on the graphics pane */ public class Drawer { /** Draw an orange face with a brimmed hat*/ public void faceWithHat() { UI.setColor(Color.orange);// set color to orange UI.fillOval(200, 100, 100, 150);// draw oval UI.setColor(Color.green);// set color to green UI.fillRect(200, 90, 100, 50);// draw rectangle UI.drawLine(180, 140, 320, 140);// draw line } } Express each comment in Java (look up the documentation if necessary)

© Peter Andreae COMP102 5:14 Compile, Run, and Test the program. Fix the errors!

© Peter Andreae COMP102 5:15 Improving the design This program is very inflexible: What if We want the face & hat to be in a different position? We want the face & hat to be bigger or smaller? We want the hat to be taller? …. We want to draw two of them? Current design is filled with literal values ⇒ difficult to change Better design: Use variables ⇒ easier to write and easier to change

© Peter Andreae COMP102 5:16 Variables to specify face & hat center top faceWd faceHt hatWd brimYhatHt

© Peter Andreae COMP102 5:17 Improving the program /** Draw an orange face with a brimmed hat*/ public void faceWithHat() { double center = 250;// horizontal center of face double top = 100;// top of face double faceWd = 100; double faceHt = 1.5 * faceWd; double hatHt = 0.3 * faceHt; double hatWd = 0.96 * faceWd; double brimWd = 1.6 * hatWd; double brimY = top * faceHt; UI.setColor(Color.orange); UI.fillOval(center-faceWd/2, top, faceWd, faceHt); // face UI.setColor(Color.green); UI.fillRect(center-hatWd/2, brimY-hatHt, hatWd, hatHt); // hat crown UI.drawLine(center-brimWd/2, brimY, center+brimWd/2, brimY)//brim }

© Peter Andreae COMP102 5:18 Principle of good design Use well named variables where-ever possible, rather than literal values ⇒ easier to understand ⇒ easier to get right ⇒ much easier to modify Choosing the right variables is an art!! why did I choose "center" instead of "faceLeft" or "brimLeft" ? why did I choose "top" to be the top of the face, not the hat? We have effectively parameterised the drawing three values (center, top, faceWd) control the whole thing.