CS 5JA Introduction to Java Graphics One of the powerful things about Java is that there is.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
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.
Variables and Operators
Week 7: Input and Output 1.  Now we are going to talk a little bit about output  You have a lot of experience with System.out.println() and System.out.print()
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
1 Objects, Classes, and Packages “Static” Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.
10-Jun-15 Using Objects. 2 Overview In this presentation we will discuss: Classes and objects Methods for objects Printing results.
Using Objects. Overview In this presentation we will discuss: –Classes and objects –Methods for objects –Printing results.
CS 280 Data Structures Professor John Peterson. Quiz 4 Recap Consider the following array: {2, 6, 7, 3, 4, 1, 5, 9}. Draw this in tree form and then show.
Programming with Collections Collections in Java Using Arrays Week 9.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS 5JA Introduction to Java note... You must use your umail when you submit homework. In fact every time you me or any of the TAs you should.
CS 106 Introduction to Computer Science I 02 / 22 / 2008 Instructor: Michael Eckmann.
A Simple Applet. Applets and applications An applet is a Java program that runs on a web page –Applets can be run from: Internet Explorer Netscape Navigator.
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
28-Jun-15 Using Objects. 2 Overview In this presentation we will discuss: Classes and objects Methods for objects Printing results.
A Simple Applet.
29-Jun-15 Using Objects. 2 Classes and objects The type of an object is the class that describes that object If we say int count, the type of count is.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
METHODS AND SCOPE CSCE More on Methods  This is chapter 6 in Small Java.
Lesson 27: Introduction to the Java GUI. // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main.
Unit 3: Java Data Types Math class and String class.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Lab 6: Shapes & Picture Extended Ellipse & Rectangle Classes Stand-Alone GUI Applications.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
CS 112 Department of Computer Science George Mason University CS 112 Department of Computer Science George Mason University Final Review Lecture 14.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Programs and Classes A program is made up from classes Classes may be grouped into packages A class has two parts static parts exist independently Non-static.
Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
1 CS161 Introduction to Computer Science Topic #9.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A class represents a single concept from the problem domain, or a.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
The Math Class Methods Utilizing the Important Math Operations of Java!
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Week 12 - Wednesday.  What did we talk about last time?  Hunters and prey.
Week 12 - Monday.  What did we talk about last time?  Defining classes  Class practice  Lab 11.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Session 7 More Implications of Inheritance & Chapter 5: Ball World Example.
CS 112 Introduction to Programming Java Graphics Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Chapter 9 Introduction to Arrays Fundamentals of Java.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
 An object is basically anything in the world that can be created and manipulated by a program.  Examples: dog, school, person, password, bank account,
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
4. Java language basics: Function
Using Objects 21-Nov-18.
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Presentation transcript:

CS 5JA Introduction to Java Graphics One of the powerful things about Java is that there is a large library of code that already exists. Not only can you use it, but you can extend the library with your own custom code. We’ll talk more about his in detail in a later class, but I just want to give you enough to use the built in user- interface widgets (called “Swing”) so that you can make some simple 2D graphics. The basic “widget” in Java is called a “JFrame” (J for java...). A frame is the square object that contains an application. It usually has a title bar, a button for minimizing and closing the application, and a panel that contains the content of the application. This panel is called a JPanel (J for java...). You can create your own JPanel and do anything you want to it. For our next homework assignment we’re going to learn how to make digital art.

CS 5JA Introduction to Java Graphics The JPanel contains lots of built-in methods to handle all sorts of things, like recognizing mouse focus, knowing how to minimize itself, knowing when to be behind another application, etc. So we can utilize all of this functionality, but customize it for our purposes. Here’s how we do it: public class MyArtwork extends JPanel The JPanel object has a built-in method called paintComponent which describes how to draw itself. By default it just draws a big gray square. You can override this method and tell your JPanel to draw itself in a more interesting way.

CS 5JA Introduction to Java Graphics //overriding! public void paintComponent(Graphics artist) { //dip paintbrush into new color artist.setColor(Color.BLACK); //paint a rectangle artist.fillRect(30, 30, 100, 100); } Each JPanel has a built-in palette and a built-in artist. The artist is prized for his graphics abilities, so he is of data type “Graphics”. Here we name him “artist”. In this method, we are telling the artist inside JPanel to paint the screen. In the first line we are telling artist to change colors. In the second line we are telling him to draw a rectangle.

CS 5JA Introduction to Java Graphics You can set the color using built-in colors, or by defining your own color using its RGB values. Let’s make a new color and tell the artist to use it: Color newColor = new Color( 255, 0, 0 ); //Red Green Blue artist.setColor(newColor); The range of the canvas is defined by pixel values. You can think of a graph, except the origin (0, 0) is in the top-left corner, and the y value is reversed. The y-value increases as you go down. The x-value increases as you go toward the right. Code example...

CS 5JA Introduction to Java Math Last week we talked about the difference between static and non-static methods. A normal instance variable exists only after an object has been instantiated (using the new keyword). Likewise, a normal method can be invoked only after the object is instantiated. The method can refer to any of the instance variables, static or non-static. The static variables are also available to normal methods because the definition of a static variable is that it is created before the object is instantiated, and it is shared with all instances of the object. That is, if you change the value of a static variable, you change it for all the objects of that class. For this reason, static variables are sometimes called class variables, and static methods are sometimes called class methods.

CS 5JA Introduction to Java Math So when would you want to use the static keyword? Because sometimes you want a particular variable to apply to all instances of a class. For example, in the Math package (built into the Java language) there are constants which never change, like the number “pi” and the number “e”. In fact, all methods inside Math are static. Math isn’t really a normal object. It’s just a collection of methods that immediately return an answer. No variables are created or manipulated or stored. For example: double number =.5001; double roundedNumber = Math.round(number);

CS 5JA Introduction to Java Math For example: double number =.5001; long roundedNumber = Math.round(number); the round method looks at the number that is passed in and returns a number (a long whole number) that is closest to the decimal. That is, it rounds it up or down. Since the round method static, you don’t need to instantiate a Math object. You can refer to the method using dot notation on the class itself.

CS 5JA Introduction to Java Math Here are some other common Math methods: double number =.999; long flooredNumber = Math.floor(double number); double number =.001; long roofedNumber = Math.ceiling(double number); double number = 9.0; double squareRoot = Math.sqrt(number); double squared = Math.pow(number, 2); double cubed = Math.pow(number, 3); double angle = 90.0; double sine = Math.sin(Math.toRadians(angle)); double number1through10 = Math.random() * 10.0;

CS 5JA Introduction to Java Math What if you wanted to make a random number 1 through 10 that was an integer? It takes a few steps: a. Make a random number between 0.0 and 1.0 b. Multiply it by 9 – now your number is between 0.0 and 9.0 c. Round it using the Math.round method -- now your number is 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9 d. Add one to it – now your number is between 1 and 10. You could also do this in one line: long num = Math.round(Math.random() * 9) + 1;

CS 5JA Introduction to Java Math What if you wanted to make a random number between -5 and 5?

CS 5JA Introduction to Java Math What if you wanted to make a random whole number between -5 and 5? How many possible numbers are there? 11 -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 So, Math.random() * 10 Then round: between 0 and 10 (11 total numbers, including the 0) Then subtract 5: between -5 and 5 (11 total numbers, including the 0) long num = Math.round(Math.random() * 11) - 5;

CS 5JA Introduction to Java Math What if you were building an audio application and you wanted to display your sound waves on the screen? Sound waves can be mathematically modeled with a bunch of sine waves: so let’s make a small application which draws a sine wave. What do we do first? What elements do we need– not for a complicated version, just for a prototype that draws a single sinewave on the screen...

CS 5JA Introduction to Java Math We need something to draw on. We need a sinewave. [see SinewaveTest.java]

CS 5JA Introduction to Java Math What if you wanted to make some random polka dots? [see PolkadotTest.java]

CS 5JA Introduction to Java Math What if you wanted to make sure that the colors didn’t change... [see PolkadotTest.java]

CS 5JA Introduction to Java Arrays An array is simply an ordered list of elements. All the elements are of the same data type. Thus you can talk about an array of ints, or an array of Strings, or an array of Accounts, etc. Arrays are used to keep track of a group of similar information. For instance, if I am a bank and I have a list of Accounts I can store these Accounts in an array. This way, when I want to check the balance of all of them, I don’t need to get each Account one at a time. Instead I can iterate through the list more easily.

CS 5JA Introduction to Java Arrays It’s kind of like taking a scattered pile of papers and putting them neatly in a filing cabinent. Here’s how you declare an array and initialize an array int[] numberArray; numberArray = new int[100]; In the declaration, the brackets next to the data type indicate that you are creating an array of that type. In the initialization of the array, the number in the brackets describe how many elements are in the array. Right now, you have an empty filing cabinet with room for 100 numbers. But there is nothing in the filing cabinent.

CS 5JA Introduction to Java Arrays We can interate through the array (the filing cabinet) and put number in each position: for (int i = 0; i < numberArray.length; i++) { numberArray[i] = (int) (Math.random() * 10); } Likewise, later on we can access the array and iterate through every element in the array: for (int i = 0; i < numberArray.length; i++) { System.out.println(“number at position “ + i + “ is “ + numberArray[i]); }

CS 5JA Introduction to Java Arrays We get at the data in the array using the indexing notation: int checkNumber = myNumbers[0]; int checkNumber = myNumbers[55]; etc...