CS 106A, Lecture 11 Graphics

Similar documents
CS 106A Midterm Review. Rishi Bedi, adapted from slides by Kate Rydberg and Nick Troccoli Summer 2017

CS 106A, Lecture 14 Events and Instance Variables

CS 106A, Lecture 14 Events and Instance Variables

Variables, Types, and Expressions

Object-Oriented Programming

Expressions, Statements, and Control Structures

Objects and Graphics

CS 106A, Lecture 27 Final Exam Review 1

Programming Lecture 2. Programming by example (Chapter 2) Hello world Patterns Classes, objects Graphical programming

Nested Loops Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

Revisiting acm.graphics

Memory Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

Simple Java YEAH Hours. Brahm Capoor and Vrinda Vasavada

CS 106A, Lecture 27 Final Exam Review 1

CS 106A July 24, 2017 CS 106A Midterm Exam Question Booklet

CS106A Review Session

Interactive Graphics. Eric Roberts Handout #23 CS 106A January 25, 2016 Interactive Graphics. Computer Graphics and the Utah Teapot.

Interactive Graphics

Solutions for Section #2

Solutions to Section #7

CS 106A, Lecture 23 Interactors and GCanvas

Getting Familiar with ACM_JTF

CS 106A, Lecture 25 Life After CS 106A, Part 1

Topic 8 Graphics. Margaret Hamilton

Solutions for Section #2

CS 106A, Lecture 19 ArrayLists

Topic 8 graphics. -mgrimes, Graphics problem report 134

Programming Lecture 2. Programming by example (Chapter 2) Hello world Patterns Classes, objects Graphical programming

CS 106A, Lecture 24 Interactors and NameSurfer

Quick Reference. There are several already named colors which you can use, but you must import the appropriate Java library.

Solutions for Section #4

CS 101 Computer Programming

File: GFace.java /* * File: GFace.java * This class implements a face as a GCompound. */

What is Java? professional software engineering.

Drawing Geometrical Objects. Graphic courtesy of Eric Roberts

Java Programming Lecture 6

Garfield AP CS. Graphics

Building Java Programs

CS 106A, Lecture 16 Arrays

Chapter 2 ACM Java Graphics

CISC 1600 Lecture 3.1 Introduction to Processing

Building Java Programs

CS 106A, Lecture 5 Booleans and Control Flow

CS 106X Lecture 26: Inheritance and Polymorphism in C++

Hangman YEAH Hours. Thursday, February 14, 7:30 9:00PM Andrew Tierno

CS 170 Java Programming 1. Week 7: More on Logic

CS 106A, Lecture 20 ArrayLists and HashMaps

CS106A Final Exam Review Session. Saturday Dec. 10, 2016 Nick Troccoli

Slide 1 CS 170 Java Programming 1 Object-Oriented Graphics Duration: 00:00:18 Advance mode: Auto

CS 106A, Lecture 7 Parameters and Return

Programming Abstractions

SUBCLASSES IN JAVA - II

Solutions to Midterm Exam

CS 106A, Lecture 3 Problem-solving with Karel

Adapted from slides by Brahm Capoor. Breakout YEAH hours. Michael (Chung Troute)

Programming via Java Subclasses

JavaScript Programs Once upon a time...

Introduction to Java

CSc 110, Spring 2018 Lecture 9: Parameters, Graphics and Random. Adapted from slides by Marty Stepp and Stuart Reges

Using Graphics. Building Java Programs Supplement 3G

Building Java Programs

Primitives, Objects, and Heap/Stack Review 1 Chris Piech CS106A, Stanford University

CS 170 Java Programming 1. Week 10: Loops and Arrays

Assignment 2: Welcome to Java!

CS106A Review Session. Monday Oct. 31, 2016 Nick Troccoli

Assignment 6 YEAH Hours. Ben Barnett and Avery Wang

Graphics and Java 2D Introduction OBJECTIVES. One picture is worth ten thousand words.

CSc 110, Autumn 2016 Lecture 7: Graphics. Adapted from slides by Marty Stepp and Stuart Reges

YEAH Hours. January , 7-8 PM Jared Wolens

CS 106A, Lecture 9 Problem-Solving with Strings

Data Structure Design II Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

Solution to Section #3 Portions of this handout by Eric Roberts, Mehran Sahami, Marty Stepp, Patrick Young and Jeremy Keeshin

CS 106A, Lecture 9 Problem-Solving with Strings

Building Java Programs

CS 106A, Lecture 3 Problem-solving with Karel

JavaScript Programs. Jerry Cain CS 106AJ October 4, 2017

Solution to Section #7

Programming Abstractions

Graphics Module Reference

COMP102: Test 2 Model Solutions

Programming Lecture 10

Interactors. Brahm Capoor.

Graphics Module Reference

12/22/11. } Rolling a Six-Sided Die. } Fig 6.7: Rolling a Six-Sided Die 6,000,000 Times

An Introduction to Processing

Programming Lecture 10

CS106AJ Midterm Review Session. October 28, 2017 Kat Gregory and Ryan Eberhardt

5. Introduction to Procedures

CS 106B, Lecture 1 Introduction to C++

CIS 110: Introduction to Computer Programming

Practice Final Examination #2

Practice Midterm #2. Midterm Time: Monday, July 18 th, 7pm 9pm Midterm Location: Hewlett 200

Lecture 15. For-Loops

Assignment #3 Breakout!

Topic 9 More Graphics. Based on slides bu Marty Stepp and Stuart Reges from

Programming via Java Defining classes

Creating Special Effects with Text

Assignment A7 BREAKOUT CS1110 Spring 2011 Due Fri 6 May 1

What is a variable? A named locajon in the computer s memory. A variable stores values

CS Problem Solving and Object-Oriented Programming

Transcription:

CS 106A, Lecture 11 Graphics reading: Art & Science of Java, 9.1-9.3 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on slides created by Keith Schwarz, Mehran Sahami, Eric Roberts, Stuart Reges, and others.

Announcements Recap: File Reading GraphicsProgram Graphical Objects Practice: Car Plan For Today 2

Announcements Read the rest of the slides from yesterday and try the Election practice problem Assignment 3 is out demo coming soon! 3

Announcements Recap: File Reading GraphicsProgram Graphical Objects Practice: Car Plan For Today 4

File Reading Overview 1. Make a Scanner to open a file to read Scanner input = new Scanner(new File("data.txt")); 2. Use Scanner methods such as nextline or next to read in the file, usually in a loop while some variation of hasnext is true 3. Scanner operations on files are dangerous because they dependent on outside resources, so we need to use a try/catch block 4. Close the Scanner when you are done: input.close() 5

File Reading Overview 1. Make a Scanner to open a file to read Scanner input = new Scanner(new File("data.txt")); 2. Use Scanner methods such as nextline or next to read in the file, usually in a loop while some variation of hasnext is true 3. Scanner operations on files are dangerous because they dependent on outside resources, so we need to use a try/catch block 4. Close the Scanner when you are done: input.close() 6

Scanner methods Method sc.nextline() sc.next() sc.nextint() sc.nextdouble() sc.hasnextline() sc.hasnext() sc.hasnextint() sc.hasnextdouble() sc.close(); Description reads and returns a one-line String from the file reads and returns a one-word String from the file reads and returns an int from the file reads and returns a double from the file returns true if there are any more lines returns true if there are any more tokens returns true if there is a next token and it's an int returns true if there is a next token and it's a double should be called when done reading the file 7

File Reading Overview 1. Make a Scanner to open a file to read Scanner input = new Scanner(new File("data.txt")); 2. Use Scanner methods such as nextline or next to read in the file, usually in a loop while some variation of hasnext is true 3. Scanner operations on files are dangerous because they dependent on outside resources, so we need to use a try/catch block 4. Close the Scanner when you are done: input.close() 8

Try/Catch try { statements; // code that might throw an exception } catch (ExceptionType name) { statements; // code to handle the error } To execute code that might throw an exception, you must enclose it in a try/catch statement. try { Scanner input = new Scanner(new File("data.txt"));... } catch (IOException ex) { println("error reading the file: " + ex); } 9

Try/Catch To execute code that might throw an exception, you must enclose it in a try/catch statement. If something fails up here try { Scanner input = new Scanner(new File("data.txt")); while (input.hasnextline()) { String line = input.nextline(); println(line); } } catch (FileNotFoundException ex) { println("error reading the file: " + ex); } 10

Try/Catch To execute code that might throw an exception, you must enclose it in a try/catch statement. If something fails up here try { Scanner input = new Scanner(new File("data.txt")); while (input.hasnextline()) { String line = input.nextline(); println(line); } } catch (FileNotFoundException ex) { println("error reading the file: " + ex); } we immediately jump down here. 11

Announcements Recap: File Reading GraphicsProgram Graphical Objects Practice: Car Plan For Today 12

HW4: Breakout You are here Graphics Programs Animation The River of Java Events Memory 13

Java Program Karel Program Console Program Graphics Program SuperKarel Program 14

Graphics Programs 15

Our First GraphicsProgram 16

Our First GraphicsProgram import acm.program.*; import acm.graphics.*; // Stanford graphical objects import java.awt.*; // Java graphical objects public class MyGraphics extends GraphicsProgram { public void run() { GRect rect = new GRect(50, 50, 200, 250); rect.setfilled(true); rect.setcolor(color.red); } } add(rect); 17

Our First GraphicsProgram // Create a 200x250 GRect at (50, 50) GRect rect = new GRect(50, 50, 200, 250); // Set some properties rect.setfilled(true); rect.setcolor(color.red); // Add to the canvas add(rect); 18

Our First GraphicsProgram // Create a 200x250 GRect at (50, 50) GRect rect = new GRect(50, 50, 200, 250); // Set some properties rect.setfilled(true); rect.setcolor(color.red); // Add to the canvas add(rect); 19

Our First GraphicsProgram // Create a 200x250 GRect at (50, 50) GRect rect = new GRect(50, 50, 200, 250); // Set some properties rect.setfilled(true); rect.setcolor(color.red); // Add to the canvas add(rect); 20

Our First GraphicsProgram // Create a 200x250 GRect at (50, 50) GRect rect = new GRect(50, 50, 200, 250); // Set some properties rect.setfilled(true); rect.setcolor(color.red); // Add to the canvas add(rect); 21

0,0 The Graphics Canvas 40,20 120,40 40,120 22

Collage Model Must have been added first 23

Plan For Today Announcements Recap: File Reading GraphicsProgram Graphical Objects Practice: Cars and Checkerboards 24

Graphical Objects GRect GOval GLine (x, y) (x, y) (x 1, y 1 ) (x+w, (x+w, y+h) y+h) GLabel GImage GArc Hello there! (x 2, y 2 ) GRoundRect GPolygon 25

Graphical Objects GRect GOval GLine (x, y) (x, y) (x 1, y 1 ) (x+w, (x+w, y+h) y+h) GLabel GImage GArc Hello there! (x 2, y 2 ) GRoundRect GPolygon 26

Graphical Objects GObject GLabel GRect GOval others GRect myrect = new GRect(50, 50, 350, 270); 27

Primitives vs. Objects Primitive Variable Types int double char boolean Object Variable Types GRect GOval GLine Scanner... Object variables: 1. Have UpperCamelCase types 2. You can call methods on them Uses dot syntax 3. Are constructed using new 28

Methods on Graphics Objects We manipulate graphics objects by calling methods on them: object.method(parameters); Receiver Message 29

Methods on Graphics Objects We manipulate graphics objects by calling methods on them: object.method(parameters); Who? What? What specifically? Example: rect.setcolor(color.red); 30

GObject Methods The following operations apply to all GObjects: object.setcolor(color) Sets the color of the object to the specified color constant. object.setlocation(x, y) Changes the location of the object to the point (x, y). object.move(dx, dy) Moves the object on the screen by adding dx and dy to its current coordinates. object.getwidth() Returns the width of the object object.getheight() Returns the height of the object Graphic courtesy of Eric Roberts. See the GObject documentation online for more methods. 31

Colors Specified as predefined Color constants: Color.NAME, where NAME is one of: BLACK BLUE CYAN DARK_GRAY GRAY GREEN LIGHT_GRAY MAGENTA ORANGE PINK RED WHITE YELLOW rect.setcolor(color.magenta); Or create one using Red-Green-Blue (RGB) values of 0-255 new Color(red, green, blue) Example: rect.setcolor(new Color(192, 128, 64)); 32

GRect new GRect(x, y, width, height); Creates a rectangle with the given width and height, whose upper-left corner is at (x, y) new GRect(width, height); Same as above, but defaults to (x, y) = (0, 0) 33

GOval new GOval(x, y, width, height); Creates an oval that fits inside a rectangle with the given width and height, and whose upper-left corner is at (x, y) new GOval(width, height); Same as above, but defaults to (x, y) = (0, 0) 34

GRect and GOval Methods shared by the GRect and GOval classes object.setfilled(fill) If fill is true, fills in the interior of the object; if false, shows only the outline. object.setfillcolor(color) Sets the color used to fill the interior, which can be different from the border. object.setsize(width, height) Sets the object s size to be the given width and height 35

GLine new GLine(x0, y0, x1, y1); Creates a line extending from (x0, y0) to (x1, y1) 36

GLabel new GLabel( your text here, x, y); Creates a label with the given text, whose baseline starts at (x, y). NOT positioned according to the top-left corner! new GLabel( your text here ); Same as above, but defaults to (x, y) = (0, 0) Ascent +x +y Hey, everyone! Height Baseline (x,y) Descent 37

GLabel Methods Methods specific to the GLabel class label.getdescent() Returns the height of the label below its baseline. label.getascent() Returns the height of the label above its baseline. label.setfont(font) Sets the font used to display the label as specified by the font string. The font is typically specified as a string in the form "family-style-size" family is the name of a font family style is either PLAIN, BOLD, ITALIC, or BOLDITALIC size is an integer indicating the point size 38

GImage new GImage( your filename here, x, y); Creates a an image displaying the given file, whose upperleft corner is at (x, y) new GImage( your filename here ); Same as above, but defaults to (x, y) = (0, 0) 39

GImage Methods object.setsize(width, height) Sets the object s size to be the given width and height 40

GraphicsProgram Methods GraphicsProgram contains these useful methods: Method add(gobj); add(gobj, x, y); getelementat(x, y) getelementcount() getwidth(), getheight() remove(gobj); removeall(); setcanvassize(w, h); setbackground(color); Description adds a graphical object to the window return the object at the given (x,y) position(s) return number of graphical objects onscreen return dimensions of window removes a graphical object from the window remove all graphical objects from window set size of drawing area set window's background color 41

Announcements Recap: File Reading GraphicsProgram Graphical Objects Practice: Car Plan For Today 42

Practice: Drawing with Loops The x,y,width,height expressions can use the loop counter variable: for (int i = 0; i < 10; i++) { add(new GOval(100 + 20 * i, 5 + 20 * i, 50, 50)); } // x y w h Nested loops can be used with graphics: for (int x = 1; x <= 4; x++) { for (int y = 1; y <= 9; y++) { add(new GLabel("Java", x * 40, y * 25)); } } 43

Practice: Drawing with Loops Q: What is the output of the following code? for (int i = 0; i < 10; i++) { add(new GRect(20 + 10 * i, 20 + 10 * i, 100-10 * i, 10)); } 1. 2. 3. 4. none (How would we modify the code above to produce each output?) 44

Practice: Car Write a graphical program named Car that draws a figure that looks (kind of) like a car. Red wheels at (20, 70) and (80, 70), size 20x20 Cyan windshield at (80, 40), size 30x20 Blue body at (10, 30), size 100x50 Yellow background 45

Announcements Recap: File Reading GraphicsProgram Graphical Objects Practice: Car Recap Next time: More Graphics + Animation 46