Building Java Programs

Similar documents
Green = 0,255,0 (Target Color for E.L. Gray Construction) CIELAB RGB Simulation Result for E.L. Gray Match (43,215,35) Equal Luminance Gray for Green

Java applets. SwIG Jing He

Photoshop- Image Editing

Interactive Programs and Graphics in Java

Forest Stewardship Council

Pantone Matching System Color Chart PMS Colors Used For Printing

The following four software tools are needed to learn to program in Java:

Building Java Programs

Part 1 Foundations of object orientation

LAB4 Making Classes and Objects

COOL ART WITH MATH & SCIENCE OPTICAL ILLUSIONS CREATIVE ACTIVITIES THAT MAKE MATH & SCIENCE FUN FOR KIDS! A NDERS HANSON AND ELISSA MANN

Line-based file processing

Graphics Module Reference

Some of the Choices. If you want to work on your own PC with a C++ compiler, rather than being logged in remotely to the PSU systems

SMART board 101. SMART board 101 Training

Introduction to Java Applets (Deitel chapter 3)

CMPT 183 Foundations of Computer Science I

PANTONE Solid to Process

Token vs Line Based Processing

Construction of classes with classes

DIA Creating Charts and Diagrams

Laser Cutter User Manual

CIS 467/602-01: Data Visualization

Lesson 10: Video-Out Interface

CS170 Lab 11 Abstract Data Types & Objects

AwanaGames Circle. Official. Diagram 1. Key to diagram: Pin & Striped Bean Bag C Diagonal or Starting Line. B Scoring Pins, or Center.

How To Color Print

Animations in DrRacket

Rapid Android Development

LESSON 7: IMPORTING AND VECTORIZING A BITMAP IMAGE

Create A Collage Of Warped Photos

MAKE YOUR FIRST A-MAZE-ING GAME IN GAME MAKER 7

Turtle Power. Introduction: Python. In this project, you ll learn how to use a turtle to draw awesome shapes and patterns. Activity Checklist

Introducing Variance into the Java Programming Language DRAFT

Lesson 26: Reflection & Mirror Diagrams

PaintPot. Figure 2-1. The PaintPot app

Hypercosm. Studio.

Week 2 Practical Objects and Turtles

Fireworks CS4 Tutorial Part 1: Intro

Essentials of the Java Programming Language

Character LCDs. Created by Ladyada. Last updated on :45:29 PM EDT

1. Three-Color Light. Introduction to Three-Color Light. Chapter 1. Adding Color Pigments. Difference Between Pigments and Light. Adding Color Light

HOW TO PRINT YOUR DIGITAL SCRAPBOOK

How to Create a Fun Circus Tent Icon. Final Image Preview. Tutorial Details. Step 1. By: Andrei Marius

Tabla de conversión Pantone a NCS (Natural Color System)

Step 1: Setting up the Document/Poster

Graphics Module Reference

6.1. Example: A Tip Calculator 6-1

Adobe Illustrator CS5 Part 1: Introduction to Illustrator

Juce Programming Tutorial. Table of Contents

Recipes4Success. Animate a Rocket Ship. Frames 6 - Drawing Tools

So you say you want something printed...

TUTORIAL 4 Building a Navigation Bar with Fireworks

Test Driven Development

History OOP languages Year Language 1967 Simula Smalltalk

Section 6 Spring 2013

Getting Started in Tinkercad

SE 450 Object-Oriented Software Development. Requirements. Topics. Textbooks. Prerequisite: CSC 416

How to Make the Most of Excel Spreadsheets

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Introduction to Object-Oriented Programming

CMSC 427 Computer Graphics Programming Assignment 1: Introduction to OpenGL and C++ Due Date: 11:59 PM, Sept. 15, 2015

Vim, Emacs, and JUnit Testing. Audience: Students in CS 331 Written by: Kathleen Lockhart, CS Tutor

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

WebSite Tonight. Getting Started Guide. Getting Your Personal Website Online in One Night. WebSite Tonight // A Wedding Website

Overview of the Adobe Flash Professional CS6 workspace

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Adjusting Digitial Camera Resolution

Visualization of 2D Domains

Chapter 7. Functions and onto. 7.1 Functions

Graphic Design Basics. Shannon B. Neely. Pacific Northwest National Laboratory Graphics and Multimedia Design Group

Customizing your Blackboard Course

TEACHER S GUIDE TO RUSH HOUR

Tips for optimizing your publications for commercial printing

Color quality guide. Quality menu. Color quality guide. Page 1 of 6

Creative Specifications for Online Ads

Using Image J to Measure the Brightness of Stars (Written by Do H. Kim)

Using Kid Pix Deluxe 3 (Windows)

D06 PROGRAMMING with JAVA. Ch3 Implementing Classes

IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account).

UNIVERSITY OF LONDON GOLDSMITHS COLLEGE. B. Sc. Examination Sample CREATIVE COMPUTING. IS52020A (CC227) Creative Computing 2.

Make your own Temple Run game

If you know exactly how you want your business forms to look and don t mind

Kristen Kachurek. Circumference, Perimeter, and Area Grades Day lesson plan. Technology and Manipulatives used:

Essentials of the Java(TM) Programming Language, Part 1

There are a number of ways to use the Trapeze Networks logo. Here are the guidelines to use that will help strengthen our logo awareness:

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Fachbereich Informatik und Elektrotechnik Java Applets. Programming in Java. Java Applets. Programming in Java, Helmut Dispert

Organizing image files in Lightroom part 2

Animazione in Java. Animazione in Java. Problemi che possono nascere, e loro soluzione. Marco Ronchetti Lezione 1

ICE: HTML, CSS, and Validation

Paper 1. Calculator not allowed. Mathematics test. First name. Last name. School. Remember KEY STAGE 3 TIER 4 6

PANTONE Coated Color Reference

Java CPD (I) Frans Coenen Department of Computer Science

How To Program In Java (Ipt) With A Bean And An Animated Object In A Powerpoint (For A Powerbook)

Guidelines for Effective Creative

Laser cutter setup instructions:

Google SketchUp Design Exercise 3

Transcription:

Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2

Code Libraries For homework 3 (and others), we ll use a Graphics Library written by the textbook authors Library: Code written to make it easier to write many programs Graphics library: Provide features like draw a black oval Library takes care of all the drawing details Library is useful for lots of different programs Feels like Java has all these features, but it s really just methods and objects defined by the library Steps for using a library 1. What set up do I have to do to use the library? 2. What are the basic features of the library? 3. What are the patterns for making the features useful? 2

1. Set-up The library is in DrawingPanel.java On course web-site Must be in the same directory as your program Your program must have import java.awt.*; in your file before public class Otherwise things the library gives you won t be defined and your program won t compile These things are kinds of objects (classes) defined in Java s package called java.awt import says you want these things to be visible to your program 3

2. Basics, part 1 A complete program using the library to draw 2 ovals: import java.awt.*; public class MyFirstDrawing { public static void main(string[] args) { DrawingPanel panel = new DrawingPanel(300,200); g.setcolor(color.black); g.drawoval(100,100,50,50); g.drawoval(125,100,75,75); 4

Line by line DrawingPanel panel = new DrawingPanel(300,200); 1. Create a new DrawingPanel A canvas to draw things on Make it 300 pixels wide and 200 pixels high These are parameters to the DrawingPanel constructor 2. Store a reference to this new thing in a variable panel The library made DrawingPanel a type (like int, String) Otherwise nothing novel about this part, just declaration and initialization 5

Line by line DrawingPanel panel = new DrawingPanel(300,200); 1. Call the panel s getgraphics method Returns a Graphics object (a pen for drawing with on the canvas) (We ll learn how to write methods that return things next time) 2. Store a reference to this pen in a variable g Again, this part is old news, the new things are: A DrawingPanel has methods we can call They are part of a DrawingPanel These methods can return things 6

Line by line DrawingPanel panel = new DrawingPanel(300,200); g.setcolor(color.black); A Graphics object, like the one in g, also has methods we can call The setcolor method takes a parameter, which is a Color The setcolor method changes the pen s color No immediate effect; affects subsequent drawings 7

Line by line DrawingPanel panel = new DrawingPanel(300,200); g.setcolor(color.black); g.drawoval(100,100,50,50); Another method a Graphics object has draws an oval The parameters describe its position and its size See how useful parameters are! Details on which parameter is which a little later (and in book) 8

Line by line DrawingPanel panel = new DrawingPanel(300,200); g.setcolor(color.black); g.drawoval(100,100,50,50); g.drawoval(125,100,75,75); Keep drawing objects to make a picture Teaser: Everything we ve learned will help us automate picture drawing Example: A loop to draw similar shapes near each other 9

Where are we 1. What set up do I have to do to use the library? Done 2. What are the basic features of the library? In progress 3. What are the patterns for making the features useful? Next steps: The general organization of the library More basic features (rectangles, filling, colors, etc.) How this is object-oriented and the new Java features we are using Then the fun stuff: Using loops and parameters to make cool and useful pictures 10

Graphical objects - Recap The library gives us 3 new kinds of objects: DrawingPanel: A window on the screen. Graphics: A "pen" to draw shapes/lines on a window. Color: Colors in which to draw shapes. 11

Coordinate system Each (x, y) position is a pixel ("picture element"). (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) 12

Graphics "Pen" objects that can draw lines and shapes Access it by calling getgraphics on your DrawingPanel. Draw shapes by calling methods on the Graphics object. g.fillrect(10, 30, 60, 35); g.filloval(80, 40, 50, 70); 13

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 14

Color Create one using Red-Green-Blue (RGB) values from 0-255 Color name = new Color(red, green, blue); Example: Color brown = new Color(192, 128, 64); Or use a predefined Color class constant (more common) Color.CONSTANT_NAME where CONSTANT_NAME is one of: BLACK, BLUE, CYAN, DARK_GRAY, GRAY, GREEN, LIGHT_GRAY, MAGENTA, ORANGE, PINK, RED, WHITE, YELLOW 15

Using Colors Pass a Color to Graphics object's setcolor method Subsequent shapes will be drawn in the new color. 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); Pass a color to DrawingPanel's setbackground method The overall window background color will change. Color brown = new Color(192, 128, 64); panel.setbackground(brown); 16

Mini-Exercises Write a program that draws a solid blue circle with radius 60 centered at x=100, y=100. Extend your program to outline the circle in black. Reminders: import java.awt.*; public class CircleExample { public static void main(string[] args) { DrawingPanel panel = new DrawingPanel(200, 200); g.drawoval(x, y, width, height); g.filloval(x, y, width, height); g.setcolor(color); outline largest oval that fits in a box of size width * height with top-left at (x, y) fill largest oval that fits in a box of size width * height with top-left at (x, y) set Graphics to paint any following shapes in the given color 17

Mini-exercises - solutions import java.awt.*; // so I can use Graphics public class CircleExample { public static void main(string[] args) { DrawingPanel panel = new DrawingPanel(200, 200); // to make a blue circle with radius 60, // draw an oval with width=height=120 g.setcolor(color.blue); g.filloval(40, 40, 120, 120); // black outline g.setcolor(color.black); g.drawoval(40, 40, 120, 120); 18

Objects (briefly) object: An entity that contains data and behavior. data: Variables inside the object. behavior: Methods inside the object. You interact with the methods; the data is hidden in the object. Constructing (creating) an object: type objectname = new type(parameters); Calling an object's method: objectname.methodname(parameters); 19

Object-oriented Two perspectives on, for example, setting a pen s color: 1. I have a procedure for changing a pen s color and and I will apply it to this pen I have Verb-oriented : focus is on the changer you have Would look like setcolor(g,color.black) 2. I consider a pen s color to be part of the pen, so to change the color I will use one of the pen s methods Noun-oriented : focus is on the pen and what it can do Looks like g.setcolor(color.black); Our Graphics library takes the second approach Most Java libraries do because the language has good support for defining and using libraries this way 20

Where are we 1. What set up do I have to do to use the library? Done 2. What are the basic features of the library? Done 3. What are the patterns for making the features useful? The fun stuff: Using loops and parameters to make cool and useful pictures 21

Drawing with loops The x,y, w,h expression can use a loop counter variable: DrawingPanel panel = new DrawingPanel(400, 300); panel.setbackground(color.yellow); g.setcolor(color.red); for (int i = 1; i <= 10; i++) { g.filloval(100 + 20 * i, 5 + 20 * i, 50, 50); Nested loops are okay as well: DrawingPanel panel = new DrawingPanel(250, 250); 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); 22

Loops that begin at 0 Beginning at 0 and using < can make coordinates easier. Example: Draw ten stacked rectangles starting at (20, 20), height 10, width starting at 100 and decreasing by 10 each time: DrawingPanel panel = new DrawingPanel(160, 160); for (int i = 0; i < 10; i++) { g.drawrect(20, 20 + 10 * i, 100-10 * i, 10); 23

Loops mini-exercise Modify the stacked rectangles program to draw ten stacked rectangles starting at (20, 20), height 10, each one with width 100. (So the width doesn t change.) Width-changing version DrawingPanel panel = new DrawingPanel(160, 160); for (int i = 0; i < 10; i++) { g.drawrect(20, 20 + 10 * i, 100-10 * i, 10); 24

Loops mini-exercise - solution DrawingPanel panel = new DrawingPanel(160, 160); for (int i = 0; i < 10; i++) { g.drawrect(20, 20 + 10 * i, 100, 10); 25

Superimposing shapes When 2 shapes occupy the same pixels, the last drawn "wins." import java.awt.*; public class Car { public static void main(string[] args) { DrawingPanel panel = new DrawingPanel(200, 100); panel.setbackground(color.light_gray); 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); 26

Drawing with methods To draw in multiple methods, you must pass Graphics g. import java.awt.*; public class Car2 { public static void main(string[] args) { DrawingPanel panel = new DrawingPanel(200, 100); panel.setbackground(color.light_gray); drawcar(g); public static void drawcar(graphics g) { 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); 27

Parameterized figures Modify the car-drawing method so that it can draw cars at different positions, as in the following image. Top-left corners: (10, 30), (150, 10) 28

Parameterized answer import java.awt.*; public class Car3 { public static void main(string[] args) { DrawingPanel panel = new DrawingPanel(260, 100); panel.setbackground(color.light_gray); drawcar(g, 10, 30); drawcar(g, 150, 10); public static void drawcar(graphics g, int x, int y) { g.setcolor(color.black); g.fillrect(x, y, 100, 50); g.setcolor(color.red); g.filloval(x + 10, y + 40, 20, 20); g.filloval(x + 70, y + 40, 20, 20); g.setcolor(color.cyan); g.fillrect(x + 70, y + 10, 30, 20); 29

Drawing parameter question Modify drawcar to allow the car to be drawn at any size. Existing car: size 100 Second car: size 50, top/left at (150, 10) Then use a for loop to draw a line of cars. Start at (10, 130), each car size 40, separated by 50px. 30

Drawing parameter answer import java.awt.*; public class Car4 { public static void main(string[] args) { DrawingPanel panel = new DrawingPanel(210, 100); panel.setbackground(color.light_gray); drawcar(g, 10, 30, 100); drawcar(g, 150, 10, 50); for (int i = 0; i < 5; i++) { drawcar(g, 10 + i * 50, 130, 40); public static void drawcar(graphics g, int x, int y, int size) { g.setcolor(color.black); g.fillrect(x, y, size, size / 2); g.setcolor(color.red); g.filloval(x + size / 10, y + 2 * size / 5, size / 5, size / 5); g.filloval(x + 7 * size / 10, y + 2 * size / 5, size / 5, size / 5); g.setcolor(color.cyan); g.fillrect(x + 7 * size / 10, y + size / 10, 3 * size / 10, size / 5); 31

Extra exercises What follows are a couple exercises related to the slides that you can try on your own (not done in class) 32

More Drawing w/ loops questions Code from earlier slide: DrawingPanel panel = new DrawingPanel(160, 160); for (int i = 0; i < 10; i++) { g.drawrect(20, 20 + 10 * i, 100-10 * i, 10); Write variations of the above program that draw the figures at right as output. 33

Drawing w/ loops answers Solution #1: for (int i = 0; i < 10; i++) { g.drawrect(20 + 10 * i, 20 + 10 * i, 100-10 * i, 10); Solution #2: for (int i = 0; i < 10; i++) { g.drawrect(110-10 * i, 20 + 10 * i, 10 + 10 * i, 10); 34