reading: Art & Science of Java,

Slides:



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

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.
Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
Chapter 2 Programming by Example. A Holistic Perspective Three sections separated by blank lines. Program comment File: FileName.java
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
Copyright 2008 by Pearson Education Building Java Programs Graphics Reading: Supplement 3G.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
2D Graphics in Java COMP53 Nov 14, Applets and Applications Java applications are stand-alone programs – start execution with main() – runs in JVM.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
1 Graphical objects We will draw graphics in Java using 3 kinds of objects: DrawingPanel : A window on the screen. Not part of Java; provided by the authors.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
Graphical Structures Eric Roberts CS 106A January 29, 2010.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Copyright 2010 by Pearson Education Building Java Programs Graphics reading: Supplement 3G.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
BUILDING JAVA PROGRAMS CHAPTER 6 File Processing.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Graphic Basics in C ATS 315. The Graphics Window Will look something like this.
1 Building Java Programs Supplement 3G: Graphics These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold,
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
CSC 1010 Programming for All Lecture 7 Input, Output & Graphics.
Building Java Programs Graphics Reading: Supplement 3G.
Introduction to Graphics. Graphical objects To draw pictures, we will use three classes of objects: –DrawingPanel : A window on the screen. –Graphics.
Graphical Structures Eric Roberts CS 106A January 27, 2016.
Simple Java Eric Roberts CS 106A January 11, 2016.
Control Statements Eric Roberts CS 106A January 15, 2010.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
CS 106A, Lecture 27 Final Exam Review 1
Pixels, Colors and Shapes
Adapted from slides by Marty Stepp and Stuart Reges
Chapter 3 Syntax, Errors, and Debugging
Review.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Java Programming: Guided Learning with Early Objects
Beyond Console Programs
CSc 110, Spring 2017 Lecture 6: Parameters (cont.) and Graphics
Basic Graphics Drawing Shapes 1.
reading: Art & Science of Java, Ch. 9
CS 106A, Lecture 12 More Graphics
CS 106A, Lecture 14 Events and Instance Variables
CS 106A, Lecture 22 More Classes
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Chapter 2—Programming by Example
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.
Building Java Programs
CSc 110, Spring 2018 Lecture 9: Parameters, Graphics and Random
Building Java Programs
SSEA Computer Science: Track A
CS 106A, Lecture 10 File Reading
CS106A, Stanford University
Graphics.
CSE 142 Lecture Notes Graphics with DrawingPanel Chapter 3
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 2—Programming by Example
Building Java Programs
Building Java Programs
Graphics Reading: Supplement 3G
Building Java Programs
Presentation transcript:

reading: Art & Science of Java, 9.1-9.3 CS 106A, Lecture 11 Graphics reading: Art & Science of Java, 9.1-9.3

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

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

File Reading Overview Make a Scanner to open a file to read Scanner input = new Scanner(new File("data.txt")); Use Scanner methods such as nextLine or next to read in the file, usually in a loop Scanner operations on files are ”dangerous”, so we need to use a try/catch block Close the Scanner when you are done – input.close()

File Reading Overview Make a Scanner to open a file to read Scanner input = new Scanner(new File("data.txt")); Use Scanner methods such as nextLine or next to read in the file, usually in a loop Scanner operations on files are ”dangerous”, so we need to use a try/catch block Close the Scanner when you are done – input.close()

Scanner methods Method Description sc.nextLine() reads and returns a one-line String from the file sc.next() reads and returns a one-word String from the file sc.nextInt() reads and returns an int from the file sc.nextDouble() reads and returns a double from the file sc.hasNextLine() returns true if there are any more lines sc.hasNext() returns true if there are any more tokens sc.hasNextInt() returns true if there is a next token and it's an int sc.hasNextDouble() returns true if there is a next token and it's a double sc.close(); should be called when done reading the file

File Reading Overview Make a Scanner to open a file to read Scanner input = new Scanner(new File("data.txt")); Use Scanner methods such as nextLine or next to read in the file, usually in a loop Scanner operations on files are ”dangerous”, so we need to use a try/catch block Close the Scanner when you are done – input.close()

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. Scanner input = new Scanner(new File("data.txt")); ... } catch (IOException ex) { println("Error reading the file: " + ex); IOException

Try/Catch 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")); while (input.hasNextLine()) { String line = input.nextLine(); println(line); } } catch (FileNotFoundException ex) { println("Error reading the file: " + ex); If something fails up here…

… we immediately jump down here. Try/Catch 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")); while (input.hasNextLine()) { String line = input.nextLine(); println(line); } } catch (FileNotFoundException ex) { println("Error reading the file: " + ex); If something fails up here… … we immediately jump down here.

Mixing lines and tokens // Counts the words on each line of a file Scanner input = new Scanner(new File("input.txt")); while (input.hasNextLine()) { Scanner tokens = new Scanner(input.nextLine()); // process the contents of this line int count = 0; while (tokens.hasNext()) { String word = tokens.next(); count++; } println("Line has " + count + " words"); ... Input file input.txt : Output to console: The quick brown fox jumps over the lazy dog. Line has 6 words Line has 3 words

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

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

Java Program Karel Program Console Program Graphics Program SuperKarel Program

Graphics Programs

Our First GraphicsProgram

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); }

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);

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);

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);

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);

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

Collage Model

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

Graphical Objects GRect GOval GLine (x, y) (x, y) (x1, y1) GLabel GImage GArc GRoundRect GPolygon (x, y) (x+w, y+h) (x, y) (x+w, y+h) (x1, y1) (x2, y2) Hello there!

Graphical Objects GRect GOval GLine (x, y) (x, y) (x1, y1) GLabel GImage GArc GRoundRect GPolygon (x, y) (x+w, y+h) (x, y) (x+w, y+h) (x1, y1) (x2, y2) Hello there!

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

Primitives vs. Objects Primitive Variable Types Object Variable Types int double char boolean GRect GOval GLine Scanner ... Object variables: Have upper camel case types You can call methods on them Are constructed using new

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

Methods on Graphics Objects We manipulate graphics objects by calling methods on them: object.method(parameters); Example: rect.setColor(Color.RED); Who? What? What specifically?

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

Colors Specified as predefined Color constants: Color.NAME , where NAME is one of: 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)); BLACK BLUE CYAN DARK_GRAY GRAY GREEN LIGHT_GRAY MAGENTA ORANGE PINK RED WHITE YELLOW

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)

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)

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

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

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)

GLabel Methods Methods specific to the GLabel class label.setFont( font) Sets the font used to display the label as specified by the font string. label.getDescent() Returns the height of the label below its baseline. label.getAscent() Returns the height of the label above its baseline. 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 Graphic courtesy of Eric Roberts

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

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

GraphicsProgram Methods GraphicsProgram contains these useful methods: Method Description add(gobj); add(gobj, x, y); adds a graphical object to the window getElementAt(x, y) return the object at the given (x,y) position(s) getElementCount() return number of graphical objects onscreen getWidth(), getHeight() return dimensions of window remove(gobj); removes a graphical object from the window removeAll(); remove all graphical objects from window setCanvasSize(w, h); set size of drawing area setBackground(color); set window's background color

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

Practice: Drawing w/ Loops The x,y,w,h 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)); }

Practice: Drawing w/ 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)); } A. B. C. D. none (How would we modify the code above to produce each output?) answer: B

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

Car Solution // When 2 shapes occupy the same pixels, the last one drawn "wins" public class Car extends GraphicsProgram { public void run() { setBackground(Color.YELLOW); GRect body = new GRect(10, 30, 100, 50); body.setFilled(true); body.setFillColor(Color.BLUE); add(body); GOval wheel1 = new GOval(20, 70, 20, 20); wheel1.setFilled(true); wheel1.setFillColor(Color.RED); add(wheel1); GOval wheel2 = new GOval(80, 70, 20, 20); wheel2.setFilled(true); wheel2.setFillColor(Color.RED); add(wheel2); GRect windshield = new GRect(80, 40, 30, 20); windshield.setFilled(true); windshield.setFillColor(Color.CYAN); add(windshield); } Can’t add same object more than once – will just move it

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