More methods, more capabilities

Slides:



Advertisements
Similar presentations
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
Advertisements

Cosc 1P02 Week 2 Lecture slides
CSE 113 Week 3 January 28 – February 1, Monday Announcements  Software Installation Fest: 2/5 and 2/6 4pm – 7pm in Baldy 21 Bring your laptop or.
Lecture 2. Review To play with Turtle, we need to download and install followings: 1.JDK 6 2.Dr. Java 3.Sample program (e.g. BookClass)
Lecture 6. What is the difference in pictures ???
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
Turtle Graphics  Turtle graphics provide a simple way to draw pictures in a window.  The name suggests the way in which we can think about the drawing.
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
TOPIC 3 INTRODUCTION TO PROGRAMMING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
COSC 1P02 Introduction to Computer Science 3.1 Cosc 1P02 Week 3 Lecture slides Birthdays are good for you. Statistics show that the people who have the.
Copyright © 2009 Curt Hill The Picture Object Getting and displaying.
Microsoft® Small Basic
Copyright © 2015 Curt Hill Models and Textures Making your entity interesting.
Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.
Turtle Graphics  Turtle graphics provide a simple way to draw pictures in a window.  The name suggests the way in which we can think about the drawing.
Copyright © Curt Hill Turtles The beginning of media computation.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
Java is an object oriented programming language In this model of programming all entities are objects that have methods and fields Methods perform tasks.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Copyright © Curt Hill Further Picture Manipulation Considering position.
CSC1401 Drawing in Java - 1. Goals Understand at a conceptual and practical level How to use the Turtle class to draw on a picture How to use the java.awt.Graphics.
Unit 2.6 Data Representation Lesson 3 ‒ Images
Getting and displaying
Pixels, Colors and Shapes
Chapter 3 Syntax, Errors, and Debugging
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Week 8 - Monday CS 121.
Graphics and Multimedia
Building Java Programs
Lesson One: The Beginning Chapter 1: Pixels Learning Processing Daniel Shiffman Presentation by Donald W. Smith Graphics from
A Tiny Look at the Graphics Window
Basic Graphics Drawing Shapes 1.
Computer Graphics.
Methods Additional Topics
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Bitmap, Vector, Pixels, Resolution, Metadata.
Board: Objects, Arrays and Pedagogy
Microsoft® Small Basic
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.
البرمجة مع لغة PYTHON TURTLE
Building Java Programs
Graphics -- Introduction
CSc 110, Spring 2018 Lecture 9: Parameters, Graphics and Random
Building Java Programs
Looping through pixels.
Arrays in Java What, why and how Copyright Curt Hill.
CSE 113 A January 26 – 30, 2009.
Teaching Java using Turtles part 2
Other displays Saving Arrays Using fors to process
CSE 142 Lecture Notes Graphics with DrawingPanel Chapter 3
Methods Again Parameter Passage
Building Java Programs
Introduction to Turtle Graphics
January 26 – 30, 2009 CSE 113 B.
Using Logo and Logic This presentation uses a version of Logo called StarLogo available through MIT. It can be downloaded for free and installed on your.
Building Java Programs
Building Java Programs
A Tiny Look at the Graphics Window
Building Java Programs
Building Java Programs
Building Java Programs
Graphics Reading: Supplement 3G
Building Java Programs
Teaching Java using Turtles part 2
The beginning of media computation Followed by a demo
Methods Coding in Java Copyright © Curt Hill.
Presentation transcript:

More methods, more capabilities Turtles More methods, more capabilities Copyright © 1997-2010 Curt Hill

Introduction Basics were covered last time but there is more Signatures Coordinates Constructors Alternate methods Pen motion Colors Copyright © 1997-2010 Curt Hill

Signatures A method signature consists of: Name of the method Number and type of the parameters In most previous languages it was the name only In Java two methods may have same name but must have different signatures This is known as overloading Copyright © 1997-2010 Curt Hill

Example The Turtle method forward has two signatures: forward(int k); Move forward k pixels forward(); Move forward 100 pixels Thus: Sam.forward(100); // and Sam.forward(); Do exactly the same thing Copyright © 1997-2010 Curt Hill

Coordinate Systems The drawing surface is different than the coordinates used in algebra The upper left corner is point (0,0) The X coordinate gets larger as we go right The Y gets larger as we go down The default World is 640 pixels wide (x value) and 480 pixels high (y value) Copyright © 1997-2010 Curt Hill

Default World Orientation (0,0) (640,0) (0,480) (640,480) Copyright © 1997-2010 Curt Hill

World Sizes The default constructor for World gives a 640 by 480 size panel World w = new World(); There is another constructor that allows different sizing: World w2 = new World(1000, 700); This gives width of 1000 pixels and height of 700 Two constructors with two signatures Copyright © 1997-2010 Curt Hill

Turtle Constructors The first constructor only used the world The location defaulted to the center Second gives the location and then world Signature: Turtle(int x, int y, World w); Example: yertle = new Turtle(50,50,myW); Copyright © 1997-2010 Curt Hill

Movement If you can move forward why not backward? It has the same form: backward(int pixels) yertle.backward(50); backward() Moves 100 pixels backward Copyright © 1997-2010 Curt Hill

Relative Movement Forward and backward are relative Start at turtles location and move in the turtles current heading There is also a moveTo method Takes a target location and moves to that absolute location Does not change heading Ignores it Copyright © 1997-2010 Curt Hill

Example Consider the following code: World w = new World(500,300); Turtle t = new Turtle(80,150,w); t.turn(75); t.forward(); t.moveTo(200,200); t.forward(100); This would produce the picture in next screen Copyright © 1997-2010 Curt Hill

Example Copyright © 1997-2010 Curt Hill

Commentary The last screen demonstrated A different sized world Three parameter Turtle constructor forward with no parameters moveTo changing position but not heading Copyright © 1997-2010 Curt Hill

Pen Movement After construction the pen is down Thus any move leaves a trail Two commands: penUp() penDown() Like all methods the object must occur first: yertle.penUp(); yertle.forward(200); yertle.penDown(); Copyright © 1997-2010 Curt Hill

Hiding and displaying The hide() method will make the turtle invisible This will not affect the previously drawn lines or the status of the pen up or down The show() method will make the turtle visible Also does not affect lines or pen status Copyright © 1997-2010 Curt Hill

Turning The turn method is the most useful but there are others turnLeft() is the same as turn(-90) turnRight() is the same as turn(90); turnToFace(int x, int y) sets the heading so that the turtle is pointed at that location turnToFace(Turtle) sets the heading so that the turtle is pointed at a second turtle Copyright © 1997-2010 Curt Hill

Another Example The following code: World w = new World(500,300); Turtle t = new Turtle(100,150,w); t.turnRight(); t.forward(); t.penUp(); // Move but don't draw t.moveTo(200,200); t.penDown(); t.forward(100); Produces next screen Copyright © 1997-2010 Curt Hill

Example Copyright © 1997-2010 Curt Hill

Colors The color of the turtle and the pen may be changed This requires some knowledge of how Java handles colors Almost everything in Java is a class Color is a class as well A color is produced on a monitor or screen by making each pixel glow with certain red, green and blue values Copyright © 1997-2010 Curt Hill

Color Constructors Color(int red,int green, int blue); All integers in 0-255 range The larger the value the brighter 255,255,255 is white Color(int red, int green, int blue, int alpha); Alpha is a transparency indicator Used with multiple color planes 0 is black/invisible - 255 is bright/opaque Copyright © 1997-2010 Curt Hill

Color Constructors A constructor is always used following the new keyword Most times this is in an assignment But not usually when dealing with colors Example: new Color(255, 0, 0) This produces bright red Copyright © 1997-2010 Curt Hill

Color Constants As a convenience several constant colors are available black, blue, cyan, darkGray, gray, green, lightGray, etc These must be referred to with a Color class or just Color prefixed Color.cyan These may be all lower or all upper case Copyright © 1997-2010 Curt Hill

Imports The Color class requires an import statement import java.awt.Color; This is placed before the first public class Placed after the package statement, if there is one Copyright © 1997-2010 Curt Hill

Turtle colors The setColor method changes the turtle color and the pen color The parameter is either a color constant or a newly created special color: yertle.setColor(Color.RED); yertle.setColor(new Color(255, 128, 10)); Constants do not need the new The constructor does need the new Copyright © 1997-2010 Curt Hill

Example Notes The import statement is needed for colors There is both a constant and constructor color Use the constant ones for common colors Use the constructor for greater refinement Copyright © 1997-2010 Curt Hill

Example import java.awt.Color; public class TurtleExample2 { public static void main(String []a){ World w = new World(500,300); Turtle t = new Turtle(100,150,w); t.turnRight(); t.forward(); t.setColor(Color.RED); t.turn(90); t.setColor(new Color(128,64,255)); t.forward(100); } Copyright © 1997-2010 Curt Hill

What it looks like Copyright © 1997-2010 Curt Hill

More Color Commands The turtle icon does not have to match the pen color setPenColor(Color) will change the pen color without changing the turtle color setShellColor(Color) changes the turtle shell without changing the pen or feet color Copyright © 1997-2010 Curt Hill

Line Width So far our turtle has only drawn lines that are one pixel wide This is OK and maybe even what we want We can control the width with the setPenWidth(int) method It takes an integer as a parameter Example: yertle.setPenWidth(5) Copyright © 1997-2010 Curt Hill

Example World w = new World(500,300); Turtle t = new Turtle(100,150,w); t.turnRight(); t.forward(); t.setColor(Color.RED); t.setPenWidth(5); t.turn(-90); t.setColor(new Color(128,64,255)); t.forward(100); t.setPenColor(Color.yellow); Copyright © 1997-2010 Curt Hill

Result of Example Code Copyright © 1997-2010 Curt Hill

Bounds The turtle must stay within the bounds of the world If the turtle is pointed towards an edge and then moves forward it will exceed the edge The turtle will then just disappear Copyright © 1997-2010 Curt Hill

Summary We can now create worlds of various sizes and turtles starting in different positions The turtles can move with or without a trail We have several ways to move them or turn them We can make their trails and themselves any color we like Copyright © 1997-2010 Curt Hill