CS 106A, Lecture 12 More Graphics

Slides:



Advertisements
Similar presentations
CREATED BY : VIRAL M.KORADIYA. Anchor elements are defined by the element. The element accepts several attributes, but either the Name or HREF attribute.
Advertisements

Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Variables and Operators
Python Programming, 2/e1 CS177: Programming in Multimedia Objects Recitation Topic: Graphics Library.
Chapter 2 Programming by Example. A Holistic Perspective Three sections separated by blank lines. Program comment File: FileName.java
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Graphics Programming. In this class we will cover: Drawing lines, rectangles, and ellipses Copying an area Drawing an image.
Java Graphics Section 1 - Multi-File Graphics Programs Section 2 - The Coordinate System and Graphics Context g Section 3 - The Java Drawing and Painting.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS Lecture 7-1 A picture's worth a thousand words.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
CHAPTER 11 Tables. How Are Tables Used Data Display  Very tidy and very useful Better Text Alignment  Putting text in tables allows you to format indents.
Graphical Structures Eric Roberts CS 106A January 29, 2010.
Designing a Web Page with Tables. A text table: contains only text, evenly spaced on the Web page in rows and columns uses only standard word processing.
Copyright 2010 by Pearson Education Building Java Programs Graphics reading: Supplement 3G.
Tkinter Canvas.
Some Graphics CS303E: Elements of Computers and Programming.
Programming Abstractions Cynthia Lee CS106X. Inheritance Topics Inheritance  The basics › Example: Stanford GObject class  Polymorphism › Example: Expression.
Building Java Programs Graphics Reading: Supplement 3G.
Graphical Structures Eric Roberts CS 106A January 27, 2016.
Simple Java Eric Roberts CS 106A January 11, 2016.
Interactive Graphics Eric Roberts CS 106A January 25, 2016.
Control Statements Eric Roberts CS 106A January 15, 2010.
CS 115 Lecture 7 Graphics – coordinate systems, Text, Entry, aliases Taken from notes by Dr. Neil Moore.
Five-Minute Review 1.What steps do we distinguish in solving a problem by computer? 2.What are essential properties of an algorithm? 3.What is a definition.
CS 115 Lecture 6 Graphics Taken from notes by Dr. Neil Moore.
Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
CS 106A, Lecture 27 Final Exam Review 1
CSS.
Jerry Cain and Eric Roberts
Adapted from slides by Marty Stepp and Stuart Reges
Strings and Graphics Jerry Cain CS 106J April 12, 2017.
Inserting and Working with Images
Review.
Example: Card Game Create a class called “Card”
CS106A, Stanford University
Beyond Console Programs
Graphics Part I Taken from notes by Dr. Neil Moore
reading: Art & Science of Java,
Graphics Part I Taken from notes by Dr. Neil Moore
Basic Graphics Drawing Shapes 1.
CS 115 Lecture Graphics II – coordinate systems, Text, Entry, aliases
reading: Art & Science of Java, Ch. 9
CS 106A, Lecture 14 Events and Instance Variables
CS 106A, Lecture 22 More Classes
Chapter 2—Programming by Example
Building Java Programs
CS 115 Lecture Graphics II – coordinate systems, Text, Entry, aliases
Building Java Programs
SSEA Computer Science: Track A
CS106A, Stanford University
Graphics.
CS106A, Stanford University
Chapter 2 Graphics Programming with C++ and the Dark GDK Library
More programming with "Processing"
Graphics Part I Taken from notes by Dr. Neil Moore
Cascading Style Sheets™ (CSS)
Interactive Graphics Jerry Cain and Ryan Eberhardt CS 106AJ
slides courtesy of Eric Roberts
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 2—Programming by Example
Building Java Programs
Variables and Operators
Building Java Programs
Building Java Programs
Working with images and scenes
Presentation transcript:

CS 106A, Lecture 12 More Graphics reading: Art & Science of Java, 9.4

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

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

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

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. There are many more methods than we can show here (we show important ones) Common in software engineering to document code for others to use

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

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

Collage Model

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 These are all variables as you are used to! This just explains the different syntax

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 and more… Graphic courtesy of Eric Roberts

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 (e.g. “SansSerif”) 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

Recap Practice: Centering Graphics Program (?, ?) One common graphics challenge: how do we center things? 1st: horizontally 2nd: vertically

Recap Practice: Centering Graphics Program getWidth() W getWidth() / 2.0 W / 2.0 rectangle's x value = getWidth() / 2.0 – W / 2.0

Recap Practice: Centering Graphics Program getHeight() rectangle's y value = getHeight() / 2.0 – H / 2.0

Plan For Today Announcements Recap: Graphics GCompounds Getters Practice: Stoplights Practice: Checkerboard A few more graphics methods I want to show, that let you “get” information back

GCompound A 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); ... add(compound); E.g. you can make a GCompound to represent a car.

GCompound setBackground(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

Plan For Today Announcements Recap: Graphics GCompounds Getters Practice: Stoplights Practice: Checkerboard A few more graphics methods I want to show, that let you “get” information back

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 Description obj.getColor() the color used to color the shape outline obj.getFillColor() the color used to color the shape interior obj.getX() the left x-coordinate of the shape obj.getY() the top y-coordinate of the shape obj.getWidth() number of pixels wide the shape is obj.getHeight() number of pixels tall the shape is

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

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

Practice: Stoplights What information do we need in order to draw this? We only really need the width (or height), x, and y

Extra Practice: Line Art Write 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)

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

Practice: Checkerboard Write a graphical program named Checkerboard that draws a checkerboard pattern using GRects. Going to break down into milestones

Milestone 1

Milestone 2

Milestone 3

Milestone 3

Recap Announcements Recap: Graphics GCompounds Getters Practice: Stoplights Practice: Checkerboard Next time: Animation