CS 106A, Lecture 12 More Graphics - Stanford University

42
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. CS 106A, Lecture 12 More Graphics reading: Art & Science of Java , 9.4

Transcript of CS 106A, Lecture 12 More Graphics - Stanford University

Page 1: CS 106A, Lecture 12 More Graphics - Stanford University

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.

CS 106A, Lecture 12More Graphics

reading:Art & Science of Java, 9.4

Page 2: CS 106A, Lecture 12 More Graphics - Stanford University

2

GraphicsPrograms

AnimationEvents

Memory

HW4:Breakout

The River

of Java

You are here

Page 3: CS 106A, Lecture 12 More Graphics - Stanford University

3

Plan For Today•Announcements•Recap: Graphics•GCompounds•Getters•Practice: Checkerboard•Practice: Stoplights

Page 4: CS 106A, Lecture 12 More Graphics - Stanford University

4

Plan For Today•Announcements•Recap: Graphics•GCompounds•Getters•Practice: Checkerboard•Practice: Stoplights

Page 5: CS 106A, Lecture 12 More Graphics - Stanford University

5

Announcements: Docs• Click the "Stanford Library Docs" link in the 106A website sidebar.

– This site lists every kind of object in the Stanford libraries.– Click an object type on the left and see its behavior on the right.– These kinds of pages exist for Stanford libraries and standard Java.

Page 6: CS 106A, Lecture 12 More Graphics - Stanford University

6

Announcements: Midterm• Midterm is next Monday 7/23 from 7PM-9PM in Hewlett 200

• You will need your own laptop

– Email Annie right away if you need us to get you a loaner laptop

• Before the exam, you will need to download two things from the website’s midterm page (neither download is ready yet, but soon):

– A program called BlueBook, which you will use to take the exam

– An encrypted file that BlueBook will read to show you the exam

•You will get the decryption password during the exam

• See the midterm page for practice exams and study strategies

– All lectures through this Thursday are fair game for the exam

– The first practice is last summer’s midterm—it is hard!

– The second uses BlueBook but covers material we haven’t seen

Page 7: CS 106A, Lecture 12 More Graphics - Stanford University

7

Plan For Today•Announcements•Recap: Graphics•GCompounds•Getters•Practice: Checkerboard•Practice: Stoplights

Page 8: CS 106A, Lecture 12 More Graphics - Stanford University

8

The Graphics Canvas0,0

40,20

40,120

120,40

Page 9: CS 106A, Lecture 12 More Graphics - Stanford University

9

Collage Model

Page 10: CS 106A, Lecture 12 More Graphics - Stanford University

10

Graphical ObjectsGRect GOval GLine

GLabel GImage GArc

GRoundRect GPolygon

(x, y)

(x+w, y+h)

(x, y)

(x+w, y+h)

(x1, y1)

(x2, y2)

Hello there!

Page 11: CS 106A, Lecture 12 More Graphics - Stanford University

11

GObject

GRect GOval others…GLabel

Graphical Objects

Initialization syntax:

type name = new type(...);Called a constructor

Example:GRect rect = new GRect(50, 50, 350, 270);

Page 12: CS 106A, Lecture 12 More Graphics - Stanford University

12

Primitives vs. ObjectsPrimitive Variable Types Object Variable Types

intdoublecharboolean

GRectGOvalGLineScanner...

Object variables:1. Have UpperCamelCase types2. You can call methods on them• Uses “dot syntax”

3. Are constructed using new

Page 13: CS 106A, Lecture 12 More Graphics - Stanford University

13

Methods on Graphics ObjectsWe manipulate graphics objects by calling methods on them:

object.method(parameters);

Example:rect.setColor(Color.RED);

Who? What? What specifically?

Page 14: CS 106A, Lecture 12 More Graphics - Stanford University

14

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.

The following operations apply to all GObjects:

Graphic courtesy of Eric Roberts

GObject Methods

object.getWidth()Returns the width of the object

object.getHeight()Returns the height of the object

and more…

Page 15: CS 106A, Lecture 12 More Graphics - Stanford University

15

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

Page 16: CS 106A, Lecture 12 More Graphics - Stanford University

16

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

Page 17: CS 106A, Lecture 12 More Graphics - Stanford University

17

GRect and GOvalMethods shared by the GRect and GOval classesobject.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

Page 18: CS 106A, Lecture 12 More Graphics - Stanford University

18

GLinenew GLine(x0, y0, x1, y1);

– Creates a line extending from (x0, y0) to (x1, y1)

Page 19: CS 106A, Lecture 12 More Graphics - Stanford University

19

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

Page 20: CS 106A, Lecture 12 More Graphics - Stanford University

20

Methods specific to the GLabel class

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 (e.g. “SansSerif”)style is either PLAIN, BOLD, ITALIC, or BOLDITALICsize is an integer indicating the point size

GLabel Methods

label.getAscent()Returns the height of the label above its baseline.

label.getDescent()Returns the height of the label below its baseline.

Page 21: CS 106A, Lecture 12 More Graphics - Stanford University

21

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

Page 22: CS 106A, Lecture 12 More Graphics - Stanford University

22

GImage Methodsobject.setSize(width, height)

Sets the object’s size to be the given width and height

Page 23: CS 106A, Lecture 12 More Graphics - Stanford University

23

GraphicsProgramMethods• GraphicsProgram contains these useful methods:

Method Descriptionadd(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 onscreengetWidth(), getHeight() return dimensions of windowremove(gobj); removes a graphical object from the windowremoveAll(); remove all graphical objects from windowsetCanvasSize(w, h); set size of drawing areasetBackground(color); set window's background color

Page 24: CS 106A, Lecture 12 More Graphics - Stanford University

24

Recap Practice: Centering

Graphics Program

(?, ?)

Page 25: CS 106A, Lecture 12 More Graphics - Stanford University

25

Recap Practice: Centering

Graphics Program

getWidth()

W

getWidth() / 2.0

W / 2.0

rectangle's x value = getWidth() / 2.0 – W / 2.0

Page 26: CS 106A, Lecture 12 More Graphics - Stanford University

26

Recap Practice: Centering

Graphics Program

getHeight()

rectangle's y value = getHeight() / 2.0 – H / 2.0

Page 27: CS 106A, Lecture 12 More Graphics - Stanford University

27

Plan For Today•Announcements•Recap: Graphics•GCompounds•Getters•Practice: Checkerboard•Practice: Stoplights

Page 28: CS 106A, Lecture 12 More Graphics - Stanford University

28

GCompoundA GCompound contains other GObjects. It’s useful when you want to do one operation on multiple GObjects at the same time.

GCompound compound = new GCompound();compound.add(shape);compound.add(shape);...compound.add(shape);

add(compound);

– You can make a GCompound to represent a car.

Page 29: CS 106A, Lecture 12 More Graphics - Stanford University

29

GCompoundsetBackground(Color.YELLOW);GCompound car = new GCompound();

GRect body = new GRect(10, 30, 100, 50);body.setFilled(true);body.setFillColor(Color.BLUE);car.add(body);

GOval wheel1 = new GOval(20, 70, 20, 20);wheel1.setFilled(true);wheel1.setFillColor(Color.RED);car.add(wheel1);GOval wheel2 = new GOval(80, 70, 20, 20);wheel2.setFilled(true);wheel2.setFillColor(Color.RED);car.add(wheel2);GRect windshield = new GRect(80, 40, 30, 20);windshield.setFilled(true);windshield.setFillColor(Color.CYAN);car.add(windshield);add(car); // at 0,0! Where we want this “sub-canvas” to go

Page 30: CS 106A, Lecture 12 More Graphics - Stanford University

30

Plan For Today•Announcements•Recap: Graphics•GCompounds•Getters•Practice: Checkerboard•Practice: Stoplights

Page 31: CS 106A, Lecture 12 More Graphics - Stanford University

31

Graphics Program ”Getters”• Methods of graphical objects that return values:

– Example: Swapping the x/y coordinates of a shape:GRect rect = new GRect(...);...int rx = rect.getX();int ry = rect.getY();rect.setLocation(ry, rx);

Method Descriptionobj.getColor() the color used to color the shape outlineobj.getFillColor() the color used to color the shape interiorobj.getX() the left x-coordinate of the shapeobj.getY() the top y-coordinate of the shapeobj.getWidth() number of pixels wide the shape isobj.getHeight() number of pixels tall the shape is

Page 32: CS 106A, Lecture 12 More Graphics - Stanford University

32

Plan For Today•Announcements•Recap: Graphics•GCompounds•Getters•Practice: Checkerboard•Practice: Stoplights

Page 33: CS 106A, Lecture 12 More Graphics - Stanford University

33

Practice: CheckerboardWrite a graphical program named Checkerboard that drawsa checkerboard pattern using GRects.

Page 34: CS 106A, Lecture 12 More Graphics - Stanford University

34

Milestone 1

Page 35: CS 106A, Lecture 12 More Graphics - Stanford University

35

Milestone 2

Page 36: CS 106A, Lecture 12 More Graphics - Stanford University

36

Milestone 3

Page 37: CS 106A, Lecture 12 More Graphics - Stanford University

37

Milestone 3

Page 38: CS 106A, Lecture 12 More Graphics - Stanford University

38

Plan For Today•Announcements•Recap: Graphics•GCompounds•Getters•Practice: Checkerboard•Practice: Stoplights

Page 39: CS 106A, Lecture 12 More Graphics - Stanford University

39

Practice: StoplightsHow would you make a method for drawing stoplights of different locations and sizes?

Page 40: CS 106A, Lecture 12 More Graphics - Stanford University

40

Practice: Stoplights

What information do we need in order to draw this?

Page 41: CS 106A, Lecture 12 More Graphics - Stanford University

41

Recap•Announcements•Recap: Graphics•GCompounds•Getters•Practice: Stoplights•Practice: Checkerboard

Next time: Animation

Page 42: CS 106A, Lecture 12 More Graphics - Stanford University

42

Extra Practice: Line ArtWrite a graphical program LineArt that draws a series of lines (see lecture code for solution):• Outer square is at (10, 30) and size 200x200• each line is 10px apart in each dimension

coordinates of top-left lines:– (210, 30) to (10, 30)– (200, 30) to (10, 40)– (190, 30) to (10, 50)– ...– (20, 30) to (10, 220)

coordinates of bottom-right lines:– (210, 30) to (210, 230)– (210, 40) to (200, 230)– ...– (210, 220) to (20, 230)