Computer Graphics - Lecture 6

Slides:



Advertisements
Similar presentations
Java Applets:. How Applets differ from application?: They do not use main method but init(), start() and paint() methods of the applet class They can.
Advertisements

CS18000: Problem Solving and Object-Oriented Programming.
Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee.
CompSci Today’s topics Java Recursion in Graphics Writing a Class Simple Animation Upcoming Simulation Reading Great Ideas, Chapters 5.
Lecture 15.1 Static Methods and Variables. © 2006 Pearson Addison-Wesley. All rights reserved Static Methods In Java it is possible to declare.
Cosc 1P02 Week 2 Lecture slides
Recursion1 Barb Ericson Georgia Institute of Technology March 2010.
CS-I Final Review Hao Jiang Computer Science Department Boston College.
Events CSC 171 FALL 2001 LECTURE 9. History: the ABC John Vincent Atanasoff, with John Berry, developed the machine we now call the ABC -- the.
Java: How to Program Arrays and ArrayLists Summary Yingcai Xiao.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Programming Task: Task 1 Controlled Assessment Practice.
Recursive Methods Noter ch.2. QUIZ What is the result of the method call foo(4) ? 1.Prints 4 2.Prints 1 3.Prints Prints Compiler error:
Internet Software Development Applets Paul J Krause.
Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise www2.hh.se/staff/jebe/oop2005/
1 Object Oriented Programming Lecture II Classes, Objects, Abstract Data Types, Representation Invariants, Mutables and Immutables.
Main Points: - Python Turtle - Fractals
Java: An Eventful Approach An innovative approach to teaching Java in CS1 † Recursion Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College † Partially.
The Drawing program – Java Applets
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.
Lec 16 Adding Mouse and KeyEvent handlers to an Applet Class.
BallWorld.java A structured walkthrough. Key Features: 2 classes are created Execution is done through the procedure called “main” which are decleared.
Java Applet Basics (2). The Body Mass Index Calculator.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
1 Lecture 3 More about C++. 2 Topic for today More about classMore about class –Init list –Inline functions –Const –Default function parameters –Static.
2/5/00SEM107 © Kamin & Reddy Review -1 Class 19 - Review r This lecture contains a selection of slides from previous lectures, giving the “high points”
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions.
BallWorld.java Ball.java A functional walkthrough Part 3: the interaction of objects.
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
SNU OOPSLA Lab. 1 Great Ideas of CS with Java Part 1 WWW & Computer programming in the language Java Ch 1: The World Wide Web Ch 2: Watch out: Here comes.
CS100A, Fall Lecture 8 1 CS100A, Fall 1997 Lecture 8, Thursday, 25 September. More on Iteration We’ll develop some loops of the form: // general.
Chapter 2 Creating a Java Application and Applet.
CSI 3125, Preliminaries, page 1 Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name,
RECURSION, CONTINUED Lecture 8 CS2110 – Fall 2015.
More OOP. Extending other’s classes extend Java platform classes, e.g. class Applet public class MyApplet extends Applet { public void init() { } public.
Programming Training kiddo Main Points: - Python Statements - Problems with selections.
Recursion in Java The answer to life’s greatest mysteries are on the last slide.
Chapter 11: Threaded Programs Situations where the program is following multiple execution paths (how to stop one?) Thread: a line of execution Thread.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
April 13, 1998CS102-02Lecture 3-1 Data Types in Java CS Lecture 3-1 Java’s Central Casting.
Recursion CITS1001.
Chapter Topics Chapter 16 discusses the following main topics:
Modern Programming Tools And Techniques-I
Lecture 24: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Recursion -- Introduction
Main Points: - Python Turtle - Fractals
More methods, more capabilities
Interface.
Chapter 12 Recursion (methods calling themselves)
LRobot Game.
Introduction to Java Programming
The Building Blocks Classes: Java class library, over 1,800 classes:
METHOD OVERRIDING in JAVA
Section 9.1 Introduction.
Unit 3 Test: Friday.
Recursion Definition: A method that calls itself. Examples
Main Points: - Python Turtle - Fractals
Recursion Think ESCHER.
Method of Classes Chapter 7, page 155 Lecture /4/6.
Dr. Sampath Jayarathna Cal Poly Pomona
Computer Graphics - Lecture 6
Enabling Application Delivery Via the Web
Drawing Shapes (Graphics Class) Creating Objects Packages
CMPT 120 Lecture 19 – Unit 3 – Graphics and Animation
Types of Recursive Methods
Presentation transcript:

Computer Graphics - Lecture 6 Turtle Graphics Objectives To design and construct the class Turtle. To give some details about recursive methods. To present the principles of Turtle Graphics. To construct some Turtle images: trees, Koch curve, Serpinski curve,... 5/6/2019 Computer Graphics - Lecture 6

Working with Java classes Constructing a class /Constructing the object this. - The class variables: private, … - The class constructors: Same name as the class; do not have returning type. Initialise the class’ variables. - The class methods: do an action on the class data. Type of methods: get/set, draw/print, …. Action on this  non-static; Create an object (call a constructor) : NameClass obj = new NameClass(a1,a2,…); Call a method obj.nameMethod(a1,a2,…); NameClass.nameMethod(a1,a2,…); 5/6/2019 Computer Graphics - Lecture 6

Turtle-like Graphics Pens What can turtles (as animals) do? Move forward or backward. Turn left or right. Retreat the head into the shell. Variables used for our Turtle-like Graphics Pens. x, y  give the turtle pen coordinates. Work with doubles dir  the direction to go in radians. pen  the pen’s status (pen=1 draws line) 5/6/2019 Computer Graphics - Lecture 6

Design of the Class Turtle public void moveTo(int x0,int y0) {x=x0;y=y0;} public void up(){pen=0;} public void down(){pen=1;} public void left(double a){dir+=a;} public void right(double a){dir-=a;} public void forward(int d,Graphics g){} public void backward(int d,Graphics g) {forward(-d,g);} } A Turtle object is a pen such that: - Has a direction to go - Can draw lines. public class Turtle{ public final static double PI=Math.PI; private double dir; private int pen; private double x,y; public Turtle(int x0,int y0,int dir0); public Turtle(); public double getDir(){return dir;} public int getX(){return x}; public int getY(){return y}; 5/6/2019 Computer Graphics - Lecture 6

Implement and Test the Class Turtle import java.awt.*; import java.applet.*; public class L6Appl1 extends Applet{ Turtle t = new Turtle(); final double PI=Math.PI; public void paint(Graphics g) { t.moveTo(20,20); t.forward(100,g);t.right(PI/2); } See the execution public Turtle(int x0,int y0,double dir0){ x=x0;y=y0;dir=dir0; pen=1; } public Turtle(){ x=250;y=250;dir=0;pen=1; public void forward(int d,Graphics g){ double x1,y1; x1=(int)(x+d*Math.cos(dir)); y1=(int)(y+d*Math.sin(dir)); if (pen==1) g.drawLine((int)x,(int)y,(int)x1,(int)y1); x=x1;y=y1; 5/6/2019 Computer Graphics - Lecture 6

Computer Graphics - Lecture 6 Recursive Methods A method can call any (known) methods from the class or libraries. A method is recursive when invokes itself. Important Rule: A recursive method must have a termination step that solves directly the problem. type myRecMethod(type1 arg1, type2 arg2, …. ){ if (termination) {calculate directly the result res; return res; } … myRecMethod(a1,a2,…) } int sum (int n, int [] a){ if(n==0) return 0; // termination step int s1 = sum(n-1,a); // recursive call return s1+a[n-1]; 5/6/2019 Computer Graphics - Lecture 6

Principles of Turtle Geometry 1. Define Recursively the figure Fn. - termination step: give the form of F0 - define Fn based on Fn-1. 2. Use a Turtle object to draw the figure. - the direction of the Turtle object must be the same. Turtle Geometry represents the simplest way to construct fractals. 5/6/2019 Computer Graphics - Lecture 6

Computer Graphics - Lecture 6 Binary Tree (1) Consider T(n,l) the binary tree. n - the order n; l - the length. T(0,l)= a line with the length l. T(n,l) is defined as follows: - construct the trunk - left 45 (PI/4) - construct T(n-1,l/2) - right 90 (PI/2) - go back at the root public void tree(int n, int l, Graphics g){ if(l==1 || n==0) return; t.forward(l,g); t.left(PI/4);tree(n-1,l/2,g); t.right(PI/2);tree(n-1,l/2,g); t.left(PI/4); t.backward(l,g); } 5/6/2019 Computer Graphics - Lecture 6

Computer Graphics - Lecture 6 Binary Tree (2) import java.awt.*; import java.applet.*; import Turtle; public class L6Appl2 extends Applet{ TextField textField1, textField2; Turtle t = new Turtle(); public void init(){ textField1 = new TextField(10); textField2 = new TextField(10); add(textField1); add(textField2); textField1.setText("2"); textField2.setText("200"); } public void paint(Graphics g){ String s = textField1.getText(); int order = Integer.parseInt(s); s = textField2.getText(); int length = Integer.parseInt(s); t.moveTo(10,300); tree(order,length,g); } public boolean action(Event event, Object arg){ repaint(); return true; The declaration of the method Tree Execution 5/6/2019 Computer Graphics - Lecture 6

Computer Graphics - Lecture 6 5/6/2019 Computer Graphics - Lecture 6

Computer Graphics - Lecture 6 5/6/2019 Computer Graphics - Lecture 6

Computer Graphics - Lecture 6 5/6/2019 Computer Graphics - Lecture 6

Computer Graphics - Lecture 6 Koch’s Fractal public void koch(int n, int l, Graphics g){ if(l<2 || n==0){t.forward(l,g);return;} koch(n-1,l/3,g); t.left(PI/3);koch(n-1,l/3,g); t.right(2*PI/3);koch(n-1,l/3,g); } public void flake(int n, int l, Graphics g){ for(int i=0;i<3;i++){ koch(n,l,g); t.left(2*PI/3); Consider K(n,l) the Koch’s curve. K(0,l)= a line. K(n,l) is defined as follows: - construct K(n-1,l/3); - left 60 (PI/3); construct K(n-1,l/3) - right 120 (2PI/3); construct K(n-1,l/3) The snow flake F(n,k) - construct K(n,l); left 120 (2PI/3); F(n,k) is a fractal representing an infinite curve bounding a finite area. 5/6/2019 Computer Graphics - Lecture 6

Computer Graphics - Lecture 6 5/6/2019 Computer Graphics - Lecture 6

Computer Graphics - Lecture 6 5/6/2019 Computer Graphics - Lecture 6

Computer Graphics - Lecture 6 5/6/2019 Computer Graphics - Lecture 6

Sierpinski’s Fractal (curve) Consider S(n,l) the Sierpinski’s curve. Find d=sqrt(l); S(0,l) = nothing. S(n,l) is defined as follows: - construct S(n-1,l); right 45; forward d - right 45; construct S(n-1,l); - left 90; forward l; left 90; - construct S(n-1,l); - right 45;forward d; right 45 S(n,k) is a fractal representing an infinite curve bounded by finite area. public void s(int n, int l, Graphics g){ int d=(int)l/Math.sqrt(2); if(n==0)return; s(n-1,l,g);t.right(PI/4);t.forward((int)d,g); t.right(PI/4);s(n-1,l,g); t.left(PI/2);t.forward(l,g);t.left(PI/2); s(n-1,l,g); t.right(PI/4);t.forward((int)d,g);t.right(PI/4); } 5/6/2019 Computer Graphics - Lecture 6

Computer Graphics - Lecture 6 5/6/2019 Computer Graphics - Lecture 6

Computer Graphics - Lecture 6 5/6/2019 Computer Graphics - Lecture 6

Computer Graphics - Lecture 6 5/6/2019 Computer Graphics - Lecture 6

Computer Graphics - Lecture 6 Problems to Solve Write an Applet to draw the Curve C(n,l). C(0,l) is a line with the length l. C(n,l) is defined construct C(n-1,l); right 90; construct C(n-1,l);left 90; Define yourself and construct a tree with 4 branches. Problems to read: Recursive Methods: Java Black Book, Using recursion, pp 192. An advanced topic about Turtle fractals: http://www.math.sunysb.edu/~scott/Book331/turtle_in_fractal.html 5/6/2019 Computer Graphics - Lecture 6