SSEA Computer Science: Track A

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.
Chapter 2—Programming by Example The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Programming by Example C H A P T E R 2.
BGI graphics library And Visual Studio.
Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
PHY-102 SAPIntroductory GraphicsSlide 1 Introductory Graphics In this section we will learn how about how to draw graphics on the screen in Java:  Drawing.
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
Chapter 5 Programming Graphics. Chapter Goals To be able to write simple applications To display graphical shapes such as lines and ellipses To use colors.
TOPIC 5 INTRODUCTION TO PICTURES 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
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.
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.
Four simple expressions in meta. Data objects Pieces of data in a computer are called objects Today, we’ll talk about four kinds of objects Numbers Pictures.
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.
Graphics. Graphics Features in Java Topics to be covered Topics to be covered Graphics Basics Graphics Basics Coordinate System Coordinate System Graphics,
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
Agenda For Feb Finish up Unit 3 Exercises on page Unit 4 Exercises on page 33. Question #2 3. Create a Happy Face Java Applet (due next class).
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
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.
Graphics & Java 2D Drawing Two Dimensional Shapes Controlling Fonts Controlling Colors.
Graphic Basics in C ATS 315. The Graphics Window Will look something like this.
Tkinter Canvas.
1 Building Java Programs Supplement 3G: Graphics These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold,
Some Graphics CS303E: Elements of Computers and Programming.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Building Java Programs Graphics Reading: Supplement 3G.
CS305j Introduction to Computing Simple Graphics 1 Topic 11 Simple Graphics "What makes the situation worse is that the highest level CS course I've ever.
Introduction to Graphics. Graphical objects To draw pictures, we will use three classes of objects: –DrawingPanel : A window on the screen. –Graphics.
Simple Java Eric Roberts CS 106A January 11, 2016.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
CompSci 4 Java 4 Apr 14, 2009 Prof. Susan Rodger.
Adapted from slides by Marty Stepp and Stuart Reges
Chapter 11—Arrays and ArrayLists
Review.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Beyond Console Programs
Microsoft® Small Basic
SSEA Computer Science: CS106A
reading: Art & Science of Java,
Graphics Part I Taken from notes by Dr. Neil Moore
Basic Graphics Drawing Shapes 1.
CS 106A, Lecture 12 More Graphics
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
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
Graphics.
Graphics Part I Taken from notes by Dr. Neil Moore
CSE 142 Lecture Notes Graphics with DrawingPanel Chapter 3
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Graphics Reading: Supplement 3G
Building Java Programs
Presentation transcript:

SSEA Computer Science: Track A Dr. Cynthia Lee Lecturer in Computer Science Stanford

Topics for today Java Graphics!

Java Graphics

Working with Graphics Mention what those parameters mean later on. We will manipulate graphics on-screen by creating graphics objects and manipulating their properties. To create a graphics object, we need to declare a variable to hold that object, and actually create the object using the new keyword. For example: GLabel label = new GLabel("Hi!", 0, 0); Mention what those parameters mean later on.

Sending Messages (calling methods) You can manipulate graphics objects by calling methods on those objects. To call a method on an object, use the syntax object.method(parameters) For example: label.setFont("Comic Sans-32"); label.setColor(Color.ORANGE);

Sending Messages (calling methods) You can manipulate graphics objects by calling methods on those objects. To call a method on an object, use the syntax object.method(parameters) For example: label.setFont("Comic Sans-32"); label.setColor(Color.ORANGE); What specifically? Who? What?

Graphics Coordinates hello, world Graphics objects are positioned by specifying an x and y coordinate x increases left-to-right, y increases top-to-bottom x and y should be specified in pixels For a GLabel, the x and y coordinates give the start of the baseline where the text is drawn +x HelloProgram Explain the history of the coordinate system. hello, world (0, 0) getWidth() +y getHeight() Graphic courtesy of Eric Roberts 7

Drawing Geometrical Objects Creating graphics objects new GRect( x, y, width, height) Creates a rectangle whose upper left corner is at (x, y) of the specified size +x Graphics Program (x, y) (x + width, y + height) +y Graphic courtesy of Eric Roberts 8

Drawing Geometrical Objects Creating graphics objects new GRect( x, y, width, height) Creates a rectangle whose upper left corner is at (x, y) of the specified size new GOval( x, y, width, height) Creates an oval that fits inside the rectangle with the same dimensions. +x Graphics Program (x, y) (x + width, y + height) +y Graphic courtesy of Eric Roberts 9

Drawing Geometrical Objects Creating graphics objects new GRect( x, y, width, height) Creates a rectangle whose upper left corner is at (x, y) of the specified size new GOval( x, y, width, height) Creates an oval that fits inside the rectangle with the same dimensions. new GLine( x0, y0, x1, y1) Creates a line extending from (x0, y0) to (x1, y1). +x Graphics Program (x0, y0) (x1, y1) +y Graphic courtesy of Eric Roberts 10

Drawing Geometrical Objects Creating graphics objects new GRect( x, y, width, height) Creates a rectangle whose upper left corner is at (x, y) of the specified size new GOval( x, y, width, height) Creates an oval that fits inside the rectangle with the same dimensions. new GLine( x0, y0, x1, y1) Creates a line extending from (x0, y0) to (x1, y1). Methods shared by GRect and GOval object.setFilled( fill) Go and run BeautifulCollage to show off these different methods. Start off with unfilled objects and show what setFilled and setFillColor do. In particular, show how calling setColor and then setFilled will fill the whole object with one color, whereas setColor and setFillColor will change the perimeter and inside. Don't overlap the objects yet. 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. Graphic courtesy of Eric Roberts 11

The Collage Model

Size of the Graphics Window Methods provided by GraphicsProgram getWidth() Returns the width of the graphics window itself. getHeight() Returns the height of the graphics window itself. Like println, readInt, and readDouble, you don't need to prefix these methods with the object. notation. Go to HugeEllipse and show it off. Based on slides by Eric Roberts 13

Making This Circle

Making This Circle

Making This Circle 100 pixels

Making This Circle 100 pixels Where is this point?

Making This Circle getWidth() pixels 100 pixels Where is this point?

Making This Circle double x = getWidth() - 100; getWidth() pixels Where is this point?

Making This Circle double x = getWidth() - 100; 100 pixels

Making This Circle double x = getWidth() - 100; getHeight() pixels

Making This Circle double x = getWidth() - 100; double y = getHeight() - 100; getHeight() pixels 100 pixels

Making This Circle double x = getWidth() - 100; double y = getHeight() - 100; 100 pixels

Making This Circle double x = getWidth() - 100; double y = getHeight() - 100; GOval circle = new GOval(x, y, 100, 100); 100 pixels

Making This Circle double x = getWidth() - 100; double y = getHeight() - 100; GOval circle = new GOval(x, y, 100, 100);

Making This Circle double x = getWidth() - 100; double y = getHeight() - 100; GOval circle = new GOval(x, y, 100, 100); circle.setFilled(true); circle.setColor(Color.BLUE);

Making This Circle double x = getWidth() - 100; double y = getHeight() - 100; GOval circle = new GOval(x, y, 100, 100); circle.setFilled(true); circle.setColor(Color.BLUE); add(circle); Go and run FeelingCornered

Colors BLACK BLUE CYAN DARK_GRAY GRAY GREEN LIGHT_GRAY MAGENTA ORANGE Specified as predefined Color constants: Color.NAME , where NAME is one of: r.setColor(Color.MAGENTA); Or create one using Red-Green-Blue (RGB) values of 0-255 new Color(red_amount, green_amount, blue_amount) Example: r.setColor(new Color(192, 128, 64)); BLACK BLUE CYAN DARK_GRAY GRAY GREEN LIGHT_GRAY MAGENTA ORANGE RED WHITE YELLOW PINK

GraphicsProgram methods The GraphicsProgram contains these useful methods: public class Hello extends GraphicsProgram { public void run() { setTitle("My cool program"); add(new GRect(getWidth() - 50, 10, getWidth(), 40); ... Method Description add(shape); displays the given graphical shape in the window getHeight() the height of the graphical window, in pixels getWidth() the width of the graphical window, in pixels pause(ms); halts for the given # of milliseconds remove(shape); removes the given shape from the window setSize(w, h); sets the console window's onscreen size setTitle("text"); sets the title bar text

Learning more Google "Stanford Java Lib" (or go toURL below). 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. http://cs.stanford.edu/people/eroberts/jtf/javadoc/student/

Extra slides

Graphics exercise Write a program that draws a series of lines as follows: 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)

Exercise solution import acm.graphics.*; import acm.program.*; public class Football extends GraphicsProgram { public void run() { add(new GRect(10, 30, 200, 200)); // top-left lines for (int i = 0; i < 20; i++) { add(new GLine(210 - i*10, 30, 10, 30 + i*10)); } // bottom-right lines add(new GLine(210, 30 + i*10, 210 - i*10, 230));