Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Graphical Output,

Slides:



Advertisements
Similar presentations
3 hr 5 hr 8 hr Hours worked Charge
Advertisements

Chapter 13 Graphics.
COMP 110: Introduction to Programming Tyler Johnson Feb 25, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 20, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Applets and Graphics.
$100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300.
Iterations for loop. Outcome Introduction to for loop The use of for loop instead of while loop Nested for loops.
$100 $200 $300 $400 $100 $200 $300 $400 $100 $200 $300 $400 $100 $200 $300 $400 $100 $200 $300 $400.
Chapter 15 Graphics. To paint, you need to specify where to paint. Each component has its own coordinate system with the origin (0, 0) at the upper-left.
Copyright 2010 by Pearson Education Building Java Programs More Graphics reading: Supplement 3G.
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.
Numbers & Geometry Points and Distances. 6/3/2013 Numbers and Geometry 2 Distance d between numbers a and b d = Example: | = Points and Distances.
 putpixel function plots a pixel at location (x, y) of specified color.  Declaration :- void putpixel(int x, int y, int color);
On / By / With The building blocks of the Mplus language.
Made with love, by Zachary Langley Applets The Graphics Presentation.
23-8 3x6 Double it Take Away 6 Share By 9 Double it +10 Halve it Beginner Start Answer Intermediate 70 50% of this ÷7÷7 x8 Double it Start Answer.
Computer Graphics … how renderings are done on a computer. Art 321 Dr. J Parker Winter.
2 x0 0 12/13/2014 Know Your Facts!. 2 x1 2 12/13/2014 Know Your Facts!
Lesson One: The Beginning
Web Design & Development Lecture 19. Java Graphics 2.
Life Cycle of an Applet, along with response to mouse events and other simple features. (ps:only two possible examples for life circle of an Applet)
Frame Windows A frame object is used to create a graphical frame window. This frame is used to show information in a graphical application. The JFrame.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 21.
Polymorphism Method overriding Method overloading Dynamic binding 1.
Chapter 2: Using Objects Part 2. Assume you wish to test the behaviour of some method. This is accomplished by providing a tester class: Supply a main.
Object Oriented Programming one of the main reasons we now teach Java instead of C++. C++ was designed to be backwardly compatible with the original.
Object Oriented Programming one of the main reasons we now teach Java instead of C++. C++ was designed to be backwardly compatible with the original.
Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
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.
Chapter 2 Programming by Example. A Holistic Perspective Three sections separated by blank lines. Program comment File: FileName.java
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects.
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 UI methods, Graphical Output.
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.
© 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.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Call a Method,
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 with Parameters COMP.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Files COMP 102.
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 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.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Classes and.
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 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 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.
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 Program Elements and Syntax.
Programs with graphics output
Presentation transcript:

Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Graphical Output, Call a method in the same class COMP 102 #5 2014T2

© Xiaoying Gao, Peter Andreae COMP102 5:2 Menu Programs with Graphical output Good design with variables Call a method in the same class this parameters Administrivia: A2 is handed out Class Rep: Shaika, Elroy This Friday: last day for withdrawal with refund

© Xiaoying Gao, Peter Andreae COMP 102 5: 3 Version 1 import ecs100.*; import java.awt.Color; public class DrawShapes{ public void drawPicture(){ UI.setColor(Color.green); UI.fillRect(100,150,200,100); UI.setColor(Color.red); UI.fillOval(150,150, 100, 100); UI.setColor(Color.orange); UI.drawLine(0, 200, 400, 200); UI.fillArc(150, 150, 100, 100, 30, 120); }

© Xiaoying Gao, Peter Andreae COMP 102 5: 4 Version 2 import ecs100.*; import java.awt.Color; public class DrawShapes2{ public void drawPicture(){ double x = 200; //centr x double y = 200; //center y double d = 100; //diameter UI.setColor(Color.green); UI.fillRect(x-d, y-d/2, 2*d, d); UI.setColor(Color.red); UI.fillOval(x-d/2, y-d/2, d, d); UI.setColor(Color.orange); UI.drawLine(x-2*d, y, x+2*d, y); UI.fillArc(x-d/2, y-d/2, d, d, 30, 120); }

© Xiaoying Gao, Peter Andreae COMP102 5:5 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 "centerX" instead of “boxLeft" or “lineLeft" ? why did I choose “diameter" to be the size? We have effectively parameterised the drawing three values (x, y, d) control the whole thing.

© Xiaoying Gao, Peter Andreae COMP102 5:6 Call another method in the same class import ecs100.*; public class Example4{ public void printMessages(){ this.message(); } public void message(){ UI.println("Hello "); //can do complicated things instead }

© Xiaoying Gao, Peter Andreae COMP102 5:7 Call a method with parameters import ecs100.*; public class Example5{ public void printMessages(){ this.message("Alex"); this.message("Linda"); this.message("Sam"); } public void message(String name){ UI.println("Hello " + name); }

© Xiaoying Gao, Peter Andreae COMP102 5:8 Call a method with more parameters import ecs100.*; public class Example6{ public void printMessages(){ UI.println("room booking"); this.message("Alex", 20); this.message("Linda", 12); this.message("Sam", 5); } public void message(String name, int x){ UI.println("Hello " + name); UI.println("room no." + x); }

© Xiaoying Gao, Peter Andreae COMP102 5:9 Call a method in the same class In the same class, normally this.methodName(arguments) this is the default object for the current class Arguments: the values to initialise the parameters Parameters: In the method to be called Method header (type name, type name, type name) Arguments and parameters have to match Type Order

© Xiaoying Gao, Peter Andreae COMP102 5:10 Method Definitions (2) /** A method with parameters*/ public void message( String name, int x ){ ….; 〈 Comment 〉〈 Header 〉〈 Body 〉 {} public void 〈 parameters 〉 () 〈 name 〉 〈 type 〉, 〈 name 〉 How do you call a method with parameters from BlueJ?

© Xiaoying Gao, Peter Andreae COMP102 5:11 More Examples import ecs100.*; public class Triangle{ public void ManyTriangles(){ this.drawTri(100, 100, 40, 50, 200, 400); this.drawTri(300,200, 150, 100, 200, 200); } public void drawTri(double x1, double y1, double x2, double y2, double x3, double y3){ UI.drawLine(x1, y1, x2, y2); UI.drawLine(x2, y2, x3, y3); UI.drawLine(x3, y3, x1, y1); }

© Xiaoying Gao, Peter Andreae COMP102 5:12 More examples import ecs100.*; import java.awt.Color; public class DrawShapes3{ public void drawMany(){ this.drawPicture(300, 100, ); this.drawPicture(300,200, 100); } public void drawPicture(double x, double y, double d){ UI.setColor(Color.green); UI.fillRect(x-d,y-d/2,2*d,d); UI.setColor(Color.red); UI.fillOval(x-d/2, y-d/2, d, d); UI.setColor(Color.orange); UI.drawLine(x-2*d, y, x+2*d, y); UI.fillArc(x-d/2, y-d/2, d, d, 30, 120);} }

© Xiaoying Gao, Peter Andreae COMP102 5:13 Method Definition: Like a pad of worksheets Calling a Method: this.drawPicture(300, 100, 75); ⇒ get a “copy” of the method worksheet ⇒ copy the arguments to the parameter places ⇒ perform each action in the body ⇒ throw the worksheet away (losing all the information on it) Method Calls with parameters public void drawPicture( double x, double y, double d){ UI.setColor(Color.green); UI.fillRect(x-d,y-d/2,2*d,d); public void drawPicture( double x, double y, double d){ UI.setColor(Color.green); UI.fillRect(x-d,y-d/2,2*d,d);