CSSE 220. Intro to Java Graphics. Check out IntroToJavaGraphics and BiggestFan projects from SVN

Similar documents
CP122 Computer Science I. Chapter 2: Using Objects

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

Chapter 2 Using Objects. Types. Number Literals. A type defines a set of values and the operations that can be carried out on the values Examples:

Final Examination Semester 2 / Year 2010

Java Simple Data Types

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

JAVA OPERATORS GENERAL

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Java Simple Data Types

Final Examination Semester 2 / Year 2012

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Survey #2. Variable Scope. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings. Scope Static.

Survey #2. Teen Talk Barbie TM Reloaded. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Partially Filled Arrays ArrayLists

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Lecture 7 Objects and Classes

Programming Basics. Digital Urban Visualization. People as Flows. ia

Java Classes - Using your classes. How the classes you write are being used

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

CS 455 Midterm Exam 1 Spring 2013 [Bono] Feb. 21, 2013

ASSIGNMENT 5 Objects, Files, and More Garage Management

Exam Duration: 2hrs and 30min Software Design

Name CIS 201 Midterm II: Chapters 1-8

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2018 Instructors: Bill & Bill

Java Programming for Selenium

Object-Oriented Programming Design. Topic : Graphics Programming GUI Part I

EECS 1001 and EECS 1030M, lab 01 conflict

Key Java Simple Data Types

Final Examination Semester 2 / Year 2011

CS/ENGRD 2110 FALL2017. Lecture 4: The class hierarchy; static components

Programming Graphics (P1 2006/2007)

Inheritance, Polymorphism, and Interfaces

CS 11 java track: lecture 3

Submit your solution to the Desire2Learn drop box. You may submit as many versions as you like the last one will be graded.

Lesson 10: Quiz #1 and Getting User Input (W03D2)

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

Object Orientated Programming in Java. Benjamin Kenwright

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2017 Instructors: Bill & Bill

CMP-326 Exam 2 Spring 2018 Total 120 Points Version 1

This exam is open book. Each question is worth 3 points.

CS 312 Midterm 1 Spring 2013

Lecture 4: The class hierarchy; static components

Survey #2. Programming Assignment 3. Final Exam. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu.

Object Oriented Programming and Design in Java. Session 2 Instructor: Bert Huang

CSE 113. Announcements

Control Statements: Part Pearson Education, Inc. All rights reserved.

CSCI 355 LAB #2 Spring 2004

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Systems Programming. Bachelor in Telecommunication Technology Engineering Bachelor in Communication System Engineering Carlos III University of Madrid

PRINCIPLES OF SOFTWARE BIM209DESIGN AND DEVELOPMENT 00. WELCOME TO OBJECTVILLE. Speaking the Language of OO

Express Yourself. Writing Your Own Classes

ICOM 4015: Advanced Programming

Announcements. 1. Forms to return today after class:

Computational Expression

2. [20] Suppose we start declaring a Rectangle class as follows:

Software Engineering. Functions

Composite Pattern - Shapes Example - Java Sourcecode

H212 Introduction to Software Systems Honors

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

CSE 1223: Introduction to Computer Programming in Java Chapter 6 ArrayLists

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Selenium Class 9 - Java Operators

Midterms Save the Dates!

Last time s big ideas

Object Oriented Programming COP3330 / CGS5409

CPS109 Lab 7. Source: Big Java, Chapter 7 Preparation: read Chapter 7 and the lecture notes for this week.

CSE 331 Final Exam 6/7/16

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

Programming Languages and Techniques (CIS120)

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

What does this print?

Object Oriented Programming

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

Lecture 2: Java & Javadoc

CSCI 136 Written Exam #2 Fundamentals of Computer Science II Spring 2015

CS 121 Intro to Programming:Java - Lecture 2. Professor Robert Moll (+ TAs) CS BLDG

1.00 Lecture 8. Using An Existing Class, cont.

CMP 326 Midterm Fall 2015

CS 106A, Lecture 27 Final Exam Review 1

Chapter 2 Using Objects

CSCI 355 Lab #2 Spring 2007

CS/ENGRD 2110 SPRING Lecture 5: Local vars; Inside-out rule; constructors

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

CPSC 324 Topics in Java Programming

Lecture 06: Classes and Objects

CS121/IS223. Object Reference Variables. Dr Olly Gotel

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Programming in the Large II: Objects and Classes (Part 2)

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

CS 231 Data Structures and Algorithms, Fall 2016

Method Overriding in Java

CE221 Programming in C++ Part 1 Introduction

CS 121 Intro to Programming:Java - Lecture 2. Professor Robert Moll (+ TAs) CS BLDG

8/23/2011. Chapter Goals

2/9/2012. Chapter Two: Using Objects. Chapter Goals

1.00/1.001 Introduction to Computers and Engineering Problem Solving Spring Quiz 2

Agenda: Notes on Chapter 3. Create a class with constructors and methods.

Chapter 4 Defining Classes I

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

Transcription:

CSSE 220 Intro to Java Graphics Check out IntroToJavaGraphics and BiggestFan projects from SVN

Announcement Exam 1 Starts Wednesday the 23 rd We re splitting the exam into written and programming and doing them on separate days Before Monday s class Complete the written portion of the 201510 written exam (provided in class today or linked on the schedule page) Bring any questions you have to Monday s class Be sure to time yourself to make sure you can complete it within the given 50 minutes

Outline Static (by examples) Live coding: a Java graphics program

Understanding static STATIC

Why fields can t always be static public class Student { private String name; private char grade; public Student( String name, char grade){ this.name = name; this.grade = grade; public static void main(string[] args) { Student a = new Student("Adam", 'A'); Student b = new Student("Bryan", 'B'); Student c = new Student("Chris", 'C'); System.out.println(a); System.out.println(b); System.out.println(c); @Override public String tostring() { return name + " has a grade of " + grade; OUTPUT: Adam has a grade of A Bryan has a grade of B Chris has a grade of C

public class Student { private String name; private static char grade; public Student( String name, char grade){ this.name = name; this.grade = grade; Why not make the grade static? public static void main(string[] args) { Student a = new Student("Adam", 'A'); Student b = new Student("Bryan", 'B'); Student c = new Student("Chris", 'C'); System.out.println(a); System.out.println(b); System.out.println(c); @Override public String tostring() { return name+ " has a grade of " + grade; OUTPUT: Adam has a grade of C Bryan has a grade of C Chris has a grade of C Static means there's only one instance of a field/method for every instance of a class that's created. So when you change a grade, they all change.

When do we make methods static? Utility Methods Things like abs, sqrt, etc. Don t need an instance of a class to run them How do I know? No references to non-static fields/methods No this keyword used in method

public class Car { double mileage; //other stuff public double getmilestravelled() { return this.mileage; public static double convertmilestokm(double numberofmiles) { return numberofmiles * 1.609344f; //Elsewhere //requires you to have a car object Car mycar = new Car(); //requires you to have a car object System.out.println(myCar.getMilesTravelled());//output depends on code //can be called on the class Car itself System.out.println(Car.convertMilesToKm(77));//output is 123.919488

public class Bicycle { private int speed; private static int numcreated = 0; public Bicycle(int speed) { this.speed = speed; Bicycle.numCreated++; public int getspeed() { return this.speed; public static int getnumcreated() { return Bicycle.numCreated; //No requirement to have a Bicycle yet System.out.println(Bicycle.getNumCreated()); Bicycle mybike1 = new Bicycle(18); Bicycle mybike2 = new Bicycle(1); System.out.println(Bicycle.getNumCreated() + " " + mybike1.getspeed()); 0 2 18

Simple Graphics JAVA GRAPHICS

Simplest Java Graphics Program import javax.swing.jframe; /** * From Ch 2, Big Java. * @author Cay Horstmann */ public class EmptyFrameViewer { /** * Draws a frame. * @param args ignored */ public static void main(string[] args) { JFrame frame = new JFrame(); frame.setsize(300,400); frame.settitle("an Empty Frame"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); Display the frame This code is already in your project for today Creates a graphics frame object Configures it Tells Java to exit program when user closes the frame

MyViewer and MyComponent (Based on RectangleViewer and RectangleComponent from Big Java) LIVE CODING

Other Shapes new Ellipse2D.Double(double x, double y, double w, double h) new Line2D.Double(double x1, double y1, double x2, double y2) new Point2D.Double(double x, double y) new Line2D.Double(Point2D p1, Point2D p2) new Arc2D.Double(double x, double y, double w, double h, double start, double extent, int type) Try these! Add an ellipse and both kinds of lines to MyComponent

Using translate and rotate successfully Translate and rotate to adjust the state of the pen It is usually easier to move the pen, then draw in a fixed configuration around (0,0), then move the pen back Make (0,0) your center of rotation can change the point of origin using translate() so you can rotate different portions of the component

Translate (-50,-50) (0,0) Originally, origin of 0,0 at top left of screen (with (50,50) marked below) If we called g2.translate(50, 50), here s what would happen: (50,50) (0,0) Always want to make sure we reset the pen, so when we re done, we need to translate back to where we started, in this case: g2.translate(-50,-50)

Rotate Let s say we ve already translated to put the origin at (50,50) (mostly to make the slides look nicer) (0,0) If we drew a rectangle here like this: g2.drawrect(0, 0, 50, 10);, we would get something like What would happen if we called g2.rotate(math.pi/4); (radians) then call g2.drawrect(0, 0, 50, 10); again? Remember, y is positive down instead of up, so the rotate will go reverse of what you might be expecting

Work on the biggest fan code We ll walk through it together to explain how the classes work Then you should modify the fan to print one blade vertically use transform to move (0,0) to the center of the fan and then draw from there

Scene project SCENE INTRODUCTION