lecture5.ppt

advertisement
CS110- Lecture 5
Feb 14, 2005

Announcements


Exam 1 is next week.
It will cover:
Section 1.4 – 1.6, Chapter 2,
Section 3.1 – 3.5, Section 4.1, Section 4.3
Section 5.1 – 5.4.

Project 1 is due on March 6 (only for
Section 1 and Section 2).
6/30/2016
CS110-Spring 2005, Lecture 5
1
Agenda

Graphics







Coordinate System
Representing Color
Applets
Drawing Shapes (Graphics Class)
Creating Objects
String class
Packages
6/30/2016
CS110-Spring 2005, Lecture 5
2
Graphics




A picture, like other information stored on a
computer must be digitized by breaking the
information into pieces and representing
those pieces as numbers.
We break the picture into pixels.
A pixel is not an absolute unit of length. The
size of a pixel can be different on different
screens.
You can think of your computer screen as
being covered by small squares. You cannot
show anything smaller than these squares. A
pixel is one of these squares.
6/30/2016
CS110-Spring 2005, Lecture 5
3
Graphics


So a pixel is the smallest length your
screen is capable of showing.
The number of pixels used to represent
the picture is called picture resolution
and the number of pixels that can be
displayed by the monitor is called the
monitor resolution.
6/30/2016
CS110-Spring 2005, Lecture 5
4
Coordinate System
positive X
direction
(0,0)
50 pixels
Y axis
(100, 50)
100 pixels
(0,0)
X axis
positive Y
direction
6/30/2016
CS110-Spring 2005, Lecture 5
5
Coordinate System
(0,0)
50 pixels
(100, 50)
drawLine(x1,y1,x2,y2)
100 pixels
drawLine(100,50,200,100)
6/30/2016
200 pixels
CS110-Spring 2005, Lecture 5
100 pixels
(200,100)
6
Coordinate System
(0,0)
50 pixels
(100, 50)
drawRect(x,y,width,height)
drawRect(100,50,200,100)
100 pixels
100
(100,150
(x, y+height)
6/30/2016
200
(x+width,y)
(300,50)
CS110-Spring 2005, Lecture 5
(300, 150)
(x+width, y+height)
7
Coordinate System
(0,0)
50 pixels
(100, 50)
drawOval(x,y,width,height)
drawOval(100,50,200,100)
100 pixels
100
(100,150
(x, y+height)
6/30/2016
200
(x+width,y)
(300,50)
CS110-Spring 2005, Lecture 5
(300, 150)
(x+width, y+height)
8
Coordinate System
(0,0)
50 pixels
(100, 50)
drawArc(x,y,width,height,start
Angle, arcAngle)
drawArc(100,50,200,100,180,
180)
6/30/2016
200
(x+width,y)
(300,50)
100 pixels
100
(100,150
(x, y+height)
CS110-Spring 2005, Lecture 5
(300, 150)
(x+width, y+height)
9
Representing Colors




A black and white picture can be stored
by representing each pixel using a single
bit.
0 – white pixel, 1 – black pixel
In Java colors are specified by three
numbers that are collectively referred to
as RGB value.
Color class in library to define and
manage colors.
6/30/2016
CS110-Spring 2005, Lecture 5
10
Applets
Java Programs
Java Applications
Java Applets
public class Welcome
{
public static void main(String[] args)
{
}
}
(intended to be embedded into an
HTML document and executed using
Web Browser)
import javax.swing.JApplet;
import java.awt.*;
public class Welcome2 extends JApplet
{
public void paint (Graphics page)
{
}
6/30/2016
}
CS110-Spring 2005, Lecture 5
11
Drawing Shapes
import javax.swing.JApplet;//uses the JApplet in Swing package
import java.awt.*;
// uses the AWT package
public class HappyFace extends JApplet //beginning of class
{
public void paint (Graphics page) //invoked automatically
{
All these
page.drawOval(100,50,200,200);
services are
page.fillOval(155,100,10,20);
provided by
page.fillOval(230,100,10,20);
Graphics class
page.drawArc(150,160,100,50,180,180);
}
}
6/30/2016
CS110-Spring 2005, Lecture 5
12
Drawing Shapes
6/30/2016
CS110-Spring 2005, Lecture 5
13
Creating Objects

In Java a variable name represents either a
primitive value or an object.
 int i;
i

String name; // name is an object of type String.
name

The second declaration creates a String variable
that holds a reference to a String object. An
object variable does not hold an object itself. It
holds an address of an Object.
6/30/2016
CS110-Spring 2005, Lecture 5
14
Creating Objects

An object variable can also be set to
null (a reserved word).




String name = null;
A null reference specifically indicates
that a variable does not refer to an
object.
i
10
i = 10;
name = new String(“James Gosling”);
name
6/30/2016
“James Gosling”
CS110-Spring 2005, Lecture 5
15
Creating Objects



The act of creating an object using the
new operator is called instantiation.
An object is said to be an instance of a
particular class.
After an object is created we can use
dot operator to access its methods.

i = name.length();
6/30/2016
i
13
CS110-Spring 2005, Lecture 5
16
Aliases
int y = 6;
y
int x = 3;
x
3
x = y;
y
6
6
String name1 = “James”;
String name2 = “Lewis”;
name2 = name1;
6/30/2016
x
6
name1
“James”
name2
“Lewis”
name1
“James”
name2
CS110-Spring 2005, Lecture 5
17
Aliases


All references to the object originally
referenced by name2 are gone, that object
can not be used again in the program. The
program can no longer invoke its methods.
At this point the object is called garbage
because it serves no useful purpose.
Java performs automatic Garbage
collection.
6/30/2016
CS110-Spring 2005, Lecture 5
18
String Class


String object is immutable. Once created
it can not be shortened or lengthened.
A character in a String can be specified by
its position (index).
J
a m e
0 1

s
2 3 4
name.replace(‘J’,’G’);
6/30/2016
CS110-Spring 2005, Lecture 5
19
Packages




A Java Standard class library is a set of
classes that supports the development of
programs.
The classes are grouped into packages.
Each class is part of a particular package.
String and System are part of java.lang
package.
Scanner is part of the java.util package.
6/30/2016
CS110-Spring 2005, Lecture 5
20
The import declaration



The classes of the java.lang package
are automatically available for use.
To use classes from any other package
we must use an import declaration.
The import declaration specifies the
packages and classes that will be used
in the program.
6/30/2016
CS110-Spring 2005, Lecture 5
21
The import declaration

import java.util.*;


import java.util.Scanner;


All classes in java.util are available for use.
Only Scanner class is available.
If you use more than one class from a
package than use first notation
otherwise second.
6/30/2016
CS110-Spring 2005, Lecture 5
22
Reading Exercise



Section 3.4, 3.5
Section 5.1- 5.4
If you are interested you can read:
Section 4.1 and Section 4.3
6/30/2016
CS110-Spring 2005, Lecture 5
23
Download