slides - Angus Forbes

Download Report

Transcript slides - Angus Forbes

CS 5JA Introduction to Java
Graphics
http://java.sun.com/javase/6/docs/api/java/awt/Graphics.html
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 userinterface 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,
//Red
artist.setColor(newColor);
0,
Green
0 );
Blue
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...