Topic 8 Graphics. Margaret Hamilton

Similar documents
Topic 8 graphics. -mgrimes, Graphics problem report 134

Building Java Programs

Building Java Programs

Garfield AP CS. Graphics

Building Java Programs

Building Java Programs

Using Graphics. Building Java Programs Supplement 3G

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

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

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

Parameters. Repetitive figures. A solution? Parameterization. Declaring parameterized methods. Generalizing methods. Readings: 3.1

Building Java Programs

public static void main(string[] args) { GTest mywindow = new GTest(); // Title This program creates the following window and makes it visible:

TWO-DIMENSIONAL FIGURES

CS 112 Introduction to Programming

Admin. CS 112 Introduction to Programming. Recap: OOP. Math.random() Design. Static Math.random() method vs Random Objects

CS 106A, Lecture 11 Graphics

Admin. CS 112 Introduction to Programming. Counting Down: Code Puzzle. Counting Down: Code Puzzle

An Introduction to Processing

CIS 162 Project 1 Business Card Section 04 (Kurmas)

CS 112 Introduction to Programming

Building Java Programs

CSC System Development with Java Introduction to Java Applets Budditha Hettige

Week 3. parameters, return, math, graphics

Repetition. We might start out with the following program that draws only the left most object.

CISC 1600 Lecture 3.1 Introduction to Processing

CS Problem Solving and Object-Oriented Programming

Unit 3. parameters and graphics

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

Using the API: Introductory Graphics Java Programming 1 Lesson 8

AWT COLOR CLASS. Introduction. Class declaration. Field

Dr. Hikmat A. M. AbdelJaber

CS 305j Midterm 1 Fall 2006

CS 312 Midterm 1 Spring 2013

CS 312 Midterm 1 Spring 2013

Appendix F: Java Graphics

Topic 6 loops, figures, constants

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

Assignment 2. Application Development

CS 305j Midterm 1 Fall 2007

Appendix F: Java Graphics

9. APPLETS AND APPLICATIONS

The FacebookPerson Example. Dr. Xiaolin Hu CSC 2310, Spring 2015

CIS 110 Introduction To Computer Programming. October 5th, 2011 Exam 1. Answer key for review problems

Java Applet Basics. Life cycle of an applet:

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

5. Introduction to Procedures

G51PRG: Introduction to Programming Second semester Applets and graphics

2IS45 Programming

2D Graphics. Shape Models, Drawing, Selection. CS d Graphics 1

Introduction to Computer Science I

CS 201 Advanced Object-Oriented Programming Lab 1 - Improving Your Image Due: Feb. 3/4, 11:30 PM

@Override public void start(stage primarystage) throws Exception { Group root = new Group(); Scene scene = new Scene(root);

Topic 27 classes and objects, state and behavior

Chapter 3 - Introduction to Java Applets

OBJECT ORIENTED PROGRAMMING. Course 8 Loredana STANCIU Room B613

AP CS Unit 12: Drawing and Mouse Events

AP CS Java Syntax Summary: (version 3)

CS 100: Programming Assignment P1

CS 112 Introduction to Programming

CS 112 Introduction to Programming

2D Graphics. Shape Models, Drawing, Selection. CS d Graphics 1

Exam: Applet, Graphics, Events: Mouse, Key, and Focus


CMPSCI 119 LAB #2 Greebles / Anime Eyes Professor William T. Verts

CSC 101: Lab Manual#11 Programming Turtle Graphics in Python Lab due date: 5:00pm, day after lab session

Building Java Programs

Nested Loops ***** ***** ***** ***** ***** We know we can print out one line of this square as follows: System.out.

1.00 Lecture 14. Lecture Preview

11/7/12. Discussion of Roulette Assignment. Objectives. Compiler s Names of Classes. GUI Review. Window Events

Creating objects TOPIC 3 INTRODUCTION TO PROGRAMMING. Making things to program with.

Chapter 1 Introducing Draw

Exercise NMCGJ: Animated Graphics

Advanced Internet Programming CSY3020

CISC 1115 (Science Section) Brooklyn College Professor Langsam. Assignment #5

Programming by Delegation

AP CS Java Syntax Summary: (version 4)

Java Programming Lecture 6

Class 14: Introduction to the Swing Toolkit

Warping & Blending AP

Graflog User s guide

Topic 11 Scanner object, conditional execution

1B1a Programming I Getting Started

To get a copy of this image you right click on the image with your mouse and you will get a menu. Scroll down the menu and select "Save Image As".

Outline. Turtles. Creating objects. Turtles. Turtles in Java 1/27/2011 TOPIC 3 INTRODUCTION TO PROGRAMMING. Making things to program with.

CS 305j Midterm 1 Fall 2008

Building Java Programs

CMPSCI 120 Extra Credit #1 Professor William T. Verts

Graphical User Interfaces

ITEC185. Introduction to Digital Media

Topic 11 Scanner object, conditional execution

TOPIC 8 MORE ON WHILE LOOPS

Topic 4 Expressions and variables

COMP1007 Principles of Programming

Admin. CS 112 Introduction to Programming. Recap. Example: Nested Loop. Example: Rewrite

Graphics Applets. By Mr. Dave Clausen

Chapter 12 GUI Basics

GUI Output. Adapted from slides by Michelle Strout with some slides from Rick Mercer. CSc 210

Chapter 1 Introduction to Java

Quick Guide for Photoshop CC Basics April 2016 Training:

Transcription:

Topic 8 Graphics When the computer crashed during the execution of your program, there was no hiding. Lights would be flashing, bells would be ringing and everyone would come running to find out whose program was doing something bad to the system. Margaret Hamilton Director of Apollo Flight Computer Programming By Daphne Weld Nichols, Photographer - Photograph of Margaret Hamilton taken by photographer Daphne Weld Nichols, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=12205173 Based on slides bu Marty Stepp and Stuart Reges from http://www.buildingjavaprograms.com/

DrawingPanel To create an empty window: DrawingPanel panel = new DrawingPanel(300, 200); 300 Width Height 200

Graphics DrawingPanel panel = new DrawingPanel(300, 200); Graphics g = panel.getgraphics(); g.fillrect(10, 30, 60, 35); Draws a rectangle, but. huh?

Graphics DrawingPanel panel = new DrawingPanel(300, 200); Graphics g = panel.getgraphics(); g.fillrect(10, 30, 60, 35); Top left corner is (0, 0)

Graphics DrawingPanel panel = new DrawingPanel(300, 200); Graphics g = panel.getgraphics(); g.fillrect(10, 30, 60, 35); (0, 0) Top left corner of rectangle is

Graphics DrawingPanel panel = new DrawingPanel(300, 200); Graphics g = panel.getgraphics(); g.fillrect(10, 30, 60, 35); (0, 0) Top left corner of 10 pixels to the right, and rectangle is

Graphics DrawingPanel panel = new DrawingPanel(300, 200); Graphics g = panel.getgraphics(); g.fillrect(10, 30, 60, 35); (0, 0) 30 pixels down. Top left corner of 10 pixels to the right, and rectangle is

Graphics DrawingPanel panel = new DrawingPanel(300, 200); Graphics g = panel.getgraphics(); g.fillrect(10, 30, 60, 35); (0, 0) 30 10 60 pixels wide

Graphics DrawingPanel panel = new DrawingPanel(300, 200); Graphics g = panel.getgraphics(); g.fillrect(10, 30, 60, 35); (0, 0) 30 35 pixels high 10 60

Coordinate system Each (x, y) position is a pixel ("picture element"). Position (0, 0) is at the window's top-left corner. x increases rightward and the y increases downward. The rectangle from (0, 0) to (200, 100) looks like this: (0, 0) x+ y+ (200, 100)

Graphics DrawingPanel panel = new DrawingPanel(300, 200); Graphics g = panel.getgraphics(); g.fillrect(10, 30, 60, 35); g.filloval(80, 40, 50, 70);

Java class libraries, import Packages: code that Java developers already wrote To use a package, add an import line to your Java code: import ; For example, Graphics belongs to a package named java.awt To use Graphics, put this at the top of your program: import java.awt.graphics;

Graphics methods Method name Description g.drawline(x1, y1, x2, y2); line between points (x1, y1), (x2, y2) g.drawoval(x, y, width, height); g.drawrect(x, y, width, height); outline largest oval that fits in a box of size width * height with top-left at (x, y) outline of rectangle of size width * height with top-left at (x, y) g.drawstring(text, x, y); text with bottom-left at (x, y) g.filloval(x, y, width, height); g.fillrect(x, y, width, height); g.setcolor(color); fill largest oval that fits in a box of size width * height with top-left at (x, y) fill rectangle of size width * height with top-left at (x, y) set Graphics to paint any following shapes in the given color

Clicker Question How many rectangles appear on the DrawingPanel when the following code is run? DrawingPanel p1 = new DrawingPanel(200, 200); Graphics gr = new Graphics(); for(int i = 0; i < 5; i++) { gr.drawrect(i * 25, i * 20, 20, 50); } A. 5 B. 6 C. 20 D. None due to syntax error E. None due to runtime error

Using colors g.setcolor(color.black); g.fillrect(10, 30, 100, 50); g.drawline(20, 0, 10, 30); g.setcolor(color.red); g.filloval(60, 40, 40, 70); Color brown = new Color(192, 128, 64); panel.setbackground(brown);

Color Specified as predefined Color class constants: Color.CONSTANT_NAME where CONSTANT_NAME is one of: BLACK, BLUE, CYAN, DARK_GRAY, GRAY, GREEN, LIGHT_GRAY, MAGENTA, ORANGE, PINK, RED, WHITE, YELLOW Or create one using Red-Green-Blue (RGB) values of 0-255 Color name = new Color(red, green, blue); Example: Color brown = new Color(192, 128, 64); Color burntorange = new Color(191, 87, 0); List of Colors

Clicker Question What named color is closest to the Color object created by this code? Color mc = new Color(255, 255, 255); A. Black B. Brown C.Gray D.Orange E. White

Outlined shapes To draw a colored shape with an outline, first fill it, then draw the same shape in the outline color. import java.awt.graphics; // so I can use Graphics import java.awt.color; public class OutlineExample { public static void main(string[] args) { DrawingPanel panel = new DrawingPanel(150, 70); Graphics g = panel.getgraphics(); } } // inner red fill g.setcolor(color.red); g.fillrect(20, 10, 100, 50); // black outline g.setcolor(color.black); g.drawrect(20, 10, 100, 50);

Superimposing shapes When 2 shapes occupy the same pixels, the last drawn "wins." import java.awt.graphics; import java.awt.color; public class Car { public static void main(string[] args) { DrawingPanel panel = new DrawingPanel(200, 100); panel.setbackground(color.light_gray); Graphics g = panel.getgraphics(); } } g.setcolor(color.black); g.fillrect(10, 30, 100, 50); g.setcolor(color.red); g.filloval(20, 70, 20, 20); g.filloval(80, 70, 20, 20); g.setcolor(color.cyan); g.fillrect(80, 40, 30, 20);

Drawing with loops The x,y,w,h expressions can use the loop counter variable: panel.setbackground(color.yellow); g.setcolor(color.red); for (int i = 1; i <= 10; i++) { // x y w h g.filloval(100 + 20 * i, 5 + 20 * i, 50, 50); } Nested loops can be used with graphics: g.setcolor(color.blue); for (int x = 1; x <= 4; x++) { for (int y = 1; y <= 9; y++) { g.drawstring("java", x * 40, y * 25); } }